-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Added notes on special .write callback behaviour on process.stdout/.stderr #3772
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
minesworld
wants to merge
2
commits into
nodejs:master
from
minesworld:feature/process.io-woews-documentation
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -286,6 +286,10 @@ event and that writes can block when output is redirected to a file (although | |
disks are fast and operating systems normally employ write-back caching so it | ||
should be a very rare occurrence indeed.) | ||
|
||
Note that on `process.stdout` and `process.stderr`, the callback passed to | ||
`stream.write` might be called before all written data is flushed completely. | ||
This can result in data loss if Node.js is ended prematurely using `process.exit`. | ||
|
||
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. Hmm... perhaps, "Note that on |
||
To check if Node.js is being run in a TTY context, read the `isTTY` property | ||
on `process.stderr`, `process.stdout`, or `process.stdin`: | ||
|
||
|
@@ -460,6 +464,10 @@ To exit with a 'failure' code: | |
|
||
The shell that executed Node.js should see the exit code as 1. | ||
|
||
Note that Node.js will shutdown as fast as possible. Consumers of `process.stdout` | ||
and `process.stderr` might not get all data even when the `stream.write` callback | ||
suggests different. | ||
|
||
|
||
## process.exitCode | ||
|
||
|
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.
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.
This is incorrect. What you should convey here is that the callback can fire before the receiving end has received all data because it's still in transit in a kernel buffer.
The other caveat is that calling
process.exit
before callbacks have fired means the data may not have been handed off to the operating system yet. The imperative word here is 'may' - it may or may not have been handed off. It's not until the callback fires that you know for sure.In case it isn't clear, the flow is
node -> kernel -> other
, whereother
is either a tty, a file or another process reading from a pipe. Callingprocess.exit
when pending data is still in node land means the data is irretrievably lost. When it's been handed off to the kernel, it's unspecified what happens - it depends on the operating system and the phase of the moon.