diff options
author | Matteo Collina <hello@matteocollina.com> | 2018-04-17 17:24:22 +0200 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2018-04-20 15:12:22 +0200 |
commit | 08577906569a4c2de70ad2a861e2f8456cd8fcdd (patch) | |
tree | d90569aa4d6099df48ffe16bb22db777c8640a92 /lib/_stream_readable.js | |
parent | 700344e388b57651c190222f570d658504b8c06f (diff) | |
download | android-node-v8-08577906569a4c2de70ad2a861e2f8456cd8fcdd.tar.gz android-node-v8-08577906569a4c2de70ad2a861e2f8456cd8fcdd.tar.bz2 android-node-v8-08577906569a4c2de70ad2a861e2f8456cd8fcdd.zip |
stream: prevent 'end' to be emitted after 'error'
This PR adds _readableState.errorEmitted and add the tracking of it.
Fixes: https://github.com/nodejs/node/issues/6083
PR-URL: https://github.com/nodejs/node/pull/20104
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Diffstat (limited to 'lib/_stream_readable.js')
-rw-r--r-- | lib/_stream_readable.js | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 8073e174cc..31a8a11e4a 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -99,6 +99,9 @@ function ReadableState(options, stream, isDuplex) { this.endEmitted = false; this.reading = false; + // Flipped if an 'error' is emitted. + this.errorEmitted = false; + // a flag to be able to tell if the event 'readable'/'data' is emitted // immediately, or on a later tick. We set this to true at first, because // any actions that shouldn't happen until "later" should generally also @@ -1069,20 +1072,23 @@ function fromList(n, state) { function endReadable(stream) { var state = stream._readableState; - debug('endReadable', state.endEmitted); - if (!state.endEmitted) { + debug('endReadable', state.endEmitted, state.errorEmitted); + if (!state.endEmitted && !state.errorEmitted) { state.ended = true; process.nextTick(endReadableNT, state, stream); } } function endReadableNT(state, stream) { - debug('endReadableNT', state.endEmitted, state.length); + debug('endReadableNT', state.endEmitted, state.length, state.errorEmitted); // Check that we didn't get one last unshift. if (!state.endEmitted && state.length === 0) { - state.endEmitted = true; stream.readable = false; - stream.emit('end'); + + if (!state.errorEmitted) { + state.endEmitted = true; + stream.emit('end'); + } } } |