diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 10f5471e21d3eb..5c352135dc3012 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -319,7 +319,7 @@ function destroyer(stream, err) { // TODO: Don't lose err? stream.close(); } else if (err) { - process.nextTick(emitErrorCloseLegacy, stream); + process.nextTick(emitErrorCloseLegacy, stream, err); } else { process.nextTick(emitCloseLegacy, stream); } diff --git a/test/parallel/test-stream-pipeline.js b/test/parallel/test-stream-pipeline.js index eecf836b5bb77f..529b18386e25a6 100644 --- a/test/parallel/test-stream-pipeline.js +++ b/test/parallel/test-stream-pipeline.js @@ -1526,3 +1526,33 @@ const tsp = require('timers/promises'); assert.strictEqual(val, null); })); } + +{ + // Mimics a legacy stream without the .destroy method + class LegacyWritable extends Stream { + write(chunk, encoding, callback) { + callback(); + } + } + + const writable = new LegacyWritable(); + writable.on('error', common.mustCall((err) => { + assert.deepStrictEqual(err, new Error('stop')); + })); + + pipeline( + Readable.from({ + [Symbol.asyncIterator]() { + return { + next() { + return Promise.reject(new Error('stop')); + } + }; + } + }), + writable, + common.mustCall((err) => { + assert.deepStrictEqual(err, new Error('stop')); + }) + ); +}