How do I make sure that file handle for every `Child` process is released after every iteration?

久未见 提交于 2019-12-02 13:26:29

问题


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


来源:https://stackoverflow.com/questions/48547534/how-do-i-make-sure-that-file-handle-for-every-child-process-is-released-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!