Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit da481c6

Browse filesBrowse files
committed
child_process: support stdio option in fork()
This commit allows child_process.fork() to pass stdio options to spawn(). This allows fork() to more easily take advantage of additional stdio channels. Refs: nodejs/node-v0.x-archive#5727 PR-URL: #7811 Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Minwoo Jung <jmwsoft@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
1 parent 2f45941 commit da481c6
Copy full SHA for da481c6

File tree

Expand file treeCollapse file tree

3 files changed

+64
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+64
-4
lines changed
Open diff view settings
Collapse file

‎doc/api/child_process.md‎

Copy file name to clipboardExpand all lines: doc/api/child_process.md
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ added: v0.5.0
253253
piped to the parent, otherwise they will be inherited from the parent, see
254254
the `'pipe'` and `'inherit'` options for [`child_process.spawn()`][]'s
255255
[`stdio`][] for more details (Default: `false`)
256+
* `stdio` {Array} Supports the array version of [`child_process.spawn()`][]'s
257+
[`stdio`][] option. When this option is provided, it overrides `silent`.
256258
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
257259
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
258260
* Return: {ChildProcess}
Collapse file

‎lib/child_process.js‎

Copy file name to clipboardExpand all lines: lib/child_process.js
+8-4Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ exports.fork = function(modulePath /*, args, options*/) {
4444

4545
args = execArgv.concat([modulePath], args);
4646

47-
// Leave stdin open for the IPC channel. stdout and stderr should be the
48-
// same as the parent's if silent isn't set.
49-
options.stdio = options.silent ? ['pipe', 'pipe', 'pipe', 'ipc'] :
50-
[0, 1, 2, 'ipc'];
47+
if (!Array.isArray(options.stdio)) {
48+
// Leave stdin open for the IPC channel. stdout and stderr should be the
49+
// same as the parent's if silent isn't set.
50+
options.stdio = options.silent ? ['pipe', 'pipe', 'pipe', 'ipc'] :
51+
[0, 1, 2, 'ipc'];
52+
} else if (options.stdio.indexOf('ipc') === -1) {
53+
throw new TypeError('Forked processes must have an IPC channel');
54+
}
5155

5256
options.execPath = options.execPath || process.execPath;
5357

Collapse file
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cp = require('child_process');
5+
const net = require('net');
6+
7+
if (process.argv[2] === 'child') {
8+
process.stdout.write('this should be ignored');
9+
process.stderr.write('this should not be ignored');
10+
11+
const pipe = new net.Socket({ fd: 4 });
12+
13+
process.on('disconnect', () => {
14+
pipe.unref();
15+
});
16+
17+
pipe.setEncoding('utf8');
18+
pipe.on('data', (data) => {
19+
process.send(data);
20+
});
21+
} else {
22+
assert.throws(() => {
23+
cp.fork(__filename, {stdio: ['pipe', 'pipe', 'pipe', 'pipe']});
24+
}, /Forked processes must have an IPC channel/);
25+
26+
let ipc = '';
27+
let stderr = '';
28+
const buf = Buffer.from('data to send via pipe');
29+
const child = cp.fork(__filename, ['child'], {
30+
stdio: [0, 'ignore', 'pipe', 'ipc', 'pipe']
31+
});
32+
33+
assert.strictEqual(child.stdout, null);
34+
35+
child.on('message', (msg) => {
36+
ipc += msg;
37+
38+
if (ipc === buf.toString()) {
39+
child.disconnect();
40+
}
41+
});
42+
43+
child.stderr.on('data', (chunk) => {
44+
stderr += chunk;
45+
});
46+
47+
child.on('exit', common.mustCall((code, signal) => {
48+
assert.strictEqual(code, 0);
49+
assert.strictEqual(signal, null);
50+
assert.strictEqual(stderr, 'this should not be ignored');
51+
}));
52+
53+
child.stdio[4].write(buf);
54+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.