I have the following program taken from the Rust docs for std::process::Command
. It stops working after some iterations.
use std::process::Command;
use std::process::Stdio;
fn main() {
loop {
let mut echo_child = Command::new("echo")
.arg("oh no a tpyo")
.stdout(Stdio::piped())
.spawn()
.expect("failed to start 'echo'");
let echo_out = echo_child.stdout.expect("failed to open 'echo' stdout");
let sed_child = Command::new("sed")
.arg("s/tpyo/typo/")
.stdin(Stdio::from(echo_out))
.stdout(Stdio::piped())
.spawn()
.expect("failed to start 'sed'");
let sed_out = sed_child
.wait_with_output()
.expect("failed to wait on 'sed'");
let sed_out_slice = sed_out.stdout.as_slice();
assert_eq!(b"oh no a typo\n", sed_out_slice);
println!("out: {:?}", String::from_utf8_lossy(sed_out_slice));
}
}
Every time it crashes, I receive the following output:
thread 'main' panicked at 'failed to start 'sed': Error { repr: Os { code: 35, message: "Resource temporarily unavailable" } }', src/libcore/result.rs:906:4
According to the docs for Child
(where I took this program from), it says:
There is no implementation of
Drop
for child processes, so if you do not ensure theChild
has exited then it will continue to run, even after theChild
handle to the child process has gone out of scope.
How do I make sure that file handle for every Child
process is released after every iteration?