Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: document auto-advancing behavior of runtime #3763

Merged
merged 3 commits into from
Jun 16, 2021
Merged
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
21 changes: 21 additions & 0 deletions tokio/src/time/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ cfg_test_util! {
///
/// Panics if time is already frozen or if called from outside of a
/// `current_thread` Tokio runtime.
///
/// # Auto-advance
///
/// If time is paused, when the runtime has no work to do, the clock is
/// auto-advanced to the closest pending timer. This can cause some
/// confusion, because if a [`Sleep`] (or other timer-backed primitive) is
/// `.await`ed, the timer is set forward unexpectedly. However, this
/// behavior is necessary because there could be situations in which the
/// runtime hangs because the clock never gets advanced, so a timer never
/// resolves.
eb-64-64 marked this conversation as resolved.
Show resolved Hide resolved
///
/// [`Sleep`]: crate::time::Sleep
pub fn pause() {
let clock = clock().expect("time cannot be frozen from outside the Tokio runtime");
clock.pause();
Expand Down Expand Up @@ -119,11 +131,20 @@ cfg_test_util! {
///
/// Panics if time is not frozen or if called from outside of the Tokio
/// runtime.
///
/// # Auto-advance
///
/// If the time is paused and there is no work to do, the runtime advances
/// time to the next-closest timer, which may cause confusion. See
/// [`pause`](pause#auto-advance) for more details.
eb-64-64 marked this conversation as resolved.
Show resolved Hide resolved
pub async fn advance(duration: Duration) {
let clock = clock().expect("time cannot be frozen from outside the Tokio runtime");
let until = clock.now() + duration;
clock.advance(duration);

// Prevent the runtime from advancing the clock to the next pending
// timer when parking, which likely will advance it too far (see
// https://github.com/tokio-rs/tokio/pull/3712)
crate::time::sleep_until(until).await;
}

Expand Down