Skip to content

Commit 188896e

Browse files
ronagTrott
authored andcommitted
stream: do not emit after 'error'
Do not emit 'prefinish' or 'finish' event after an error. PR-URL: #28708 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 4e782c9 commit 188896e

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/_stream_writable.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,11 @@ function callFinal(stream, state) {
617617
state.pendingcb--;
618618
if (err) {
619619
errorOrDestroy(stream, err);
620+
} else {
621+
state.prefinished = true;
622+
stream.emit('prefinish');
623+
finishMaybe(stream, state);
620624
}
621-
state.prefinished = true;
622-
stream.emit('prefinish');
623-
finishMaybe(stream, state);
624625
});
625626
}
626627
function prefinish(stream, state) {

test/parallel/test-stream2-writable.js

+17
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)