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: avoid premature close when will not emit close #45301

Merged
merged 2 commits into from
Nov 20, 2022
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
20 changes: 16 additions & 4 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ function eos(stream, options, callback) {
callback.call(stream);
};

const onclosed = () => {
closed = true;

const errored = isWritableErrored(stream) || isReadableErrored(stream);

if (errored && typeof errored !== 'boolean') {
return callback.call(stream, errored);
}

callback.call(stream);
};

const onrequest = () => {
stream.req.on('finish', onfinish);
};
Expand Down Expand Up @@ -186,22 +198,22 @@ function eos(stream, options, callback) {
process.nextTick(onclose);
} else if (wState?.errorEmitted || rState?.errorEmitted) {
if (!willEmitClose) {
process.nextTick(onclose);
process.nextTick(onclosed);
}
} else if (
!readable &&
(!willEmitClose || isReadable(stream)) &&
(writableFinished || isWritable(stream) === false)
) {
process.nextTick(onclose);
process.nextTick(onclosed);
} else if (
!writable &&
(!willEmitClose || isWritable(stream)) &&
(readableFinished || isReadable(stream) === false)
) {
process.nextTick(onclose);
process.nextTick(onclosed);
} else if ((rState && stream.req && stream.aborted)) {
process.nextTick(onclose);
process.nextTick(onclosed);
}

const cleanup = () => {
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-stream-finished.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,17 @@ testClosed((opts) => new Writable({ write() {}, ...opts }));
).end();
});
}

{
const stream = new Duplex({
write(chunk, enc, cb) {
setImmediate(cb);
}
});

stream.end('foo');

finished(stream, { readable: false }, common.mustCall((err) => {
assert(!err);
}));
}