Initially I asked this on StackOverflow, but looks like a bug (nodejs/node-v0.x-archive#9190). Post follows:
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).
I tried to ps.stdin.close() in ps.on("close", cb). It didn't work... 😢
Initially I asked this on StackOverflow, but looks like a bug (nodejs/node-v0.x-archive#9190). Post follows:
I'm using
spawnto create a child process and pipe data:The problem is that when piping the
process.stdinto the child processstdin, the main process is not ended when the child process is finished.The code looks like this (not a really good example because
psdoes not usestdindata, I guess):If I remove the
process.stdin.pipe(ps.stdin)line, the main process is ended, but thestdindata is not piped anymore.Why isn't the main process ended when the
pschild process is ended? How can I solve this problem?An ugly solution would be:
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).I tried to
ps.stdin.close()inps.on("close", cb). It didn't work... 😢