skip to content

Why would a Node.js service whose retry helper reschedules itself with process.nextTick until it succeeds stop answering health checks and firing timers?

level: seniorimportance: should knowfreq 50%

answer

  1. the queue that must empty first
  2. callbacks added during the drain
  3. the poll phase never comes
  4. no stack overflow, no error
  5. yield with setImmediate or a timer

basics

~20 s

Node drains the whole next tick queue, including callbacks added during the drain, before the event loop continues. A self-rescheduling tick keeps it non-empty, so the loop never reaches timers or the poll phase that serves sockets.

solid answer

~50 s

Node drains the next tick queue completely before letting the event loop continue, and callbacks queued during the drain join the same drain. A retry that reschedules itself with `process.nextTick()` therefore keeps the queue non-empty forever: the loop never reaches the poll phase (no socket reads, no new connections, no file or DNS callbacks), the timers phase or the check phase. Often the state the retry waits for is set by an I/O callback that can now never run, so a brief wait becomes infinite. Because each callback runs on an unwound stack there is no `RangeError`, no crash, just a pinned core. `queueMicrotask()` or `await Promise.resolve()` loops starve the loop the same way. The fix is to wait for the event that signals readiness, or yield with `setImmediate()` or a back-off timer, and to cap the attempts.

code

javascript · 10 lines
javascript
const fs = require('node:fs');

let ticks = 0;
setTimeout(() => console.log('timer after', ticks, 'ticks'), 0);
fs.readFile(__filename, () => console.log('readFile after', ticks, 'ticks'));

function spin() {
  if (++ticks < 2_000_000) process.nextTick(spin);
}
process.nextTick(spin);

go deeper

for a junior

Remember that a callback which keeps scheduling itself with process.nextTick stops every timer and every request from being handled.

for a middle

Explain why: the tick queue drains completely, new entries included, before the loop continues, and why setImmediate() behaves differently.

for a senior

Diagnose the silent hang from symptoms and a CPU profile, spot the I/O dependency that makes the retry infinite, and replace polling with an event, a yield or a bounded back-off.

for a principal

Make unbounded self-rescheduling through tick or microtask queues a review-blocking pattern, and require every retry helper to have a cap and a delay.

## The scenario A service has a small retry helper. When an operation fails because some state is not ready yet (a connection not marked open, a lock not released, a buffer not filled), the helper calls itself again through `process.nextTick()`: ```js function retry(op, done) { const result = op(); if (result.ok) return done(null, result.value); process.nextTick(retry, op, done); // try again "as soon as possible" } ``` In testing the state is usually ready, so the helper rarely loops. In production the state sometimes is not, and the process stops serving: health checks time out, timers never fire, new connections wait in the kernel's backlog and are never read, one CPU core sits at 100%, and nothing is logged. ## Why the loop never moves Node drains the **next tick queue** after the current operation and **before the event loop continues**. The drain is a loop of its own: it keeps taking callbacks off the queue until the queue is empty, and callbacks added *during* the drain are processed in that same drain. Node's documentation warns that recursive `process.nextTick()` calls can create an infinite loop and "starve" I/O by preventing the event loop from reaching the poll phase. So every `retry` call queues the next one, the queue is never empty, and the loop is stuck between two operations: - **no poll phase**, so no socket data, no new connections, no completed file or DNS callbacks; - **no timers phase**, so no `setTimeout()` callbacks, including any timeout meant to abort the retry; - **no check phase**, so no `setImmediate()` callbacks. The trap closes on itself: the state the helper is waiting for is usually set **by an I/O callback**, and that callback can never run. A retry that would succeed after a few milliseconds becomes infinite. ## Why there is no stack overflow Each tick callback runs after the previous one has returned and the JavaScript stack has unwound. The recursion is through the queue, not the stack, so it never throws `RangeError: Maximum call stack size exceeded`. That is why the failure is silent: no exception, no crash, no `'uncaughtException'`, just a busy process that no longer answers. ## Promises starve it just as well The same happens with V8's **microtask queue**. A loop such as `while (!ready) await Promise.resolve();`, or a retry rescheduled with `queueMicrotask()`, keeps the microtask queue non-empty, and Node will not let the loop continue until both queues are empty. Swapping `process.nextTick()` for `queueMicrotask()` does not fix starvation. | Rescheduled with | Does pending I/O run between attempts? | |---|---| | `process.nextTick()` | no | | `queueMicrotask()` or `await Promise.resolve()` | no | | `setImmediate()` | yes, an immediate queued from an immediate waits for the next iteration | | `setTimeout(fn, delay)` | yes, and it adds a deliberate pause | ## Diagnosing it in production 1. **Symptoms**: CPU pinned on one core, requests and health checks timing out, timers silent, memory flat, no errors. 2. **Measure the loop**: an event-loop delay monitor shows delay growing without bound; measuring that belongs with the material on blocking and lag measurement. 3. **Profile**: a CPU profile taken with `--cpu-prof` (or through the inspector) shows time concentrated in the retried function and Node's internal tick processing, with no I/O callbacks in between. 4. **Read the code** for any function that reschedules itself through `process.nextTick()`, `queueMicrotask()` or an `await` of an already-resolved promise, in a loop with no upper bound. ## The fix - **Wait for the event, not in a loop**: if the operation waits for a connection, listen for its `'connect'` or `'ready'` event instead of polling. - **Yield to the loop between attempts**: reschedule with `setImmediate()` so I/O can run, or with a timer and back-off so a failing dependency is not hammered; the timer details belong with the material on timers and immediates. - **Bound the retries**: cap attempts or total time, and fail with an error the caller can handle. - **Keep `process.nextTick()` for one-shot deferrals**, such as emitting an event after a constructor returns, never for loops. A minimal bounded version that yields to the loop between attempts: ```js function retry(op, done, attempts = 0) { const result = op(); if (result.ok) return done(null, result.value); if (attempts >= 100) return done(new Error('gave up after 100 attempts')); setImmediate(retry, op, done, attempts + 1); // I/O runs before the next try } ```

  • Does replacing process.nextTick() with queueMicrotask() in the retry fix the starvation?
    No. Node lets the event loop continue only once both the next tick queue and V8's microtask queue are empty. A callback that keeps queueing microtasks, or an `await Promise.resolve()` loop, keeps the microtask queue non-empty in the same way, so timers and I/O still never run.
  • Why does rescheduling with setImmediate() let I/O run between attempts?
    Immediates run in the check phase, and an immediate queued from inside an executing immediate callback waits for the next loop iteration. Between two attempts the loop therefore passes through timers and the poll phase, so socket reads, file callbacks and health-check requests get served.
  • How would you confirm this in a live process that has stopped responding?
    A pinned core with flat memory and no errors points to a busy loop. An event-loop delay monitor shows the delay climbing without bound, and a CPU profile from `--cpu-prof` or the inspector shows the retried function and Node's tick processing, with no I/O callbacks between them.

A receptionist who answers each ringing desk phone, but whose own phone rings again the instant it is put down: the queue at the counter never gets served, although nobody there is doing anything wrong.

  • process.nextTick() yields to the event loop between each queued callback.
  • Endless nextTick recursion ends with a Maximum call stack size exceeded error.
  • Switching the retry to queueMicrotask() lets pending I/O run between attempts.
  • A retry that only ever uses async APIs cannot block the event loop.
  • Node lets the event loop move on after a fixed number of nextTick callbacks.