-
Notifications
You must be signed in to change notification settings - Fork 29.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Process does not die when child process to which stdin is piped dies #24327
Comments
I tried manually |
@nodejs/child_process |
Why not just call: |
^ This. It's not a bug, it's a ps quirk. |
@bnoordhuis If #!/usr/bin/env node
const util = require("util");
const cp = require("child_process");
const exe = "scp";
const args = [
"-o",
"StrictHostKeyChecking=no",
"-o",
"LogLevel=ERROR",
"-i",
"~/.ssh/test_machine_rsa",
"-r",
"test.js",
"Administrator@192.168.33.164:C:/test.txt",
];
const { stdin, stdout, stderr } = process;
const child = cp.spawn(exe, args, {
stdio: ["pipe", "pipe", "pipe"],
});
process.stdin.pipe(child.stdin); // this is the problematic one
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
child.on("exit", (code, signal) => {
console.log({ msg: "Exited", code, signal });
}); Maybe the quirk common to both is that neither actually consumes anything from |
That's right; |
child_process
/stream
/process
Basically a duplicate of: #2276, which was either closed without having been resolved or has regressed.
Stick the following in a file:
chmod +x
and run it in some shell, and you'll see it'll output the stuff fromps
, then just hang. Assuming your shell's stdin is being forwarded to the node process, hitting enter will unstick it. If you comment out theprocess.stdin...
line (sinceps
doesn't actually need any stdin), you'll see it end as expected.The workaround I'm using for this is to add the event handler
child.on("exit", (code, signal) => code !== null ? process.exit(code) : signal !== null ? process.kill(process.pid, signal) : fail("Impossible situation, process exited but neither exited nor was killed"))
. I can't really use this in the general case, because I may need to do more stuff while the child process is running and exiting, and can't always assume the parent needs to die precisely when this one specific child process dies. At the same time the parent should be free to exit when all children are dead, and shouldn't hang like this.The text was updated successfully, but these errors were encountered: