javascript / expert
Snippet
Event Loop: Microtask Queue Starvation
In Node.js, microtasks (process.nextTick and Promises) are processed immediately after the current operation and before the Event Loop continues to the next phase. Recursively calling process.nextTick creates an infinite loop of microtasks that starves the Event Loop, preventing I/O and timers from ever executing.
snippet.js
1
2
3
4
5
6
7
8
9
const recursiveTick = () => {process.nextTick(recursiveTick);};setImmediate(() => {console.log('This will never run');});recursiveTick();
nodejs
Breakdown
1
process.nextTick(recursiveTick);
Schedules the function to run in the microtask queue, which is drained completely before the next event loop phase.
2
setImmediate(() => { ... });
Schedules a callback in the 'check' phase of the event loop, which will be blocked by the infinite microtask loop.