Skip to content
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
42 changes: 1 addition & 41 deletions tokio/src/process/unix/pidfd_reaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
imp::{orphan::Wait, OrphanQueue},
kill::Kill,
},
util::error::RUNTIME_SHUTTING_DOWN_ERROR,
};

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

fn display_eq(d: impl std::fmt::Display, s: &str) -> bool {
use std::fmt::Write;

struct FormatEq<'r> {
remainder: &'r str,
unequal: bool,
}

impl<'r> Write for FormatEq<'r> {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
if !self.unequal {
if let Some(new_remainder) = self.remainder.strip_prefix(s) {
self.remainder = new_remainder;
} else {
self.unequal = true;
}
}
Ok(())
}
}

let mut fmt_eq = FormatEq {
remainder: s,
unequal: false,
};
let _ = write!(fmt_eq, "{d}");
fmt_eq.remainder.is_empty() && !fmt_eq.unequal
}

fn is_rt_shutdown_err(err: &io::Error) -> bool {
if let Some(inner) = err.get_ref() {
err.kind() == io::ErrorKind::Other
&& inner.source().is_none()
&& display_eq(inner, RUNTIME_SHUTTING_DOWN_ERROR)
} else {
false
}
}

impl<W> Future for PidfdReaperInner<W>
where
W: Wait + Unpin,
Expand All @@ -150,7 +110,7 @@ where
}
this.pidfd.registration().clear_readiness(evt);
}
Poll::Ready(Err(err)) if is_rt_shutdown_err(&err) => {}
Poll::Ready(Err(err)) if crate::runtime::is_rt_shutdown_err(&err) => {}
Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
Poll::Pending => return Poll::Pending,
};
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ cfg_rt! {
pub use handle::{EnterGuard, Handle, TryCurrentError};

mod runtime;
pub use runtime::{Runtime, RuntimeFlavor};
pub use runtime::{Runtime, RuntimeFlavor, is_rt_shutdown_err};

/// Boundary value to prevent stack overflow caused by a large-sized
/// Future being placed in the stack.
Expand Down
42 changes: 42 additions & 0 deletions tokio/src/runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use crate::runtime::blocking::BlockingPool;
use crate::runtime::scheduler::CurrentThread;
use crate::runtime::{context, EnterGuard, Handle};
use crate::task::JoinHandle;
use crate::util::error::RUNTIME_SHUTTING_DOWN_ERROR;
use crate::util::trace::SpawnMeta;

use std::future::Future;
use std::io;
use std::mem;
use std::time::Duration;

Expand Down Expand Up @@ -513,3 +515,43 @@ impl Drop for Runtime {
impl std::panic::UnwindSafe for Runtime {}

impl std::panic::RefUnwindSafe for Runtime {}

fn display_eq(d: impl std::fmt::Display, s: &str) -> bool {
use std::fmt::Write;

struct FormatEq<'r> {
remainder: &'r str,
unequal: bool,
}

impl<'r> Write for FormatEq<'r> {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
if !self.unequal {
if let Some(new_remainder) = self.remainder.strip_prefix(s) {
Comment thread
Keruspe marked this conversation as resolved.
self.remainder = new_remainder;
} else {
self.unequal = true;
}
}
Ok(())
}
}

let mut fmt_eq = FormatEq {
remainder: s,
unequal: false,
};
let _ = write!(fmt_eq, "{d}");
fmt_eq.remainder.is_empty() && !fmt_eq.unequal
}

/// Checks whether the given error was emitted by tokio when shutting down its runtime.
Comment thread
ADD-SP marked this conversation as resolved.
Outdated
pub fn is_rt_shutdown_err(err: &io::Error) -> bool {
if let Some(inner) = err.get_ref() {
err.kind() == io::ErrorKind::Other
&& inner.source().is_none()
&& display_eq(inner, RUNTIME_SHUTTING_DOWN_ERROR)
} else {
false
}
}