Skip to content

Commit

Permalink
child_process: give forks stdout and stderr
Browse files Browse the repository at this point in the history
Previously using these streams was not possible because of making them
use given file descriptors. This solution is more generic - it uses
`pipe` function to pipe fork's output into parent's `stdout` and
`stderr` unless `silent: true` is set as an option.

This doesn't break any existing API and it makes `fork` more compatible
with `spawn`.

Please note that fork's `stdout` and `stderr` aren't meant to be used as
communication channels - use `fork(...).send()` instead.

Fixes nodejs#2442.
  • Loading branch information
mmalecki committed Jan 3, 2012
1 parent c123ac0 commit ae2a4ee
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ exports.fork = function(modulePath, args, options) {
throw new Error('customFds not allowed for fork()');
}

// Leave stdin open for the IPC channel. stdout and stderr should be the
// same as the parent's if silent isn't set.
options.customFds = (options.silent ? [-1, -1, -1] : [-1, 1, 2]);

// Just need to set this - child process won't actually use the fd.
// For backwards compat - this can be changed to 'NODE_CHANNEL' before v0.6.
if (!options.env) options.env = { };
Expand All @@ -181,6 +177,11 @@ exports.fork = function(modulePath, args, options) {

var child = spawn(process.execPath, args, options);

if (!options.silent) {
child.stdout.pipe(process.stdout, { end: false });
child.stderr.pipe(process.stderr, { end: false });
}

setupChannel(child, options.stdinStream);

child.on('exit', function() {
Expand Down

0 comments on commit ae2a4ee

Please sign in to comment.