Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 0 additions & 8 deletions tokio/src/time/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ impl Instant {
Instant { std }
}

pub(crate) fn far_future() -> Instant {
// Roughly 30 years from now.
// API does not provide a way to obtain max `Instant`
// or convert specific date in the future to instant.
// 1000 years overflows on macOS, 100 years overflows on FreeBSD.
Self::now() + Duration::from_secs(86400 * 365 * 30)
}

/// Convert the value into a `std::time::Instant`.
pub fn into_std(self) -> std::time::Instant {
self.std
Expand Down
8 changes: 3 additions & 5 deletions tokio/src/time/interval.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::time::{sleep_until, Duration, Instant, Sleep};
use crate::time::{safe_delay, sleep_until, Duration, Instant, Sleep};
use crate::util::trace;

use std::future::{poll_fn, Future};
Expand Down Expand Up @@ -139,7 +139,7 @@ fn internal_interval_at(

Interval {
delay,
period,
period: safe_delay(period),
missed_tick_behavior: MissedTickBehavior::default(),
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span,
Expand Down Expand Up @@ -479,9 +479,7 @@ impl Interval {
self.missed_tick_behavior
.next_timeout(timeout, now, self.period)
} else {
timeout
.checked_add(self.period)
.unwrap_or_else(Instant::far_future)
timeout + self.period
};

// When we arrive here, the internal delay returned `Poll::Ready`.
Expand Down
6 changes: 6 additions & 0 deletions tokio/src/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@
//! [`interval`]: crate::time::interval()
//! [`sleep`]: sleep()

fn safe_delay(duration: Duration) -> Duration {
// Roughly 30 years from now.
// 1000 years overflows on macOS, 100 years overflows on FreeBSD.
duration.min(Duration::from_secs(86400 * 365 * 30))
}

mod clock;
pub(crate) use self::clock::Clock;
cfg_test_util! {
Expand Down
14 changes: 3 additions & 11 deletions tokio/src/time/sleep.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::runtime::Timer;
use crate::time::{error::Error, Duration, Instant};
use crate::time::{error::Error, safe_delay, Duration, Instant};
use crate::util::trace;

use pin_project_lite::pin_project;
Expand Down Expand Up @@ -121,12 +121,8 @@ pub fn sleep_until(deadline: Instant) -> Sleep {
#[cfg_attr(docsrs, doc(alias = "wait"))]
#[track_caller]
pub fn sleep(duration: Duration) -> Sleep {
let location = trace::caller_location();

match Instant::now().checked_add(duration) {
Some(deadline) => Sleep::new_timeout(deadline, location),
None => Sleep::new_timeout(Instant::far_future(), location),
}
Comment on lines -126 to -129
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would prefer to keep the checked_add() logic, to always perform the addition correctly when at all possible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous code has no "correct" addition, even though it looks like it. This becomes clear when you inline Instant::far_future():

const SAFE_DELAY: Duration = Duration::from_secs(86400 * 365 * 30);
let deadline = Instant::now()
    .checked_add(duration)
    .unwrap_or_else(|| Instant::now().checked_add(SAFE_DELAY).unwrap());

This PR changes the above to:

const SAFE_DELAY: Duration = Duration::from_secs(86400 * 365 * 30);
let deadline = Instant::now().checked_add(duration.min(SAFE_DELAY)).unwrap();

let deadline = Instant::now() + safe_delay(duration);
Sleep::new_timeout(deadline, trace::caller_location())
}

pin_project! {
Expand Down Expand Up @@ -303,10 +299,6 @@ impl Sleep {
Sleep { inner, entry }
}

pub(crate) fn far_future(location: Option<&'static Location<'static>>) -> Sleep {
Self::new_timeout(Instant::far_future(), location)
}

/// Returns the instant at which the future will complete.
pub fn deadline(&self) -> Instant {
self.entry.deadline()
Expand Down
13 changes: 4 additions & 9 deletions tokio/src/time/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use crate::{
task::coop,
time::{error::Elapsed, sleep_until, Duration, Instant, Sleep},
time::{error::Elapsed, safe_delay, Duration, Instant, Sleep},
util::trace,
};

Expand Down Expand Up @@ -87,13 +87,8 @@ pub fn timeout<F>(duration: Duration, future: F) -> Timeout<F::IntoFuture>
where
F: IntoFuture,
{
let location = trace::caller_location();

let deadline = Instant::now().checked_add(duration);
let delay = match deadline {
Some(deadline) => Sleep::new_timeout(deadline, location),
None => Sleep::far_future(location),
};
let deadline = Instant::now() + safe_delay(duration);
let delay = Sleep::new_timeout(deadline, trace::caller_location());
Timeout::new_with_delay(future.into_future(), delay)
}

Expand Down Expand Up @@ -165,7 +160,7 @@ pub fn timeout_at<F>(deadline: Instant, future: F) -> Timeout<F::IntoFuture>
where
F: IntoFuture,
{
let delay = sleep_until(deadline);
let delay = Sleep::new_timeout(deadline, trace::caller_location());
Timeout::new_with_delay(future.into_future(), delay)
}

Expand Down
Loading