Skip to content

Commit 2a54ad0

Browse files
authored
time: do not overflow to signal value (#5710)
1 parent cb18b0a commit 2a54ad0

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

tokio/src/runtime/time/entry.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ type TimerResult = Result<(), crate::time::error::Error>;
7272
const STATE_DEREGISTERED: u64 = u64::MAX;
7373
const STATE_PENDING_FIRE: u64 = STATE_DEREGISTERED - 1;
7474
const STATE_MIN_VALUE: u64 = STATE_PENDING_FIRE;
75+
/// The largest safe integer to use for ticks.
76+
///
77+
/// This value should be updated if any other signal values are added above.
78+
pub(super) const MAX_SAFE_MILLIS_DURATION: u64 = u64::MAX - 2;
7579

7680
/// This structure holds the current shared state of the timer - its scheduled
7781
/// time (if registered), or otherwise the result of the timer completing, as
@@ -126,7 +130,7 @@ impl StateCell {
126130
fn when(&self) -> Option<u64> {
127131
let cur_state = self.state.load(Ordering::Relaxed);
128132

129-
if cur_state == u64::MAX {
133+
if cur_state == STATE_DEREGISTERED {
130134
None
131135
} else {
132136
Some(cur_state)

tokio/src/runtime/time/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
mod entry;
1010
pub(crate) use entry::TimerEntry;
11-
use entry::{EntryList, TimerHandle, TimerShared};
11+
use entry::{EntryList, TimerHandle, TimerShared, MAX_SAFE_MILLIS_DURATION};
1212

1313
mod handle;
1414
pub(crate) use self::handle::Handle;

tokio/src/runtime/time/source.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::MAX_SAFE_MILLIS_DURATION;
12
use crate::time::{Clock, Duration, Instant};
23

34
/// A structure which handles conversion from Instants to u64 timestamps.
@@ -25,7 +26,7 @@ impl TimeSource {
2526
.unwrap_or_else(|| Duration::from_secs(0));
2627
let ms = dur.as_millis();
2728

28-
ms.try_into().unwrap_or(u64::MAX)
29+
ms.try_into().unwrap_or(MAX_SAFE_MILLIS_DURATION)
2930
}
3031

3132
pub(crate) fn tick_to_duration(&self, t: u64) -> Duration {

tokio/tests/time_sleep.rs

+14
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,20 @@ async fn exactly_max() {
267267
time::sleep(ms(MAX_DURATION)).await;
268268
}
269269

270+
#[tokio::test]
271+
async fn issue_5183() {
272+
time::pause();
273+
274+
let big = std::time::Duration::from_secs(u64::MAX / 10);
275+
// This is a workaround since awaiting sleep(big) will never finish.
276+
#[rustfmt::skip]
277+
tokio::select! {
278+
biased;
279+
_ = tokio::time::sleep(big) => {}
280+
_ = tokio::time::sleep(std::time::Duration::from_nanos(1)) => {}
281+
}
282+
}
283+
270284
#[tokio::test]
271285
async fn no_out_of_bounds_close_to_max() {
272286
time::pause();

0 commit comments

Comments
 (0)