From 94f8a2ab83b6c8581a2bdf279bcef3b0dfb0115e Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sat, 21 Nov 2026 08:26:14 +0000 Subject: [PATCH] stream: forward compose tail error after finish compose() only listened for 'data' and 'end' on the last stream it was given. When that stream emitted 'error' after its writable side had already finished, pipeline() had already removed its own error listener, so the error reached no listener and was raised as an unhandled 'error' event, crashing the process. Forward tail errors to the composed Duplex by destroying it whenever the tail errors before the composition has been destroyed. Signed-off-by: Matteo Collina --- lib/internal/streams/compose.js | 6 +++ ...-stream-compose-tail-error-after-finish.js | 54 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 test/parallel/test-stream-compose-tail-error-after-finish.js diff --git a/lib/internal/streams/compose.js b/lib/internal/streams/compose.js index b36cfe736245..54b33702ce94 100644 --- a/lib/internal/streams/compose.js +++ b/lib/internal/streams/compose.js @@ -196,6 +196,12 @@ module.exports = function compose(...streams) { tail.on('end', function() { d.push(null); }); + + tail.on('error', function(err) { + if (!d.destroyed) { + d.destroy(err); + } + }); } else if (isWebStream(tail)) { const readable = isTransformStream(tail) ? tail.readable : tail; const reader = readable.getReader(); diff --git a/test/parallel/test-stream-compose-tail-error-after-finish.js b/test/parallel/test-stream-compose-tail-error-after-finish.js new file mode 100644 index 000000000000..34efd543de6d --- /dev/null +++ b/test/parallel/test-stream-compose-tail-error-after-finish.js @@ -0,0 +1,54 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { + compose, + Duplex, + Readable, +} = require('stream'); + +// Returns a Duplex whose writable side finishes (emitting 'finish') but whose +// readable side then errors instead of ending. This mimics a zlib decompressor +// that only detects truncated input during its final flush, which runs after the +// writable side has already finished, so 'end' is never emitted. +function createTail() { + return new Duplex({ + write(chunk, encoding, callback) { + callback(); + }, + final(callback) { + callback(); + setImmediate(() => this.destroy(new Error('readable-side failure after finish'))); + }, + read() {}, + }); +} + +// Regression test: an error emitted by the last stream of compose() after its +// writable side has finished must be forwarded to the composed Duplex, rather +// than becoming an unhandled 'error' event on the tail. +{ + const composed = compose(Readable.from([Buffer.from('x')]), createTail()); + + composed.on('error', common.mustCall((err) => { + assert.strictEqual(err.message, 'readable-side failure after finish'); + })); + composed.resume(); +} + +// The same error must reject a `for await` consumer of the composed stream. +{ + const composed = compose(Readable.from([Buffer.from('x')]), createTail()); + + (async () => { + try { + for await (const chunk of composed) { + assert.ok(chunk); + } + assert.fail('should have thrown'); + } catch (err) { + assert.strictEqual(err.message, 'readable-side failure after finish'); + } + })().then(common.mustCall()); +}