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: nodejs#46170
PR-URL: nodejs#53438
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
jakecastelli authored and sophoniie committed Jun 20, 2024
1 parent d9e8ceb commit 4b08951
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 4b08951

Please sign in to comment.