-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
stream: fix _final and 'prefinish' timing #32780
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
This PR fixes a few different things: The timing of 'prefinish' depends on whether or not _final is defined. In on case the event is emitted synchronously with end() and otherwise asynchronously. _final is currently unecessarily called asynchronously which forces implementors to use 'prefinish' as a hack to emulate synchronous behaviour. Furthermore, this hack is subtly broken due to the above issue. The stream should not finish if errored or destroyed synchronously during the prefinish stage. Refs: #31401 Refs: #32763 (comment)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -629,24 +629,33 @@ function needFinish(state) { | |
} | ||
|
||
function callFinal(stream, state) { | ||
state.sync = true; | ||
state.pendingcb++; | ||
stream._final((err) => { | ||
state.pendingcb--; | ||
if (err) { | ||
errorOrDestroy(stream, err); | ||
errorOrDestroy(stream, err, state.sync); | ||
} else { | ||
// Backwards compat. Don't check needFinish() here. | ||
// Some streams assume 'finish' will be emitted | ||
// even if stream has been destroyed. | ||
state.prefinished = true; | ||
stream.emit('prefinish'); | ||
finishMaybe(stream, state); | ||
// Backwards compat. Don't check state.sync here. | ||
// Some stream assume 'finish' will be emitted | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// asynchronously relative to _final callback. | ||
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. Might be worth to revisit once 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 to support the following commonly used pattern: _final(cb) (
if (!this.ready) {
this.once('ready', () => this._final(cb)) // Unfortunately not wrapped in a nextTick
}
// cleanup...
cb();
} |
||
state.pendingcb++; | ||
process.nextTick(finish, stream, state); | ||
} | ||
}); | ||
state.sync = false; | ||
} | ||
|
||
function prefinish(stream, state) { | ||
if (!state.prefinished && !state.finalCalled) { | ||
if (typeof stream._final === 'function' && !state.destroyed) { | ||
state.pendingcb++; | ||
state.finalCalled = true; | ||
process.nextTick(callFinal, stream, state); | ||
callFinal(stream, state); | ||
} else { | ||
state.prefinished = true; | ||
stream.emit('prefinish'); | ||
|
@@ -655,10 +664,9 @@ function prefinish(stream, state) { | |
} | ||
|
||
function finishMaybe(stream, state, sync) { | ||
const need = needFinish(state); | ||
if (need) { | ||
if (needFinish(state)) { | ||
prefinish(stream, state); | ||
if (state.pendingcb === 0) { | ||
if (state.pendingcb === 0 && needFinish(state)) { | ||
state.pendingcb++; | ||
if (sync) { | ||
process.nextTick(finish, stream, state); | ||
|
@@ -667,7 +675,6 @@ function finishMaybe(stream, state, sync) { | |
} | ||
} | ||
} | ||
return need; | ||
} | ||
|
||
function finish(stream, state) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1710,11 +1710,14 @@ function streamOnPause() { | |
} | ||
|
||
function afterShutdown(status) { | ||
const stream = this.handle[kOwner]; | ||
if (stream) { | ||
stream.on('finish', () => { | ||
stream[kMaybeDestroy](); | ||
}); | ||
} | ||
// Currently this status value is unused | ||
this.callback(); | ||
const stream = this.handle[kOwner]; | ||
if (stream) | ||
stream[kMaybeDestroy](); | ||
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 assumes |
||
} | ||
|
||
function finishSendTrailers(stream, headersList) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to add a check for
errored
here in a follow up PR...