diff options
author | Luigi Pinca <luigipinca@gmail.com> | 2019-02-12 19:24:39 +0100 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2019-03-05 23:52:36 +0100 |
commit | 49f1bb9b9366c928b82046695c288699fce012ad (patch) | |
tree | 73aed66929d4f7628a9391f211c19db05a99d5d8 /lib/internal/streams | |
parent | 60aaf2c2144d68eae063e09b5216338341d83633 (diff) | |
download | android-node-v8-49f1bb9b9366c928b82046695c288699fce012ad.tar.gz android-node-v8-49f1bb9b9366c928b82046695c288699fce012ad.tar.bz2 android-node-v8-49f1bb9b9366c928b82046695c288699fce012ad.zip |
stream: ensure writable.destroy() emits error once
Prevent the `'error'` event from being emitted multiple times if
`writable.destroy()` is called with an error before the `_destroy()`
callback is called.
Emit the first error, discard all others.
PR-URL: https://github.com/nodejs/node/pull/26057
Fixes: https://github.com/nodejs/node/issues/26015
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/internal/streams')
-rw-r--r-- | lib/internal/streams/destroy.js | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 0c652be9dd..200c75459a 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -10,10 +10,15 @@ function destroy(err, cb) { if (readableDestroyed || writableDestroyed) { if (cb) { cb(err); - } else if (err && - (!this._writableState || !this._writableState.errorEmitted)) { - process.nextTick(emitErrorNT, this, err); + } else if (err) { + if (!this._writableState) { + process.nextTick(emitErrorNT, this, err); + } else if (!this._writableState.errorEmitted) { + this._writableState.errorEmitted = true; + process.nextTick(emitErrorNT, this, err); + } } + return this; } @@ -31,9 +36,13 @@ function destroy(err, cb) { this._destroy(err || null, (err) => { if (!cb && err) { - process.nextTick(emitErrorAndCloseNT, this, err); - if (this._writableState) { + if (!this._writableState) { + process.nextTick(emitErrorAndCloseNT, this, err); + } else if (!this._writableState.errorEmitted) { this._writableState.errorEmitted = true; + process.nextTick(emitErrorAndCloseNT, this, err); + } else { + process.nextTick(emitCloseNT, this); } } else if (cb) { process.nextTick(emitCloseNT, this); |