diff options
author | Robert Nagy <ronagy@icloud.com> | 2019-07-15 23:48:44 +0200 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2019-08-18 16:27:05 -0700 |
commit | 188896ea3e5145df921d9708e8546b5778cf7267 (patch) | |
tree | f48ebec48adc022f6c41213f2142e2ef4315cc6c | |
parent | 4e782c9deb03b58923f22e930ba1c5a778fbe4a5 (diff) | |
download | android-node-v8-188896ea3e5145df921d9708e8546b5778cf7267.tar.gz android-node-v8-188896ea3e5145df921d9708e8546b5778cf7267.tar.bz2 android-node-v8-188896ea3e5145df921d9708e8546b5778cf7267.zip |
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>
-rw-r--r-- | lib/_stream_writable.js | 7 | ||||
-rw-r--r-- | test/parallel/test-stream2-writable.js | 17 |
2 files changed, 21 insertions, 3 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 47a9bb208c..c6d4ec4266 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -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) { diff --git a/test/parallel/test-stream2-writable.js b/test/parallel/test-stream2-writable.js index b20f5d3f18..4027700782 100644 --- a/test/parallel/test-stream2-writable.js +++ b/test/parallel/test-stream2-writable.js @@ -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)); +} |