-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
stream: fix writable.end callback behavior #34101
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,8 @@ ObjectSetPrototypeOf(Writable, Stream); | |
|
||
function nop() {} | ||
|
||
const kOnFinished = Symbol('kOnFinished'); | ||
|
||
function WritableState(options, stream, isDuplex) { | ||
// Duplex streams are both readable and writable, but share | ||
// the same options object. | ||
|
@@ -185,6 +187,8 @@ function WritableState(options, stream, isDuplex) { | |
// True if close has been emitted or would have been emitted | ||
// depending on emitClose. | ||
this.closeEmitted = false; | ||
|
||
this[kOnFinished] = []; | ||
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. Possible optimization to start this as |
||
} | ||
|
||
function resetBuffer(state) { | ||
|
@@ -411,7 +415,7 @@ function onwriteError(stream, state, er, cb) { | |
// not enabled. Passing `er` here doesn't make sense since | ||
// it's related to one specific write, not to the buffered | ||
// writes. | ||
errorBuffer(state, new ERR_STREAM_DESTROYED('write')); | ||
errorBuffer(state); | ||
// This can emit error, but error must always follow cb. | ||
errorOrDestroy(stream, er); | ||
} | ||
|
@@ -487,14 +491,14 @@ function afterWrite(stream, state, count, cb) { | |
} | ||
|
||
if (state.destroyed) { | ||
errorBuffer(state, new ERR_STREAM_DESTROYED('write')); | ||
errorBuffer(state); | ||
} | ||
|
||
finishMaybe(stream, state); | ||
} | ||
|
||
// If there's something in the buffer waiting, then invoke callbacks. | ||
function errorBuffer(state, err) { | ||
function errorBuffer(state) { | ||
if (state.writing) { | ||
return; | ||
} | ||
|
@@ -503,7 +507,11 @@ function errorBuffer(state, err) { | |
const { chunk, callback } = state.buffered[n]; | ||
const len = state.objectMode ? 1 : chunk.length; | ||
state.length -= len; | ||
callback(err); | ||
callback(new ERR_STREAM_DESTROYED('write')); | ||
} | ||
|
||
for (const callback of state[kOnFinished].splice(0)) { | ||
callback(new ERR_STREAM_DESTROYED('end')); | ||
} | ||
|
||
resetBuffer(state); | ||
|
@@ -611,10 +619,11 @@ Writable.prototype.end = function(chunk, encoding, cb) { | |
} | ||
|
||
if (typeof cb === 'function') { | ||
if (err || state.finished) | ||
if (err || state.finished) { | ||
process.nextTick(cb, err); | ||
else | ||
onFinished(this, cb); | ||
} else { | ||
state[kOnFinished].push(cb); | ||
} | ||
} | ||
|
||
return this; | ||
|
@@ -636,6 +645,9 @@ function callFinal(stream, state) { | |
stream._final((err) => { | ||
state.pendingcb--; | ||
if (err) { | ||
for (const callback of state[kOnFinished].splice(0)) { | ||
callback(err); | ||
} | ||
errorOrDestroy(stream, err, state.sync); | ||
} else if (needFinish(state)) { | ||
state.prefinished = true; | ||
|
@@ -683,6 +695,11 @@ function finish(stream, state) { | |
return; | ||
|
||
state.finished = true; | ||
|
||
for (const callback of state[kOnFinished].splice(0)) { | ||
callback(); | ||
} | ||
|
||
stream.emit('finish'); | ||
|
||
if (state.autoDestroy) { | ||
|
@@ -701,26 +718,6 @@ function finish(stream, state) { | |
} | ||
} | ||
|
||
// TODO(ronag): Avoid using events to implement internal logic. | ||
function onFinished(stream, cb) { | ||
function onerror(err) { | ||
stream.removeListener('finish', onfinish); | ||
stream.removeListener('error', onerror); | ||
cb(err); | ||
if (stream.listenerCount('error') === 0) { | ||
stream.emit('error', err); | ||
} | ||
} | ||
|
||
function onfinish() { | ||
stream.removeListener('finish', onfinish); | ||
stream.removeListener('error', onerror); | ||
cb(); | ||
} | ||
stream.on('finish', onfinish); | ||
stream.prependListener('error', onerror); | ||
} | ||
|
||
ObjectDefineProperties(Writable.prototype, { | ||
|
||
destroyed: { | ||
|
@@ -800,7 +797,7 @@ const destroy = destroyImpl.destroy; | |
Writable.prototype.destroy = function(err, cb) { | ||
const state = this._writableState; | ||
if (!state.destroyed) { | ||
process.nextTick(errorBuffer, state, new ERR_STREAM_DESTROYED('write')); | ||
process.nextTick(errorBuffer, state); | ||
} | ||
destroy.call(this, err, cb); | ||
return this; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.