Commit 88c3dbedb63 for nodejs
commit 88c3dbedb633b4fd1a3b19e8189e5a7687700ef5
Author: Matteo Collina <hello@matteocollina.com>
Date: Wed Nov 25 06:59:14 2026 +0100
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 <matteo.collina@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67674
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
diff --git a/lib/internal/streams/compose.js b/lib/internal/streams/compose.js
index b36cfe73624..54b33702ce9 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 00000000000..34efd543de6
--- /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());
+}