Skip to content

Commit

Permalink
time: prevent panicking in sleep() with large durations (#4495)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton authored Feb 15, 2022
1 parent 37917b8 commit 0826f76
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 17 deletions.
2 changes: 1 addition & 1 deletion tokio/src/time/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl ClockTime {
.unwrap_or_else(|| Duration::from_secs(0));
let ms = dur.as_millis();

ms.try_into().expect("Duration too far into the future")
ms.try_into().unwrap_or(u64::MAX)
}

pub(self) fn tick_to_duration(&self, t: u64) -> Duration {
Expand Down
16 changes: 0 additions & 16 deletions tokio/tests/time_sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,22 +235,6 @@ async fn long_sleeps() {
assert!(tokio::time::Instant::now() <= deadline + Duration::from_millis(1));
}

#[tokio::test]
#[should_panic(expected = "Duration too far into the future")]
async fn very_long_sleeps() {
tokio::time::pause();

// Some platforms (eg macos) can't represent times this far in the future
if let Some(deadline) = tokio::time::Instant::now().checked_add(Duration::from_secs(1u64 << 62))
{
tokio::time::sleep_until(deadline).await;
} else {
// make it pass anyway (we can't skip/ignore the test based on the
// result of checked_add)
panic!("Duration too far into the future (test ignored)")
}
}

#[tokio::test]
async fn reset_after_firing() {
let timer = tokio::time::sleep(std::time::Duration::from_millis(1));
Expand Down

0 comments on commit 0826f76

Please sign in to comment.