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 1 commit
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
Prev Previous commit
Next Next commit
Fix process_spawned_and_wait_in_different_runtime on linux with pidfd
Signed-off-by: Jiahao XU <[email protected]>
  • Loading branch information
NobodyXu committed Jan 10, 2024
commit ef05617d4e781b328daf72266c1047bb365afeab
11 changes: 11 additions & 0 deletions tokio/src/io/poll_evented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ impl<E: Source> PollEvented<E> {
.map_err(io::Error::from)
.map_ok(|_| ())
}

/// Re-register under new runtime with `interest`.
#[cfg(all(feature = "process", target_os = "linux"))]
pub(crate) fn reregister(&mut self, interest: Interest) -> io::Result<()> {
let io = self.io.as_mut().unwrap(); // As io shouldn't ever be None, just unwrap here.
let _ = self.registration.deregister(io);
self.registration =
Registration::new_with_interest_and_handle(io, interest, scheduler::Handle::current())?;

Ok(())
}
}

feature! {
Expand Down
20 changes: 19 additions & 1 deletion tokio/src/process/unix/pidfd_reaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
imp::{orphan::Wait, OrphanQueue},
kill::Kill,
},
util::error::RUNTIME_SHUTTING_DOWN_ERROR,
};

use libc::{syscall, SYS_pidfd_open, __errno_location, ENOSYS, PIDFD_NONBLOCK};
Expand Down Expand Up @@ -93,6 +94,17 @@ where
pidfd: PollEvented<Pidfd>,
}

#[allow(deprecated)]
fn is_rt_shutdown_err(err: &io::Error) -> bool {
if let Some(inner) = err.get_ref() {
// Using `Error::description()` is more efficient than `format!("{inner}")`,
// so we use it here even if it is deprecated.
inner.source().is_none() && inner.description() == RUNTIME_SHUTTING_DOWN_ERROR
} else {
false
}
}

impl<W> Future for PidfdReaperInner<W>
where
W: Wait + Unpin,
Expand All @@ -102,7 +114,13 @@ where
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = Pin::into_inner(self);

ready!(this.pidfd.poll_read_ready(cx))?;
match ready!(this.pidfd.poll_read_ready(cx)) {
Err(err) if err.kind() == io::ErrorKind::Other && is_rt_shutdown_err(&err) => {
NobodyXu marked this conversation as resolved.
Show resolved Hide resolved
this.pidfd.reregister(Interest::READABLE)?;
ready!(this.pidfd.poll_read_ready(cx))?
}
res => res?,
}
Poll::Ready(Ok(this
.inner
.try_wait()?
Expand Down
Loading