Skip to content

Commit 1fc6382

Browse files
ronagtargos
authored andcommitted
stream: don't emit prefinish after error or close
PR-URL: #39332 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent dfe99d2 commit 1fc6382

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

Diff for: lib/internal/streams/writable.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,9 @@ function needFinish(state) {
654654
!state.errored &&
655655
state.buffered.length === 0 &&
656656
!state.finished &&
657-
!state.writing);
657+
!state.writing &&
658+
!state.errorEmitted &&
659+
!state.closeEmitted);
658660
}
659661

660662
function callFinal(stream, state) {
@@ -685,7 +687,7 @@ function callFinal(stream, state) {
685687
then.call(
686688
result,
687689
function() {
688-
if (state.prefinished)
690+
if (state.prefinished || !needFinish(state))
689691
return;
690692
state.prefinish = true;
691693
process.nextTick(() => stream.emit('prefinish'));
@@ -735,10 +737,6 @@ function finishMaybe(stream, state, sync) {
735737

736738
function finish(stream, state) {
737739
state.pendingcb--;
738-
// TODO (ronag): Unify with needFinish.
739-
if (state.errorEmitted || state.closeEmitted)
740-
return;
741-
742740
state.finished = true;
743741

744742
const onfinishCallbacks = state[kOnFinished].splice(0);

Diff for: test/parallel/test-stream-writable-final-destroy.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
const { Writable } = require('stream');
5+
6+
{
7+
const w = new Writable({
8+
write(chunk, encoding, callback) {
9+
callback(null);
10+
},
11+
final(callback) {
12+
queueMicrotask(callback);
13+
}
14+
});
15+
w.end();
16+
w.destroy();
17+
18+
w.on('prefinish', common.mustNotCall());
19+
w.on('finish', common.mustNotCall());
20+
w.on('close', common.mustCall());
21+
}

0 commit comments

Comments
 (0)