process: fix panic from spurious pidfd wakeup (#7494)

This commit is contained in:
Alice Ryhl 2025-08-01 11:27:15 +02:00 committed by GitHub
parent f669a609cf
commit 1979615cbf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 20 deletions

View File

@ -125,7 +125,7 @@ impl<E: Source> PollEvented<E> {
} }
/// Returns a reference to the registration. /// Returns a reference to the registration.
#[cfg(feature = "net")] #[cfg(any(feature = "net", all(feature = "process", target_os = "linux")))]
pub(crate) fn registration(&self) -> &Registration { pub(crate) fn registration(&self) -> &Registration {
&self.registration &self.registration
} }
@ -138,14 +138,6 @@ impl<E: Source> PollEvented<E> {
Ok(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(|_| ())
}
/// Re-register under new runtime with `interest`. /// Re-register under new runtime with `interest`.
#[cfg(all(feature = "process", target_os = "linux"))] #[cfg(all(feature = "process", target_os = "linux"))]
pub(crate) fn reregister(&mut self, interest: Interest) -> io::Result<()> { pub(crate) fn reregister(&mut self, interest: Interest) -> io::Result<()> {

View File

@ -19,7 +19,7 @@ use std::{
pin::Pin, pin::Pin,
process::ExitStatus, process::ExitStatus,
sync::atomic::{AtomicBool, Ordering::Relaxed}, sync::atomic::{AtomicBool, Ordering::Relaxed},
task::{ready, Context, Poll}, task::{Context, Poll},
}; };
#[derive(Debug)] #[derive(Debug)]
@ -117,17 +117,21 @@ where
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = Pin::into_inner(self); let this = Pin::into_inner(self);
match ready!(this.pidfd.poll_read_ready(cx)) { match this.pidfd.registration().poll_read_ready(cx) {
Err(err) if is_rt_shutdown_err(&err) => { Poll::Ready(Ok(evt)) => {
this.pidfd.reregister(Interest::READABLE)?; if let Some(exit_code) = this.inner.try_wait()? {
ready!(this.pidfd.poll_read_ready(cx))? return Poll::Ready(Ok(exit_code));
}
this.pidfd.registration().clear_readiness(evt);
} }
res => res?, Poll::Ready(Err(err)) if is_rt_shutdown_err(&err) => {}
} Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
Poll::Ready(Ok(this Poll::Pending => return Poll::Pending,
.inner };
.try_wait()?
.expect("pidfd is ready to read, the process should have exited"))) this.pidfd.reregister(Interest::READABLE)?;
cx.waker().wake_by_ref();
Poll::Pending
} }
} }

View File

@ -0,0 +1,28 @@
#![cfg(feature = "process")]
#![warn(rust_2018_idioms)]
#![cfg(target_os = "linux")]
#![cfg(not(miri))]
use tokio::process::Command;
use tokio::time::{sleep, Duration};
#[tokio::test]
async fn issue_7144() {
let mut threads = vec![];
for _ in 0..20 {
threads.push(tokio::spawn(test_one()));
}
for thread in threads {
thread.await.unwrap();
}
}
async fn test_one() {
let mut t = Command::new("strace")
.args("-o /dev/null -D sleep 5".split(' '))
.spawn()
.unwrap();
sleep(Duration::from_millis(100)).await;
unsafe { libc::kill(t.id().unwrap() as _, libc::SIGINT) };
t.wait().await.unwrap();
}