stream: pipeline don't destroy Duplex src before 'finish'#32966
Closed
ronag wants to merge 2 commits intonodejs:masterfrom
Closed
stream: pipeline don't destroy Duplex src before 'finish'#32966ronag wants to merge 2 commits intonodejs:masterfrom
ronag wants to merge 2 commits intonodejs:masterfrom
Conversation
fcb17b5 to
229f5d2
Compare
pipeline was too agressive with destroying Duplex streams which were the first argument into pipeline. Just because it's !writable does not mean that it is safe to be destroyed, unless it has also emitted 'finish'. Fixes: nodejs#32955
BridgeAR
reviewed
Apr 21, 2020
lib/internal/streams/pipeline.js
Outdated
Comment on lines
53
to
73
| const wState = stream._writableState; | ||
|
|
||
| const writableEnded = stream.writableEnded || | ||
| (wState && wState.ended); | ||
| const writableFinished = stream.writableFinished || | ||
| (wState && wState.finished); | ||
|
|
||
| const willFinish = stream.writable || | ||
| (writableEnded && !writableFinished); | ||
| const willEnd = stream.readable; | ||
|
|
||
| if (err || !final || !stream.readable) { | ||
| destroyImpl.destroyer(stream, err); | ||
| if (!err) { | ||
| // First | ||
| if (reading && !writing && willFinish) { | ||
| return callback(); | ||
| } | ||
|
|
||
| // Last | ||
| if (!reading && writing && willEnd) { | ||
| return callback(); | ||
| } |
Member
There was a problem hiding this comment.
Nit: it is possible to reduce the code branches a bit:
Suggested change
| const wState = stream._writableState; | |
| const writableEnded = stream.writableEnded || | |
| (wState && wState.ended); | |
| const writableFinished = stream.writableFinished || | |
| (wState && wState.finished); | |
| const willFinish = stream.writable || | |
| (writableEnded && !writableFinished); | |
| const willEnd = stream.readable; | |
| if (err || !final || !stream.readable) { | |
| destroyImpl.destroyer(stream, err); | |
| if (!err) { | |
| // First | |
| if (reading && !writing && willFinish) { | |
| return callback(); | |
| } | |
| // Last | |
| if (!reading && writing && willEnd) { | |
| return callback(); | |
| } | |
| if (!err) { | |
| // Check if the last stream will end: | |
| if (!reading) { | |
| if (writing && stream.readable) { | |
| return callback(); | |
| } | |
| // First stream | |
| } else if (!writing) { | |
| if (stream.writable) { | |
| return callback(); | |
| } | |
| const wState = stream._writableState; | |
| const writableEnded = stream.writableEnded || | |
| (wState && wState.ended); | |
| const writableFinished = stream.writableFinished || | |
| (wState && wState.finished); | |
| if (writableEnded && !writableFinished) | |
| return callback(); | |
| } | |
| } |
Member
Author
There was a problem hiding this comment.
@BridgeAR: Any specific reason? This does make it a bit harder to read in my opinon.
Member
There was a problem hiding this comment.
Code should IMO always only run when necessary. Right now some code is executed that is only needed in some situations.
Member
Author
There was a problem hiding this comment.
This is not a hot path so readability should IMO come first. I think I can make a compromise 😄. PTAL.
4 tasks
ronag
commented
Apr 21, 2020
| destroyImpl.destroyer(stream, err); | ||
| if (!err) { | ||
| // First | ||
| if (reading && !writing && willFinish) { |
Member
Author
There was a problem hiding this comment.
The willFinish part here is the significant change. Before we only checked stream.writable.
The rest of the changes should be logically the same.
Member
Author
|
blocked pending #32954 |
Member
Author
|
closed in favor of #32968 |
This was referenced Aug 31, 2021
This was referenced Sep 12, 2021
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
pipeline was too agressive with destroying Duplex
streams which were the first argument into pipeline.
Just because it's !writable does not mean that it
is safe to be destroyed, unless it has also emitted
'finish'.
Fixes: #32955
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passes