Skip to content

Commit

Permalink
stream: do not emit after 'error'
Browse files Browse the repository at this point in the history
Do not emit 'prefinish' or 'finish' event after an error.

PR-URL: #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>
  • Loading branch information
ronag authored and Trott committed Aug 18, 2019
1 parent 4e782c9 commit 188896e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,10 +617,11 @@ function callFinal(stream, state) {
state.pendingcb--;
if (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) {
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-stream2-writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,20 @@ const helloWorldBuffer = Buffer.from('hello world');
w.write('hello');
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));
}

0 comments on commit 188896e

Please sign in to comment.