Repository navigation
process.stdout/stderr with no FileSink: write synchronously, reset after a failed write #44954
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
Open
robobun
wants to merge
3
commits into
main
Choose a base branch
from
robobun/466087e8/stdio-without-filesink
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
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.
🔴 Users whose stdout resets after a failed write that left more writes buffered get an end(cb) that never calls back; on the base cb got the EPIPE. The reset at src/js/builtins/ProcessObjectInternals.ts:112 keeps the old pendingcb: errorBuffer (src/js/internal/streams/writable.ts:670-687) fails each buffered write's callback without decrementing it, and nothing later does, so finishMaybe (writable.ts:882) never reaches finish. 'finish' and finished() never fire either. Fix: after the reset pendingcb must count only writes still outstanding, e.g. decrement it in errorBuffer per flushed write; add a test that ends the stream after the failed-writes scenario. This differs from the posted errorEmitted finding: its fix (bypassing cb) is in and does not touch pendingcb.
Why this was flagged
Trigger: process.stdout or process.stderr with no FileSink on a pipe or socket, the reader gone, and two or more write() calls in one synchronous run. The first write fails synchronously in underscoreWriteSync and onwrite sets kErrored, so every later write in that tick is buffered by writeOrBuffer (writable.ts:503-509) after _write did state.pendingcb++ (writable.ts:453). Next tick onwriteError decrements pendingcb once for the failed write, then errorBuffer (writable.ts:670-687) invokes each buffered callback with the EPIPE and resets the buffer without touching pendingcb. errorOrDestroy then destroys, the wrapper at ProcessObjectInternals.ts:108-118 calls _undestroy(), which resets ended, errored and closed but not pendingcb. A later end(cb) sets kEnding, finishMaybe skips finish because state.pendingcb === 0 is false (writable.ts:882); no later path decrements pendingcb, so 'finish' never emits, cb never runs, and finished() never settles. On the base branch the stream stays errored, so end(cb) delivers cb(state.errored) next tick (writable.ts:806-807). None of the new tests combine buffered failed writes with end().
Verification: Trigger: a no-FileSink process.stdout/stderr on a pipe/socket whose reader is gone, two or more write() calls before the reset lands, then end(cb). errorBuffer (writable.ts:670-687) never touches pendingcb; undestroy (destroy.ts:171-183) does not reset pendingcb, so finishMaybe's if (state.pendingcb === 0) (line 882) is false. Base: end(cb) runs process.nextTick(cb, state[kErroredValue]) (writable.ts:806-807).