Skip to content

Commit 13d24b0

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 13d24b0

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/internal/streams/pipeline.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ function pipelineImpl(streams, callback, opts) {
278278
err &&
279279
err.name !== 'AbortError' &&
280280
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)