diff --git a/doc/api/process.md b/doc/api/process.md index 40ec3e0b1aaff2..f7915a66faeb59 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -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. diff --git a/lib/net.js b/lib/net.js index 24318207836a2e..49e3d3ff0a0454 100644 --- a/lib/net.js +++ b/lib/net.js @@ -195,18 +195,6 @@ 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 { @@ -214,6 +202,20 @@ function Socket(options) { 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); diff --git a/lib/tty.js b/lib/tty.js index d467c827810491..a0c64f382ad3a0 100644 --- a/lib/tty.js +++ b/lib/tty.js @@ -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) { diff --git a/test/known_issues/test-stdout-buffer-flush-on-exit.js b/test/parallel/test-stdout-buffer-flush-on-exit.js similarity index 100% rename from test/known_issues/test-stdout-buffer-flush-on-exit.js rename to test/parallel/test-stdout-buffer-flush-on-exit.js