]> git.lizzy.rs Git - rust.git/commit
std: Add a nonblocking `Child::try_wait` method
authorAlex Crichton <alex@alexcrichton.com>
Fri, 6 Jan 2017 06:47:09 +0000 (22:47 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Sat, 7 Jan 2017 05:20:39 +0000 (21:20 -0800)
commitabb91890831d71f5cbe4346a9ab57d432372df65
treed7886306e0b9d5f174a0b12fde2d595e15863b2c
parent42bed7238534b50178cb2bd275f1153d67cc3ece
std: Add a nonblocking `Child::try_wait` method

This commit adds a new method to the `Child` type in the `std::process` module
called `try_wait`. This method is the same as `wait` except that it will not
block the calling thread and instead only attempt to collect the exit status. On
Unix this means that we call `waitpid` with the `WNOHANG` flag and on Windows it
just means that we pass a 0 timeout to `WaitForSingleObject`.

Currently it's possible to build this method out of tree, but it's unfortunately
tricky to do so. Specifically on Unix you essentially lose ownership of the pid
for the process once a call to `waitpid` has succeeded. Although `Child` tracks
this state internally to be resilient to multiple calls to `wait` or a `kill`
after a successful wait, if the child is waited on externally then the state
inside of `Child` is not updated. This means that external implementations of
this method must be extra careful to essentially not use a `Child`'s methods
after a call to `waitpid` has succeeded (even in a nonblocking fashion).

By adding this functionality to the standard library it should help canonicalize
these external implementations and ensure they can continue to robustly reuse
the `Child` type from the standard library without worrying about pid ownership.
src/libstd/process.rs
src/libstd/sys/unix/process/process_unix.rs
src/libstd/sys/windows/c.rs
src/libstd/sys/windows/process.rs
src/test/run-pass/try-wait.rs [new file with mode: 0644]