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 13d24b0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 282 in lib/internal/streams/pipeline.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'&&' should be placed at the end of the line

Check failure on line 282 in lib/internal/streams/pipeline.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Trailing spaces not allowed
) {
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)

Check failure on line 1692 in test/parallel/test-stream-pipeline.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
assert.strictEqual(err.message, 'problem')

Check failure on line 1693 in test/parallel/test-stream-pipeline.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
});
src.destroy(new Error('problem'));
}

}

0 comments on commit 13d24b0

Please sign in to comment.