Skip to content

Use pidfd on Linux for tokio::process::Child::wait #6141

@NobodyXu

Description

@NobodyXu

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 all tokio::process::Child::wait() future to be awakened and execute std::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:

And then the pidfd returned can be epolled and it will awake only one future when the process has exited, then waitid 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.)

For tokio::process::Child::{kill, start_kill}, if we have a pidfd, then pidfd_send_signal is the most robust way of sending the signal to the right process.

According to pidfd_open:

Even if the child has already terminated by the time of the pidfd_open() call, its PID will not have been recycled and the returned file descriptor will refer to the resulting zombie process.

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:

use std::os::fd::BorrowedFd;

impl Child {
    #[cfg(linux)]
    fn pidfd(&self) -> Option<BorrowedFd<'_>>;
}

Describe alternatives you've considered

#6140

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tokioArea: The main tokio crateC-feature-requestCategory: A feature request.M-processModule: tokio/process

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions