Skip to content

Commit

Permalink
stream: callback should be called when pendingcb is 0
Browse files Browse the repository at this point in the history
Fixes: #46170
PR-URL: #53438
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
jakecastelli authored and targos committed Jun 20, 2024
1 parent 4b47f89 commit b3372a8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ function eos(stream, options, callback) {
} else if (
!readable &&
(!willEmitClose || isReadable(stream)) &&
(writableFinished || isWritable(stream) === false)
(writableFinished || isWritable(stream) === false) &&
(wState == null || wState.pendingcb === 0)
) {
process.nextTick(onclosed);
} else if (
Expand Down
8 changes: 7 additions & 1 deletion test/parallel/test-stream-finished.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,15 +669,21 @@ testClosed((opts) => new Writable({ write() {}, ...opts }));
}

{
let isCalled = false;
const stream = new Duplex({
write(chunk, enc, cb) {
setImmediate(cb);
setImmediate(() => {
isCalled = true;
cb();
});
}
});

stream.end('foo');

finished(stream, { readable: false }, common.mustCall((err) => {
assert(!err);
assert.strictEqual(isCalled, true);
assert.strictEqual(stream._writableState.pendingcb, 0);
}));
}

0 comments on commit b3372a8

Please sign in to comment.