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 targos committed Jun 5, 2021
1 parent 8529864 commit c101e37
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
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
22 changes: 22 additions & 0 deletions test/parallel/test-stream-finished.js
Expand Up @@ -473,3 +473,25 @@ testClosed((opts) => new Writable({ write() {}, ...opts }));
finished(p, common.mustNotCall());
}));
}

{
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 c101e37

Please sign in to comment.