Child_process.exec returns empty stdout when many child processes are created #4709
-
Version19.7.0 (WSL), PlatformLinux MSI 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 GNU/Linux; SubsystemChild process What steps will reproduce the bug?run the following snippet passing a large number (> 10000), as argument
import cp from "node:child_process";
const throttle = Number(process.argv[2]);
//it doesn't matter the exact command, but the time it requires to be completed. Even ls it is fine, possibly executed on a directory with many files
const cmd = `file -b a.pdf`;
// const cmd = "echo a.pdf";//for windows
const timeout = 100;
let i = 0;
while (i < throttle) {
setTimeout(() => {
const subp = cp.exec(
cmd,
{timeout: 1000 * timeout},
function (error, stdout, stderr) {
if (error) {
throw error;
}
if (stderr) {
throw stderr;
}
if (!stdout) {
throw `stdout is empty!`;
}
}, 0);
subp.on("error", function (error) {
throw error;
});
});
i += 1;
}; How often does it reproduce? Is there a required condition?When the number of processess spawned is very large, it may happen. The exact threshold depends on what the machine is currenty executing at the time, the time needed by the child process to terminate its work, etc. What is the expected behavior?any kind of error/exception, signaling possibly too many spawned, etc. What do you see instead?stdout is empty Additional informationNo response |
Beta Was this translation helpful? Give feedback.
Replies: 0 comments 16 replies
-
in windows I can reproduce the issue even with |
Beta Was this translation helpful? Give feedback.
-
I've converted this to a discussion because I see no reason to believe this is a bug in node rather than a problem with the program you're trying to execute. I'll convert it back if you can conclusively demonstrate the issue lies with node. |
Beta Was this translation helpful? Give feedback.
-
I'm still investigating the issue but I found that the issue does not appear if I call spawn instead of exec. with the command line |
Beta Was this translation helpful? Give feedback.
-
Well, I think I've discovered what happens. In short, imho this discussion should go back in the issue section |
Beta Was this translation helpful? Give feedback.
-
Ok, after having performed another bazillion of tests, I'm going to write the "accepted" aswer. When exec (or execFile) is invoked with a timeout, three different main cases may occur (there are some variations, though):
The problem here is that , from the point of view of the callback, there's no difference in those three cases, which is actually a bug imho. And I couldn't find any mentions on the docs. I don't think anymore that a "timeout" event is the correct behavior, I'm prone to consider a not empty callback error argument as the proper behaviour in 2. and 3. Otherwise, checking the aforementioned attributes in the callback body should do as a workaround @bnoordhuis, what is you opinion? Cheers |
Beta Was this translation helpful? Give feedback.
Ok, after having performed another bazillion of tests, I'm going to write the "accepted" aswer.
When exec (or execFile) is invoked with a timeout, three different main cases may occur (there are some variations, though):
The problem here is that , from the point of view of the callback, there's no difference in those three cases, which is actually a bug imho.
you must chec…