Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: callback should be called when pendingcb is 0 #53438

Merged
merged 5 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test that does not look into _writableState? Why are you doing this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I will add a new test to address it, thanks!

Why are you doing this change?

Do you mean the change to the test? If so, it is because when the callback function is called in stream.finished it won't wait for the callback in the writable._write (if any async action is performed) and one of the indication is stream._writableState.pendingcb is not 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have expanded the test to make sure the cb in _write is called before the cb in finished is called. PTAL @mcollina 🙏

}));
}