diff --git a/lib/internal/streams/pipeline.js b/lib/internal/streams/pipeline.js index bb34759b1fea12..9e05e5af891d56 100644 --- a/lib/internal/streams/pipeline.js +++ b/lib/internal/streams/pipeline.js @@ -278,6 +278,8 @@ function pipelineImpl(streams, callback, opts) { err && err.name !== 'AbortError' && err.code !== 'ERR_STREAM_PREMATURE_CLOSE' + // It is 1 not 0 as finishCount will be decremented in finish + && finishCount === 1 ) { finish(err); } diff --git a/test/parallel/test-stream-pipeline.js b/test/parallel/test-stream-pipeline.js index 7e69754b36d771..74931e8a248708 100644 --- a/test/parallel/test-stream-pipeline.js +++ b/test/parallel/test-stream-pipeline.js @@ -1678,4 +1678,21 @@ tmpdir.refresh(); assert.strictEqual(err, undefined); })); + { + // See https://github.com/nodejs/node/issues/51540 + const src = new Readable(); + const dst = new Writable({ + destroy(error, cb) { + // Takes a while to destroy + setImmediate(cb); + }, + }); + + pipeline(src, dst, (err) => { + assert.strictEqual(dst.closed, true) + assert.strictEqual(err.message, 'problem') + }); + src.destroy(new Error('problem')); + } + }