stream: do not emit after 'error'

Do not emit 'prefinish' or 'finish' event after an error.

PR-URL: https://github.com/nodejs/node/pull/28708
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Robert Nagy 2019-07-15 23:48:44 +02:00 committed by Rich Trott
parent 4e782c9deb
commit 188896ea3e
2 changed files with 21 additions and 3 deletions

View File

@ -617,10 +617,11 @@ function callFinal(stream, state) {
state.pendingcb--; state.pendingcb--;
if (err) { if (err) {
errorOrDestroy(stream, err); errorOrDestroy(stream, err);
} else {
state.prefinished = true;
stream.emit('prefinish');
finishMaybe(stream, state);
} }
state.prefinished = true;
stream.emit('prefinish');
finishMaybe(stream, state);
}); });
} }
function prefinish(stream, state) { function prefinish(stream, state) {

View File

@ -441,3 +441,20 @@ const helloWorldBuffer = Buffer.from('hello world');
w.write('hello'); w.write('hello');
w.destroy(new Error()); w.destroy(new Error());
} }
{
// Verify that finish is not emitted after error
const w = new W();
w._final = common.mustCall(function(cb) {
cb(new Error());
});
w._write = function(chunk, e, cb) {
process.nextTick(cb);
};
w.on('error', common.mustCall());
w.on('prefinish', common.mustNotCall());
w.on('finish', common.mustNotCall());
w.write(Buffer.allocUnsafe(1));
w.end(Buffer.allocUnsafe(0));
}