Skip to content
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

console,process: refactoring stdout/stderr error handling #6174

Closed
wants to merge 4 commits into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Apr 13, 2016

Checklist
  • tests and code linting passes
  • a test and/or benchmark is included
  • documentation is changed or added
  • the commit message follows commit guidelines
Affected core subsystem(s)

console, process

Description of change

This is a largely speculative refactoring of error handling around process.stdout, process.stderr, and console in an attempt to address the long standing EPIPE error issues (see #831 and https://github.com/cjihrig/node-1/blob/10bc673123f29692b7dae5e306a155ed0210e413/test/known_issues/test-process-external-stdio-close.js and #947).

Within the stdio setup in internal/process/stdio.js, an error handler is set on the default stdout and stderr streams. If any error occurs, the stdout or stderr is replaced with a null stream (equivalent to /dev/null) and the error is forwarded to a new 'stdio-error' event on process which is passed the err and the fd.

The reason for this is straightforward: when process.stdout and process.stderr are Pipes, the EPIPE error is the only way we can detect if the other end of pipe has been closed (see the test-process-external-stdio-close.js test case for an existing example of this). This means that calling console.log() in a child process, for instance, can be a bit problematic (per issue #831).

The effect of this change is that calls to console.log() won't throw or result in a process.on('error') if the stdout pipe happens to be shut down on the other end. The output simply goes to the null stream.

This is a work in progress currently and is intended for discussion and review.

It also includes some refactoring to simplify and modernize some of the code bits. Those are mostly isolated in their own commits.

/cc @vkurchatkin @bnoordhuis @trevnorris

On an error in stdout/stderr stream, redirect the error to a new
`'stdio-error'` event and set the stdout/stderr to a null stream.

Additional refactoring the stdio.js while in the code.
@jasnell jasnell added wip Issues and PRs that are still a work in progress. semver-major PRs that contain breaking changes and should be released in the next major version. process Issues and PRs related to the process subsystem. console Issues and PRs related to the console subsystem. labels Apr 13, 2016
Console.prototype.log = function() {
this._stdout.write(util.format.apply(null, arguments) + '\n');
Console.prototype.log = function(...args) {
this._stdout.write(`${util.format(...args)}\n`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't ...args still slightly slower than .apply?

Not that it would probably be measurable given log.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW v8 5.x supposedly has improved this significantly (according to the v8 blog), but we will have to test and see how it compares there.

Also, the other day I happened to notice that backtick strings are much slower to create than typical concatenation. I'm not sure what, if any, impact it has on GC though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, as indicated, much of this change is speculative. The code cleanups
make a few assumptions about what v8 v5 should be doing as far as
optimizations but those assumptions have yet to be tested. Those specific
changes can be pulled back out of this if necessary tho.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've separated the more general modifications to console out to a separate PR #6233

@jasnell
Copy link
Member Author

jasnell commented Apr 30, 2016

Closing. Made several iterations on this basic approach and not happy with it. Will be investigating other options.

@jasnell jasnell closed this Apr 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem. process Issues and PRs related to the process subsystem. semver-major PRs that contain breaking changes and should be released in the next major version. wip Issues and PRs that are still a work in progress.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants