-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
The process never ends when piping the stdin to a child process #2276
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Your example works fine with a command that uses stdin like var Spawn = require('child_process').spawn;
var ps = Spawn('cat');
process.stdin.pipe(ps.stdin);
ps.stdout.pipe(process.stdout);
ps.stderr.pipe(process.stderr);
We may need more information about the context of your problem, in order to help further. |
Does Edit: Maybe |
@targos Interesting! If I pipe data to the process, it's closed: The code is: var Spawn = require("child_process").spawn;
var ps = Spawn('cat');
process.stdin.pipe(ps.stdin);
ps.stdout.pipe(process.stdout);
ps.stderr.pipe(process.stderr);
ps.on("close", function () {
console.log("Closed");
}); And the result is: $ echo "foo" | node index.js
foo
Closed If I don't pipe anything, the process is not closed, which is fine in this case because the child process is not closed (it still listens for data). However, in the $ node index.js # This does not close the main process, even the child process is closed
PID TTY TIME CMD
9021 pts/23 00:00:00 node
9026 pts/23 00:00:00 ps
30815 pts/23 00:00:00 bash
Closed
^C
$ echo "foo" | node index.js
PID TTY TIME CMD
12728 pts/23 00:00:00 node
12733 pts/23 00:00:00 ps
30815 pts/23 00:00:00 bash
Closed
$ echo "foo" | node index.js
PID TTY TIME CMD
12735 pts/23 00:00:00 node
12740 pts/23 00:00:00 ps
30815 pts/23 00:00:00 bash
Closed
$ echo "foo" | node index.js
events.js:85
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at exports._errnoException (util.js:746:11)
at WriteWrap.afterWrite (net.js:766:14) Why does the EPIPE error appears randomly?
I'm happy to provide more information, just ask me. 😸 @Fishrock123 Yeah, probable it doesn't use the stdin data, but the child process is closed -- while the parent still remains alive. |
I'm not sure, but I guess we could say that if the stdin data is not consumed in the child process, then the main process never ends, even the child process does. |
Any feedback on this? |
@IonicaBizau hmmm, that looks like #947? (Which fwiw, is a pretty tricky issue.) |
@Fishrock123 Ah, actually, yes. Thanks. |
I'm still seeing this issue in Node v10.3.0, was this supposed to be resolved in #947? |
The issue being that if you create a child process and pipe the parent's |
I'm a bit confused by the current state of this, you seem to show this is not a problem with
Can you provide a small self-contained example and a description of how the examples behaviour differs from your expectation? |
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:The problem is that when piping the
process.stdin
to 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
ps
does not usestdin
data, I guess):If I remove the
process.stdin.pipe(ps.stdin)
line, the main process is ended, but thestdin
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:
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... 😢The text was updated successfully, but these errors were encountered: