Skip to content

Commit

Permalink
stream: don't emit prefinish after error or close
Browse files Browse the repository at this point in the history
PR-URL: #39332
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
ronag committed Jul 11, 2021
1 parent 5960f16 commit 7a7ba82
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,9 @@ function needFinish(state) {
!state.errored &&
state.buffered.length === 0 &&
!state.finished &&
!state.writing);
!state.writing &&
!state.errorEmitted &&
!state.closeEmitted);
}

function callFinal(stream, state) {
Expand Down Expand Up @@ -685,7 +687,7 @@ function callFinal(stream, state) {
then.call(
result,
function() {
if (state.prefinished)
if (state.prefinished || !needFinish(state))
return;
state.prefinish = true;
process.nextTick(() => stream.emit('prefinish'));
Expand Down Expand Up @@ -735,10 +737,6 @@ function finishMaybe(stream, state, sync) {

function finish(stream, state) {
state.pendingcb--;
// TODO (ronag): Unify with needFinish.
if (state.errorEmitted || state.closeEmitted)
return;

state.finished = true;

const onfinishCallbacks = state[kOnFinished].splice(0);
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-stream-writable-final-destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
const common = require('../common');

const { Writable } = require('stream');

{
const w = new Writable({
write(chunk, encoding, callback) {
callback(null);
},
final(callback) {
queueMicrotask(callback);
}
});
w.end();
w.destroy();

w.on('prefinish', common.mustNotCall());
w.on('finish', common.mustNotCall());
w.on('close', common.mustCall());
}

0 comments on commit 7a7ba82

Please sign in to comment.