Skip to content

Commit 2411bea

Browse files
bnoordhuistrevnorris
authored andcommitted
lib: fix stdio/ipc sync i/o regression
process.send() should be synchronous, it should block until the message has been sent in full, but it wasn't after the second-to-last libuv upgrade because of commit libuv/libuv@393c1c5 ("unix: set non-block mode in uv_{pipe,tcp,udp}_open"), which made its way into io.js in commit 07bd05b ("deps: update libuv to 1.2.1"). Commit libuv/libuv@b36d4ff ("unix: implement uv_stream_set_blocking()") as landed in io.js in commit 9681fca ("deps: update libuv to 1.4.0") makes it possible to restore the synchronous behavior again and that's precisely what this commit does. The same line of reasoning applies to `net.Socket({ fd: 1 })`: creating a socket object from a stdio file descriptor, like the `process.stdout` getter does, should put the file descriptor in blocking mode for compatibility reasons. Reviewed-By: Julien Gilli <[email protected]> PR-URL: nodejs/node-v0.x-archive#9179
1 parent c0766eb commit 2411bea

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

lib/child_process.js

+7
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,13 @@ exports._forkChild = function(fd) {
586586
// set process.send()
587587
var p = createPipe(true);
588588
p.open(fd);
589+
590+
// p.open() puts the file descriptor in non-blocking mode
591+
// but it must be synchronous for backwards compatibility.
592+
var err = p.setBlocking(true);
593+
if (err)
594+
throw errnoException(err, 'setBlocking');
595+
589596
p.unref();
590597
setupChannel(process, p);
591598

lib/net.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ function Socket(options) {
155155
} else if (!util.isUndefined(options.fd)) {
156156
this._handle = createHandle(options.fd);
157157
this._handle.open(options.fd);
158-
if ((options.fd == 1 || options.fd == 2) &&
159-
(this._handle instanceof Pipe) &&
160-
process.platform === 'win32') {
158+
// this._handle.open() puts the file descriptor in non-blocking
159+
// mode but it must be synchronous for backwards compatibility.
160+
if ((options.fd == 1 || options.fd == 2) && this._handle instanceof Pipe) {
161161
// Make stdout and stderr blocking on Windows
162162
var err = this._handle.setBlocking(true);
163163
if (err)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var fork = require('child_process').fork;
25+
var N = 4 << 20; // 4 MB
26+
27+
for (var big = '*'; big.length < N; big += big);
28+
29+
if (process.argv[2] === 'child') {
30+
process.send(big);
31+
process.exit(42);
32+
}
33+
34+
var proc = fork(__filename, ['child']);
35+
36+
proc.on('message', common.mustCall(function(msg) {
37+
assert.equal(typeof msg, 'string');
38+
assert.equal(msg.length, N);
39+
assert.equal(msg, big);
40+
}));
41+
42+
proc.on('exit', common.mustCall(function(exitCode) {
43+
assert.equal(exitCode, 42);
44+
}));

test/simple/test-net-sync-stdio.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var spawn = require('child_process').spawn;
25+
var N = 4 << 20; // 4 MB
26+
27+
for (var big = '*'; big.length < N; big += big);
28+
29+
if (process.argv[2] === 'child') {
30+
process.stdout.write(big);
31+
process.exit(42);
32+
}
33+
34+
var stdio = ['inherit', 'pipe', 'inherit'];
35+
var proc = spawn(process.execPath, [__filename, 'child'], { stdio: stdio });
36+
37+
var chunks = [];
38+
proc.stdout.setEncoding('utf8');
39+
proc.stdout.on('data', chunks.push.bind(chunks));
40+
41+
proc.on('exit', common.mustCall(function(exitCode) {
42+
assert.equal(exitCode, 42);
43+
assert.equal(chunks.join(''), big);
44+
}));

0 commit comments

Comments
 (0)