Skip to content

Commit c5cffa8

Browse files
committed
stream: don't emit after 'error'
1 parent 5e3b4d6 commit c5cffa8

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/_stream_writable.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,11 @@ function callFinal(stream, state) {
607607
state.pendingcb--;
608608
if (err) {
609609
errorOrDestroy(stream, err);
610+
} else {
611+
state.prefinished = true;
612+
stream.emit('prefinish');
613+
finishMaybe(stream, state);
610614
}
611-
state.prefinished = true;
612-
stream.emit('prefinish');
613-
finishMaybe(stream, state);
614615
});
615616
}
616617
function prefinish(stream, state) {

test/parallel/test-stream2-writable.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,20 @@ const helloWorldBuffer = Buffer.from('hello world');
441441
w.write('hello');
442442
w.destroy(new Error());
443443
}
444+
445+
{
446+
// Verify that finish is not emitted after error
447+
const w = new W();
448+
449+
w._final = common.mustCall(function(cb) {
450+
cb(new Error());
451+
});
452+
w._write = function(chunk, e, cb) {
453+
process.nextTick(cb);
454+
};
455+
w.on('error', common.mustCall());
456+
w.on('prefinish', common.mustNotCall());
457+
w.on('finish', common.mustNotCall());
458+
w.write(Buffer.allocUnsafe(1));
459+
w.end(Buffer.allocUnsafe(0));
460+
}

0 commit comments

Comments
 (0)