Description
The wait
and wait_with_output
methods of process::Child
expect to do the waiting for the child to exit themselves. If you are running many processes at once, and you don't know which one will finish first, this is inconvenient, because the system calls wait
and waitpid
consume each exit notification as they report it. (waitid
can be told not to do that, but it is not fully supported in the libc crate yet.) That places the Child
object in an inconsistent state, in which it is not safe to call wait
or wait_with_output
.
If Child
had a set_exit_status
method, an application in this situation could use it to keep the objects consistent: something like
fn wait_all(children: &[&mut Child]) -> nix::Result<()> {
use nix::sys::wait::{wait, WaitStatus}
let mut pending : HashMap<u32, &mut Child> =
children.iter().map(|c| (c.id(), c)).collect();
while pending.len() {
let status = try!(wait());
match status {
Exited(pid, ..) | Signaled(pid, ..) => {
let mut child = try!(pending.remove(pid).ok_or(UnexpectedChild(pid)));
child.set_exit_status(status);
},
_ => unreachable!();
}
}
Ok(())
}
(In this hypothetical, ExitStatus
impls From<nix::WaitStatus>
, which I believe nix can make happen without assistance from std.)