Skip to content

Commit 8b751f7

Browse files
committed
process: use more direct sync I/O for stdio
This avoids routing writes through the full LibuvStreamWrap write machinery. In particular, it enables the next commit, because otherwise the callback passed to `_write()` would not be called synchronously for pipes on Windows (because the latter does not support `uv_try_write()`, even for blocking I/O). PR-URL: #18019 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent c84582c commit 8b751f7

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

lib/internal/net.js

+24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
const Buffer = require('buffer').Buffer;
4+
const { writeBuffer } = process.binding('fs');
5+
36
// Check that the port number is not NaN when coerced to a number,
47
// is an integer and that it falls within the legal range of port numbers.
58
function isLegalPort(port) {
@@ -9,7 +12,28 @@ function isLegalPort(port) {
912
return +port === (+port >>> 0) && port <= 0xFFFF;
1013
}
1114

15+
function makeSyncWrite(fd) {
16+
return function(chunk, enc, cb) {
17+
if (enc !== 'buffer')
18+
chunk = Buffer.from(chunk, enc);
19+
20+
this._bytesDispatched += chunk.length;
21+
22+
try {
23+
writeBuffer(fd, chunk, 0, chunk.length, null);
24+
} catch (ex) {
25+
// Legacy: net writes have .code === .errno, whereas writeBuffer gives the
26+
// raw errno number in .errno.
27+
if (typeof ex.code === 'string')
28+
ex.errno = ex.code;
29+
return cb(ex);
30+
}
31+
cb();
32+
};
33+
}
34+
1235
module.exports = {
1336
isLegalPort,
37+
makeSyncWrite,
1438
normalizedArgsSymbol: Symbol('normalizedArgs')
1539
};

lib/net.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ const stream = require('stream');
2626
const timers = require('timers');
2727
const util = require('util');
2828
const internalUtil = require('internal/util');
29-
const { isLegalPort, normalizedArgsSymbol } = require('internal/net');
29+
const {
30+
isLegalPort,
31+
normalizedArgsSymbol,
32+
makeSyncWrite
33+
} = require('internal/net');
3034
const assert = require('assert');
3135
const cares = process.binding('cares_wrap');
3236
const {
@@ -220,20 +224,24 @@ function Socket(options) {
220224
this._handle = options.handle; // private
221225
this[async_id_symbol] = getNewAsyncId(this._handle);
222226
} else if (options.fd !== undefined) {
223-
this._handle = createHandle(options.fd, false);
224-
this._handle.open(options.fd);
227+
const fd = options.fd;
228+
this._handle = createHandle(fd, false);
229+
this._handle.open(fd);
225230
this[async_id_symbol] = this._handle.getAsyncId();
226231
// options.fd can be string (since it is user-defined),
227232
// so changing this to === would be semver-major
228233
// See: https://github.com/nodejs/node/pull/11513
229234
// eslint-disable-next-line eqeqeq
230-
if ((options.fd == 1 || options.fd == 2) &&
235+
if ((fd == 1 || fd == 2) &&
231236
(this._handle instanceof Pipe) &&
232237
process.platform === 'win32') {
233238
// Make stdout and stderr blocking on Windows
234239
var err = this._handle.setBlocking(true);
235240
if (err)
236241
throw errnoException(err, 'setBlocking');
242+
243+
this._writev = null;
244+
this._write = makeSyncWrite(fd);
237245
}
238246
this.readable = options.readable !== false;
239247
this.writable = options.writable !== false;

lib/tty.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
const util = require('util');
2525
const net = require('net');
2626
const { TTY, isTTY } = process.binding('tty_wrap');
27+
const { makeSyncWrite } = require('internal/net');
2728
const { inherits } = util;
2829
const errnoException = util._errnoException;
2930
const errors = require('internal/errors');
@@ -91,6 +92,8 @@ function WriteStream(fd) {
9192
// even though it was originally intended to change in v1.0.2 (Libuv 1.2.1).
9293
// Ref: https://github.com/nodejs/node/pull/1771#issuecomment-119351671
9394
this._handle.setBlocking(true);
95+
this._writev = null;
96+
this._write = makeSyncWrite(fd);
9497

9598
var winSize = new Array(2);
9699
var err = this._handle.getWindowSize(winSize);

0 commit comments

Comments
 (0)