Sobes.tech
Middle

Is it possible to hang Node.js on the JavaScript/V8 side without using an infinite while(true) loop? How to make sure the promise queue never empties?

sobes.tech AI

Answer from AI

Yes, in Node.js it is possible to 'hang' from the JavaScript/V8 side without using an infinite loop while(true). This is possible if you constantly add tasks to the microtask queue (promises), preventing the event loop from moving on to other operations.

To ensure the promise queue never empties, you can recursively create promises that, in their then, create the next promise, for example:

function keepQueueBusy() {
  Promise.resolve().then(() => {
    // some work
    keepQueueBusy();
  });
}

keepQueueBusy();

Thus, microtasks (promises) are constantly added to the queue, and the event loop cannot proceed to handle macrotasks or other operations, leading to a hang.

This is not an infinite loop in the classical sense, but the effect is similar — constant loading of the microtask queue.