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

process: always make stdio blocking #12970

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1629,11 +1629,7 @@ important ways:
respectively.
2. They cannot be closed ([`end()`][] will throw).
3. They will never emit the [`'finish'`][] event.
4. Writes may be synchronous depending on the what the stream is connected to
and whether the system is Windows or Unix:
- Files: *synchronous* on Windows and Linux
- TTYs (Terminals): *asynchronous* on Windows, *synchronous* on Unix
- Pipes (and sockets): *synchronous* on Windows, *asynchronous* on Unix
4. Writes will be synchronous (“blocking”).

These behaviours are partly for historical reasons, as changing them would
create backwards incompatibility, but they are also expected by some users.
Expand Down
26 changes: 14 additions & 12 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,25 +195,27 @@ function Socket(options) {
this._handle = createHandle(options.fd);
this._handle.open(options.fd);
this[async_id_symbol] = this._handle.getAsyncId();
// options.fd can be string (since it is user-defined),
// so changing this to === would be semver-major
// See: https://github.com/nodejs/node/pull/11513
// eslint-disable-next-line eqeqeq
if ((options.fd == 1 || options.fd == 2) &&
(this._handle instanceof Pipe) &&
process.platform === 'win32') {
// Make stdout and stderr blocking on Windows
var err = this._handle.setBlocking(true);
if (err)
throw errnoException(err, 'setBlocking');
}
this.readable = options.readable !== false;
this.writable = options.writable !== false;
} else {
// these will be set once there is a connection
this.readable = this.writable = false;
}

// options.fd can be string (since it is user-defined),
// so changing this to === would be semver-major
// See: https://github.com/nodejs/node/pull/11513
// Also, sometimes libuv re-opens handles (e.g. for TTYs), so check
// both the handle and the passed fd.
// eslint-disable-next-line eqeqeq
if (this._handle && (this._handle.fd === 1 || this._handle.fd === 2) ||
(options.fd == 1 || options.fd == 2)) {
// Make stdout and stderr blocking
var err = this._handle.setBlocking(true);
if (err)
throw errnoException(err, 'setBlocking');
}

// shut down the socket when we're finished with it.
this.on('finish', onSocketFinish);
this.on('_socketEnd', onSocketEnd);
Expand Down
10 changes: 2 additions & 8 deletions lib/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,10 @@ function WriteStream(fd) {
net.Socket.call(this, {
handle: new TTY(fd, false),
readable: false,
writable: true
writable: true,
fd
});

// Prevents interleaved or dropped stdout/stderr output for terminals.
// As noted in the following reference, local TTYs tend to be quite fast and
// this behaviour has become expected due historical functionality on OS X,
// even though it was originally intended to change in v1.0.2 (Libuv 1.2.1).
// Ref: https://github.com/nodejs/node/pull/1771#issuecomment-119351671
this._handle.setBlocking(true);

var winSize = new Array(2);
var err = this._handle.getWindowSize(winSize);
if (!err) {
Expand Down