In my Rust application, I need to run an executable program, wait for it to terminate, and gather its output and status code. I'm currently using Command::spawn and wait_with_output to achieve this, which works well for small programs. However, when I run a program that generates a large amount of output, my Rust program hangs (at wait_with_output), and the child process becomes a zombie (<defunct>).
let mut command_cmd = Command::new(executable);
command_cmd.args(args).stdout(std::process::Stdio::piped());
// spawn the exec
let mut child_proc = match command_cmd.spawn() {
Ok(output) => output,
Err(e) => {
...
}
};
let output = match child_proc.wait_with_output() {
Ok(output) => output,
Err(e) => {
...
}
};
let stdout = String::from_utf8(output.stdout).unwrap();
let status = output.status;
If I do child_proc.wait() without waiting for the output, the child finishes and returns a status code.
After researching online, I suspect that the large amount of output is causing the issue, but I'm not sure how to proceed. I need to collect the program's output and exit code for further processing.
Can anyone provide guidance on how to handle large amounts of program output while still gathering the exit code in Rust? Any help or advice would be greatly appreciated!
Interesting. You say wait_with_output vs. wait makes a difference, yet all that wait_with_output does beyond wait is to read from the spawned process’s pipe (in this case stdout).
To make this question more easy to answer, it would probably useful if you could
confirm that a minimal change (e.g. just changing the name of the function being called) between using .wait_with_output and ignoring the result vs. using .wait and ignoring the result is really what makes the difference, and
give an estimation of what kind of size you are talking about when saying “a large amount of output”
If you just want to execute the command, wait for it to return and get its output why can't you use the output method of Command ? That will give the status code and every output of your command.
FYI: I would consider 3000 chars to be very small.
By actual code I mean code we can compile and run such as this (or this using output directly. (the large comment is just to make the output larger than 3000 chars)