← Back to Node.js, page 6

nodejs-0101 · Medium mid

A stream pipeline never finishes and the process exits quietly. How do you find out why a stream has stalled?

A stream stalls when one stage never calls its callback, nobody consumes the readable side, or the writer ignores 'drain'. Node does not warn, and when no timer or socket is left the process simply exits, often with code 0. Inspect readableFlowing, readableLength, writableLength and writableNeedDrain on each stage to see where data stopped, and add a watchdog timer in tests.

Full answer

Stalled streams are hard to see because nothing throws. A stream is a state machine, so the way to debug it is to read its state.

The usual causes:

  • A Transform or Writable never calls its callback for a chunk (a missed code path, an early return). The next chunk is never requested.
  • A Readable has no consumer. Until you attach a 'data' listener, call resume(), pipe it or iterate it, nothing flows (readableFlowing is null).
  • A writer loop ignores the false result of write() and never waits for 'drain', or waits for 'drain' that was never needed.
  • One side finished but the other side was never ended (end() was not called, or push(null) was missing).
  • An error is swallowed because pipe() was used without handlers.

Useful properties:

  • readable.readableFlowing: null (nobody has asked yet), true or false (paused).
  • readable.readableLength and writable.writableLength: bytes or objects waiting in the buffer.
  • writable.writableNeedDrain: true when write() returned false and 'drain' has not fired yet.
  • readable.readableEnded and writable.writableFinished: the end of each side was reached.

Process exit is a clue. If the event loop has nothing left to do, Node exits. A pipeline that is waiting for a callback that will never come does not keep the process alive, so you see no error and no output. In a service this shows up as requests that hang while the stream sits idle with open sockets.

Habits that prevent it: use stream/promises pipeline, put timeouts on network streams, call the callback in every code path (use try/finally), and test streams with a short watchdog timer.

javascript
const { Readable, Transform, Writable, pipeline } = require('node:stream');

// Bug: the transform never calls its callback for the chunk 'b'.
const buggy = new Transform({
  transform(chunk, encoding, callback) {
    if (chunk.toString() === 'b') return; // forgot callback()
    callback(null, chunk);
  },
});
const sink = new Writable({ write(chunk, enc, cb) { console.log('sink got', chunk.toString()); cb(); } });

let finished = false;
pipeline(Readable.from(['a', 'b', 'c']), buggy, sink, () => { finished = true; });

// A watchdog turns the silent stall into a report.
setTimeout(() => {
  console.log('after 50 ms: pipeline finished =', finished);
  console.log('transform writableLength:', buggy.writableLength, '| writableNeedDrain:', buggy.writableNeedDrain);
  console.log('transform readableLength:', buggy.readableLength, '| sink writableFinished:', sink.writableFinished);

  // A Readable with no consumer does not flow.
  const idle = Readable.from(['x', 'y']);
  console.log('no consumer: readableFlowing =', idle.readableFlowing);
  idle.resume();
  console.log('after resume(): readableFlowing =', idle.readableFlowing);

  // Ignoring the result of write().
  const slow = new Writable({ highWaterMark: 4, write(chunk, enc, cb) { setTimeout(cb, 5); } });
  const accepted = slow.write('12345678');
  console.log('write() returned', accepted, '| writableNeedDrain =', slow.writableNeedDrain);
  slow.once('drain', () => { console.log('drain fired, safe to write again'); process.exit(0); });
}, 50);

The first part stalls because one callback is missing. The watchdog timer keeps the process alive long enough to print the state of each stage. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): sink got a after 50 ms: pipeline finished = false transform writableLength: 2 | writableNeedDrain: false transform readableLength: 0 | sink writableFinished: false no consumer: readableFlowing = null after resume(): readableFlowing = true write() returned false | writableNeedDrain = true drain fired, safe to write again

Key points

  • Stalls produce no error, so read the stream state.
  • Missing callbacks, no consumer and ignored drain are the usual causes.
  • readableFlowing, readableLength, writableNeedDrain show where data stopped.
  • A stalled pipeline may let the process exit with code 0.

Common mistakes

  • Using pipe() without error handlers and wondering why nothing is logged.
  • Returning early from transform() without calling the callback.
  • Calling write() in a loop without checking its return value.
  • Adding a 'readable' listener and a 'data' listener together and expecting both to flow.
  • Forgetting that a hung network stream needs a timeout.

Follow-ups

  • What does the process exit code tell you when a top-level await never settles?
  • How does stream.finished help detect a stream that ended or failed?
  • How would you add a stall timeout to a streaming HTTP download?

Tips

  • Name the three usual causes first.
  • Mention the stream state properties by name.
  • Say that Node exits silently when nothing keeps the loop alive.