Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(process): Use pidfd on Linux for tokio::process::Child::wait #6152

Merged
merged 17 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ net = [
"windows-sys/Win32_System_SystemServices",
]
process = [
"rt",
NobodyXu marked this conversation as resolved.
Show resolved Hide resolved
"bytes",
"libc",
"mio/os-poll",
NobodyXu marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
8 changes: 8 additions & 0 deletions tokio/src/io/poll_evented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ impl<E: Source> PollEvented<E> {
self.registration.deregister(&mut inner)?;
Ok(inner)
}

#[cfg(all(feature = "process", target_os = "linux"))]
pub(crate) fn poll_read_ready(&self, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.registration
.poll_read_ready(cx)
.map_err(io::Error::from)
.map_ok(|_| ())
}
}

feature! {
Expand Down
57 changes: 44 additions & 13 deletions tokio/src/process/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ use orphan::{OrphanQueue, OrphanQueueImpl, Wait};
mod reap;
use reap::Reaper;

#[cfg(target_os = "linux")]
mod pidfd_reaper;

use crate::io::{AsyncRead, AsyncWrite, PollEvented, ReadBuf};
use crate::process::kill::Kill;
use crate::process::SpawnedChild;
Expand Down Expand Up @@ -100,15 +103,15 @@ impl OrphanQueue<StdChild> for GlobalOrphanQueue {
}

#[must_use = "futures do nothing unless polled"]
pub(crate) struct Child {
inner: Reaper<StdChild, GlobalOrphanQueue, Signal>,
pub(crate) enum Child {
SignalReaper(Reaper<StdChild, GlobalOrphanQueue, Signal>),
#[cfg(target_os = "linux")]
PidfdReaper(pidfd_reaper::PidfdReaper<StdChild, GlobalOrphanQueue>),
}

impl fmt::Debug for Child {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Child")
.field("pid", &self.inner.id())
.finish()
fmt.debug_struct("Child").field("pid", &self.id()).finish()
}
}

Expand All @@ -118,12 +121,24 @@ pub(crate) fn spawn_child(cmd: &mut std::process::Command) -> io::Result<Spawned
let stdout = child.stdout.take().map(stdio).transpose()?;
let stderr = child.stderr.take().map(stdio).transpose()?;

#[cfg(target_os = "linux")]
match pidfd_reaper::PidfdReaper::new(child, GlobalOrphanQueue) {
NobodyXu marked this conversation as resolved.
Show resolved Hide resolved
Ok(pidfd_reaper) => {
return Ok(SpawnedChild {
child: Child::PidfdReaper(pidfd_reaper),
stdin,
stdout,
stderr,
})
}
Err((Some(err), _child)) => return Err(err),
Err((None, child_returned)) => child = child_returned,
}

let signal = signal(SignalKind::child())?;

Ok(SpawnedChild {
child: Child {
inner: Reaper::new(child, GlobalOrphanQueue, signal),
},
child: Child::SignalReaper(Reaper::new(child, GlobalOrphanQueue, signal)),
stdin,
stdout,
stderr,
Expand All @@ -132,25 +147,41 @@ pub(crate) fn spawn_child(cmd: &mut std::process::Command) -> io::Result<Spawned

impl Child {
pub(crate) fn id(&self) -> u32 {
self.inner.id()
match self {
Self::SignalReaper(signal_reaper) => signal_reaper.id(),
#[cfg(target_os = "linux")]
Self::PidfdReaper(pidfd_reaper) => pidfd_reaper.id(),
}
}

fn std_child(&mut self) -> &mut StdChild {
match self {
Self::SignalReaper(signal_reaper) => signal_reaper.inner_mut(),
#[cfg(target_os = "linux")]
Self::PidfdReaper(pidfd_reaper) => pidfd_reaper.inner_mut(),
}
}

pub(crate) fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
self.inner.inner_mut().try_wait()
self.std_child().try_wait()
}
}

impl Kill for Child {
fn kill(&mut self) -> io::Result<()> {
self.inner.kill()
self.std_child().kill()
}
}

impl Future for Child {
type Output = io::Result<ExitStatus>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.inner).poll(cx)
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match Pin::into_inner(self) {
Self::SignalReaper(signal_reaper) => Pin::new(signal_reaper).poll(cx),
#[cfg(target_os = "linux")]
Self::PidfdReaper(pidfd_reaper) => Pin::new(pidfd_reaper).poll(cx),
}
}
}

Expand Down
Loading
Loading