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: only use legacy close listeners if not willEmitClose #36649

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/internal/streams/end-of-stream.js
Expand Up @@ -129,7 +129,9 @@ function eos(stream, options, callback) {

if (isRequest(stream)) {
stream.on('complete', onfinish);
stream.on('abort', onclose);
if (!willEmitClose) {
stream.on('abort', onclose);
}
if (stream.req) onrequest();
else stream.on('request', onrequest);
} else if (writable && !wState) { // legacy streams
Expand All @@ -138,7 +140,7 @@ function eos(stream, options, callback) {
}

// Not all streams will emit 'close' after 'aborted'.
if (typeof stream.aborted === 'boolean') {
if (!willEmitClose && typeof stream.aborted === 'boolean') {
stream.on('aborted', onclose);
}

Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-stream-finished.js
Expand Up @@ -492,3 +492,26 @@ testClosed((opts) => new Writable({ write() {}, ...opts }));
.on('response', common.mustCall());
});
}


{
const w = new Writable({
write(chunk, encoding, callback) {
process.nextTick(callback);
}
});
w.aborted = false;
w.end();
let closed = false;
w.on('finish', () => {
assert.strictEqual(closed, false);
w.emit('aborted');
});
Comment on lines +506 to +509
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
w.on('finish', () => {
assert.strictEqual(closed, false);
w.emit('aborted');
});
w.on('finish', common.mustCall(() => {
assert.strictEqual(closed, false);
w.emit('aborted');
}));

w.on('close', common.mustCall(() => {
closed = true;
}));

finished(w, common.mustCall(() => {
assert.strictEqual(closed, true);
}));
}