Skip to content

Commit

Permalink
stream: wait for close before calling the callback
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jakecastelli committed Jun 15, 2024
1 parent 177d63f commit de856a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ function pipelineImpl(streams, callback, opts) {
if (
err &&
err.name !== 'AbortError' &&
err.code !== 'ERR_STREAM_PREMATURE_CLOSE'
err.code !== 'ERR_STREAM_PREMATURE_CLOSE' &&
// It is 1 not 0 as finishCount will be decremented in finish
finishCount === 1
) {
finish(err);
}
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-stream-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

}

0 comments on commit de856a4

Please sign in to comment.