Skip to content

Commit de856a4

Browse files
committed
stream: wait for close before calling the callback
The pipeline should wait for close event to finish before calling the callback. The `finishCount` should not below 0 when calling finish function. Fixes: #51540
1 parent 177d63f commit de856a4

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/internal/streams/pipeline.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ function pipelineImpl(streams, callback, opts) {
277277
if (
278278
err &&
279279
err.name !== 'AbortError' &&
280-
err.code !== 'ERR_STREAM_PREMATURE_CLOSE'
280+
err.code !== 'ERR_STREAM_PREMATURE_CLOSE' &&
281+
// It is 1 not 0 as finishCount will be decremented in finish
282+
finishCount === 1
281283
) {
282284
finish(err);
283285
}

test/parallel/test-stream-pipeline.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,4 +1678,21 @@ tmpdir.refresh();
16781678
assert.strictEqual(err, undefined);
16791679
}));
16801680

1681+
{
1682+
// See https://github.com/nodejs/node/issues/51540
1683+
const src = new Readable();
1684+
const dst = new Writable({
1685+
destroy(error, cb) {
1686+
// Takes a while to destroy
1687+
setImmediate(cb);
1688+
},
1689+
});
1690+
1691+
pipeline(src, dst, (err) => {
1692+
assert.strictEqual(dst.closed, true);
1693+
assert.strictEqual(err.message, 'problem');
1694+
});
1695+
src.destroy(new Error('problem'));
1696+
}
1697+
16811698
}

0 commit comments

Comments
 (0)