-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
stream: fix multiple destroy()
calls
#29197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,15 @@ function destroy(err, cb) { | |
const r = this._readableState; | ||
const w = this._writableState; | ||
|
||
if ((w && w.destroyed) || (r && r.destroyed)) { | ||
if (typeof cb === 'function') { | ||
// TODO(ronag): Invoke with `'close'`/`'error'`. | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cb(); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section was modified and moved above |
||
if (err) { | ||
if (w) { | ||
w.errored = true; | ||
|
@@ -16,16 +25,6 @@ function destroy(err, cb) { | |
} | ||
} | ||
|
||
if ((w && w.destroyed) || (r && r.destroyed)) { | ||
if (cb) { | ||
cb(err); | ||
} else if (err) { | ||
process.nextTick(emitErrorNT, this, err); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
// We set destroyed to true before firing error callbacks in order | ||
// to make it re-entrance safe in case destroy() is called within callbacks | ||
|
||
|
@@ -53,13 +52,11 @@ function destroy(err, cb) { | |
r.closed = true; | ||
} | ||
|
||
if (cb) { | ||
// Invoke callback before scheduling emitClose so that callback | ||
// can schedule before. | ||
if (typeof cb === 'function') { | ||
cb(err); | ||
// Don't emit 'error' if passed a callback. | ||
process.nextTick(emitCloseNT, this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We always need to emit 'error'. Before the callback would call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
} else if (err) { | ||
} | ||
|
||
if (err) { | ||
process.nextTick(emitErrorCloseNT, this, err); | ||
} else { | ||
process.nextTick(emitCloseNT, this); | ||
|
@@ -138,6 +135,10 @@ function errorOrDestroy(stream, err, sync) { | |
const r = stream._readableState; | ||
const w = stream._writableState; | ||
|
||
if ((w && w.destroyed) || (r && r.destroyed)) { | ||
return this; | ||
} | ||
|
||
if ((r && r.autoDestroy) || (w && w.autoDestroy)) | ||
stream.destroy(err); | ||
else if (err) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,8 +63,7 @@ file | |
|
||
callbacks.close++; | ||
console.error('write after end should not be allowed'); | ||
file.write('should not work anymore'); | ||
file.on('error', common.expectsError({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is after |
||
file.write('should not work anymore', common.expectsError({ | ||
code: 'ERR_STREAM_WRITE_AFTER_END', | ||
name: 'Error', | ||
message: 'write after end' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,10 @@ const assert = require('assert'); | |
const r = new stream.Readable({ | ||
captureRejections: true, | ||
read() { | ||
this.push('hello'); | ||
this.push('world'); | ||
this.push(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. push(null) will cause |
||
} | ||
}); | ||
r.push('hello'); | ||
r.push('world'); | ||
|
||
const err = new Error('kaboom'); | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.