Skip to content

Allow process::Child to be used with external calls to waitpid  #39188

Open
@zackw

Description

@zackw

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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-processArea: `std::process` and `std::env`C-feature-acceptedCategory: A feature request that has been accepted pending implementation.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions