How do I make sure that file handle for every `Chi

2019-03-02 17:39发布

问题:

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 the Child has exited then it will continue to run, even after the Child 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?

回答1:

If you read the paragraph immediately after the one you have quoted:

Calling wait (or other functions that wrap around it) will make the parent process wait until the child has actually exited before continuing.

In order to call wait, you need to not move stdout out of Child:

let echo_out = echo_child.stdout.take().expect("failed to open 'echo' stdout");

// ...

echo_child.wait().expect("Couldn't wait for echo child");

See also:

  • Kill child process while waiting for it