Use pidfd on Linux for tokio::process::Child::wait
#6141
Labels
A-tokio
Area: The main tokio crate
C-feature-request
Category: A feature request.
M-process
Module: tokio/process
Is your feature request related to a problem? Please describe.
tokio::process::Child::wait
currently uses signal to decide whether or not the child might have exited.Since signal can be dropped at arbitrary time, any
SIGCHLD
signal received would cause alltokio::process::Child::wait()
future to be awakened and executestd::process::Child::try_wait
to decide whether it's ready.Describe the solution you'd like
On Linux, a better solution would be to use pidfd.
Since libstd support for pidfd is still unstable rust-lang/rust#82971, tokio can choose to either:
vfork
cannot be used and a lot of code will be addedpidfd_open
in parent to open the pid, this isn't 100% race free but it might be a good start, it will be simple to implement andvfork
can continue to be usedAnd then the pidfd returned can be
epoll
ed and it will awake only one future when the process has exited, thenwaitid
can be used to wait on the child in a race free manner.(Or tokio can just continue to call
std::process::Child::try_wait
since holding a pidfd prevents the pid from being recycled.)Fortokio::process::Child::{kill, start_kill}
, if we have a pidfd, thenpidfd_send_signal
is the most robust way of sending the signal to the right process.According to
pidfd_open
:So as long as the pidfd is held, existing implementation of
tokio::process::Child::{kill, start_kill}
should be race-free.tokio can also have a new method:
Describe alternatives you've considered
#6140
Additional context
The text was updated successfully, but these errors were encountered: