Skip to content

Commit

Permalink
stream: only use legacy close listeners if not willEmitClose
Browse files Browse the repository at this point in the history
Some streams that willEmitClose unecessarily fallback to legacy
events.

PR-URL: #36649
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
ronag authored and danielleadams committed Jan 12, 2021
1 parent 651e7d2 commit 190ddce
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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');
});
w.on('close', common.mustCall(() => {
closed = true;
}));

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

0 comments on commit 190ddce

Please sign in to comment.