Skip to content

Commit

Permalink
process: always make stdio blocking
Browse files Browse the repository at this point in the history
Always make stdio blocking. There have only been a few exceptions to
this rule and I can’t seem to find any reason for those that outweighs
the benefits.

Fixes: #6456
Fixes: #12921
  • Loading branch information
addaleax committed May 11, 2017
1 parent 6914aea commit d6a90c4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
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

0 comments on commit d6a90c4

Please sign in to comment.