GeezerVison is showing github.com — stream: ensure writable.destroy() emits error once · nodejs/node@82a68ce
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/destroy.js +14-5Lines changed: 14 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,10 +10,15 @@ function destroy(err, cb) { | 10 | 10 | if (readableDestroyed || writableDestroyed) { | 11 | 11 | if (cb) { | 12 | 12 | cb(err); | 13 | | - } else if (err && | 14 | | - (!this._writableState || !this._writableState.errorEmitted)) { | 15 | | - process.nextTick(emitErrorNT, this, err); | | 13 | + } else if (err) { | | 14 | + if (!this._writableState) { | | 15 | + process.nextTick(emitErrorNT, this, err); | | 16 | + } else if (!this._writableState.errorEmitted) { | | 17 | + this._writableState.errorEmitted = true; | | 18 | + process.nextTick(emitErrorNT, this, err); | | 19 | + } | 16 | 20 | } | | 21 | + | 17 | 22 | return this; | 18 | 23 | } | 19 | 24 |
| @@ -31,9 +36,13 @@ function destroy(err, cb) { | 31 | 36 |
| 32 | 37 | this._destroy(err || null, (err) => { | 33 | 38 | if (!cb && err) { | 34 | | - process.nextTick(emitErrorAndCloseNT, this, err); | 35 | | - if (this._writableState) { | | 39 | + if (!this._writableState) { | | 40 | + process.nextTick(emitErrorAndCloseNT, this, err); | | 41 | + } else if (!this._writableState.errorEmitted) { | 36 | 42 | this._writableState.errorEmitted = true; | | 43 | + process.nextTick(emitErrorAndCloseNT, this, err); | | 44 | + } else { | | 45 | + process.nextTick(emitCloseNT, this); | 37 | 46 | } | 38 | 47 | } else if (cb) { | 39 | 48 | process.nextTick(emitCloseNT, this); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-writable-destroy.js +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -152,6 +152,32 @@ const assert = require('assert'); | 152 | 152 | assert.strictEqual(write.destroyed, true); | 153 | 153 | } | 154 | 154 |
| | 155 | +{ | | 156 | + const writable = new Writable({ | | 157 | + destroy: common.mustCall(function(err, cb) { | | 158 | + process.nextTick(cb, new Error('kaboom 1')); | | 159 | + }), | | 160 | + write(chunk, enc, cb) { | | 161 | + cb(); | | 162 | + } | | 163 | + }); | | 164 | + | | 165 | + writable.on('close', common.mustCall()); | | 166 | + writable.on('error', common.expectsError({ | | 167 | + type: Error, | | 168 | + message: 'kaboom 2' | | 169 | + })); | | 170 | + | | 171 | + writable.destroy(); | | 172 | + assert.strictEqual(writable.destroyed, true); | | 173 | + assert.strictEqual(writable._writableState.errorEmitted, false); | | 174 | + | | 175 | + // Test case where `writable.destroy()` is called again with an error before | | 176 | + // the `_destroy()` callback is called. | | 177 | + writable.destroy(new Error('kaboom 2')); | | 178 | + assert.strictEqual(writable._writableState.errorEmitted, true); | | 179 | +} | | 180 | + | 155 | 181 | { | 156 | 182 | const write = new Writable({ | 157 | 183 | write(chunk, enc, cb) { cb(); } | |
|