2

I'm using spawn to create a child process and pipe data:

child process  |  parent process (main)
---------------------------------------
stdout      ----->       process.stdout
stderr      ----->       process.stderr
stdin       <-----        process.stdin

The problem is that when piping the process.stdin to the child process stdin, the main process is not ended when the child process is finished.

The code looks like this (not a really good example because ps does not use stdin data, I guess):

var Spawn = require("child_process").spawn;

var ps = Spawn("ps");
process.stdin.pipe(ps.stdin);
ps.stdout.pipe(process.stdout);
ps.stderr.pipe(process.stderr);

If I remove the process.stdin.pipe(ps.stdin) line, the main process is ended, but the stdin data is not piped anymore.

Why isn't the main process ended when the ps child process is ended? How can I solve this problem?

An ugly solution would be:

ps.on("close", process.exit.bind(process));

I don't like this, because I don't really want to force the main process to be closed, but I want to be closed naturally (e.g. having setTimeout(function(){}, 1000) you wait 1000ms and then the process ends).

6
  • Did you try Spawn('ps', [], { stdio: 'inherit' });?
    – Amit
    Jul 30, 2015 at 6:56
  • @Amit Using stdio:'inherit' works but I want to be able to pass any stream there. So, I still need to pipe things. Jul 30, 2015 at 6:58
  • 1
    Try ps.on("close", function(){ process.stdin.end(); });
    – stdob--
    Jul 30, 2015 at 7:48
  • @stdob-- The close callback is reached, but after calling stdin.end nothing changes... Just like without it (the main process is not closed). Jul 30, 2015 at 7:51
  • Might be related: bug
    – Amit
    Jul 30, 2015 at 14:10

1 Answer 1

0

I don't know whether this is a bug in Node or expected behavior, but there's a couple of workarounds, one of which may be suitable for you.

If your current process is supposed to die precisely when the child process dies, you can simply add a handler for exit like this:

const fail = err => { throw err }
ps.on("exit", (code, signal) => code !== null ? process.exit(code) : signal !== null ? process.kill(process.pid, signal) : fail("Impossible situation, child process neither exited nor was killed"))

This will just bypass our issues with the stdin stream sitting there holding everything up.

If you do need to do more things in the current process than just run this child process, this approach may not be acceptable. What you can instead do is destroy process.stdin precisely when you become aware that all the work the current process is supposed to do has been accomplished. For example, if all you wanted to run was ps after all, you could add:

ps.on("exit", (...) => {
  ...
  process.stdin.destroy()
})

and this will have the same effect. If there's more work you need to do after ps is dead, you can put the same thing in some other appropriate event handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.