From 75c953bd639ee8630f678a02edd87e7814b04abc Mon Sep 17 00:00:00 2001 From: Weijia Jiang Date: Tue, 4 Jun 2024 15:45:35 +0800 Subject: [PATCH 1/2] time: fix big time panic issue (#6612) --- tokio/src/runtime/time/source.rs | 14 +++++++++++--- tokio/src/runtime/time/tests/mod.rs | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/tokio/src/runtime/time/source.rs b/tokio/src/runtime/time/source.rs index e371c207cdb..e3ba8d790c0 100644 --- a/tokio/src/runtime/time/source.rs +++ b/tokio/src/runtime/time/source.rs @@ -22,9 +22,11 @@ impl TimeSource { pub(crate) fn instant_to_tick(&self, t: Instant) -> u64 { // round up let dur: Duration = t.saturating_duration_since(self.start_time); - let ms = dur.as_millis(); - - ms.try_into().unwrap_or(MAX_SAFE_MILLIS_DURATION) + let ms = dur + .as_millis() + .try_into() + .unwrap_or(MAX_SAFE_MILLIS_DURATION); + ms.min(MAX_SAFE_MILLIS_DURATION) } pub(crate) fn tick_to_duration(&self, t: u64) -> Duration { @@ -34,4 +36,10 @@ impl TimeSource { pub(crate) fn now(&self, clock: &Clock) -> u64 { self.instant_to_tick(clock.now()) } + + #[cfg(test)] + #[allow(dead_code)] + pub(super) fn start_time(&self) -> Instant { + self.start_time + } } diff --git a/tokio/src/runtime/time/tests/mod.rs b/tokio/src/runtime/time/tests/mod.rs index 676cf55f9c6..0e453433691 100644 --- a/tokio/src/runtime/time/tests/mod.rs +++ b/tokio/src/runtime/time/tests/mod.rs @@ -267,3 +267,17 @@ fn poll_process_levels_targeted() { handle.process_at_time(0, 192); handle.process_at_time(0, 192); } + +#[test] +#[cfg(not(loom))] +fn instant_to_tick_max() { + use crate::runtime::time::entry::MAX_SAFE_MILLIS_DURATION; + + let rt = rt(true); + let handle = rt.handle().inner.driver().time(); + + let start_time = handle.time_source.start_time(); + let long_future = start_time + std::time::Duration::from_millis(MAX_SAFE_MILLIS_DURATION + 1); + + assert!(handle.time_source.instant_to_tick(long_future) <= MAX_SAFE_MILLIS_DURATION); +} From 8fca6f6dad209b9f6200202d8a46a9769f796ce7 Mon Sep 17 00:00:00 2001 From: Timo <39920115+tglane@users.noreply.github.com> Date: Tue, 4 Jun 2024 13:34:22 +0200 Subject: [PATCH 2/2] process: add `Command::as_std_mut` (#6608) --- tokio/src/process/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tokio/src/process/mod.rs b/tokio/src/process/mod.rs index 585dbdd6af6..fc768809f02 100644 --- a/tokio/src/process/mod.rs +++ b/tokio/src/process/mod.rs @@ -325,6 +325,12 @@ impl Command { &self.std } + /// Cheaply convert to a `&mut std::process::Command` for places where the type from the + /// standard library is expected. + pub fn as_std_mut(&mut self) -> &mut StdCommand { + &mut self.std + } + /// Adds an argument to pass to the program. /// /// Only one argument can be passed per use. So instead of: