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: positively check the deadline before reading the state of TimerEntry in Sleep #6548

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion tokio/src/runtime/time/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,6 @@ impl TimerEntry {
self.driver.driver().time()
}

#[cfg(all(tokio_unstable, feature = "tracing"))]
pub(crate) fn clock(&self) -> &super::Clock {
self.driver.driver().clock()
}
Expand Down
13 changes: 13 additions & 0 deletions tokio/src/time/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,19 @@ impl Sleep {
#[cfg(any(not(tokio_unstable), not(feature = "tracing")))]
let coop = ready!(crate::runtime::coop::poll_proceed(cx));

let time_source = &me.entry.driver().time_source();
let now_tick = time_source.now(me.entry.clock());
let deadline_tick = time_source.deadline_to_tick(me.entry.deadline());

// If the deadline has been reached, then we return here without checking the state of TimerEntry.
// This ensures that even if the state of TimerEntry is not maintained by the Timer Driver,
// it can expire as expected.
// See <https://github.com/tokio-rs/tokio/issues/6545>
if deadline_tick <= now_tick {
me.entry.cancel();
return Poll::Ready(Ok(()));
}

let result = me.entry.poll_elapsed(cx).map(move |r| {
coop.made_progress();
r
Expand Down
8 changes: 4 additions & 4 deletions tokio/tests/rt_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ fn worker_park_count() {
let rt = current_thread();
let metrics = rt.metrics();
rt.block_on(async {
time::sleep(Duration::from_millis(1)).await;
time::sleep(Duration::from_millis(100)).await;
});
drop(rt);
assert!(1 <= metrics.worker_park_count(0));

let rt = threaded();
let metrics = rt.metrics();
rt.block_on(async {
time::sleep(Duration::from_millis(1)).await;
time::sleep(Duration::from_millis(100)).await;
});
drop(rt);
assert!(1 <= metrics.worker_park_count(0));
Expand All @@ -160,15 +160,15 @@ fn worker_noop_count() {
let rt = current_thread();
let metrics = rt.metrics();
rt.block_on(async {
time::sleep(Duration::from_millis(1)).await;
time::sleep(Duration::from_millis(100)).await;
});
drop(rt);
assert!(0 < metrics.worker_noop_count(0));

let rt = threaded();
let metrics = rt.metrics();
rt.block_on(async {
time::sleep(Duration::from_millis(1)).await;
time::sleep(Duration::from_millis(100)).await;
});
drop(rt);
assert!(0 < metrics.worker_noop_count(0));
Expand Down
32 changes: 32 additions & 0 deletions tokio/tests/time_rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,35 @@ async fn timeout_value() {
assert!(res.is_err());
assert!(Instant::now() >= now + dur);
}

#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads
#[test]
fn ready_in_busy_loop() {
async fn never_pending() {}

let handle = std::thread::spawn(|| {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
tokio::spawn(async move {
let mut timer = tokio::time::interval(tokio::time::Duration::from_millis(2));
loop {
tokio::select! {
_= timer.tick() => {
break;
}
_ = never_pending() => {}
}
}
})
.await
.unwrap();
});
});

std::thread::sleep(std::time::Duration::from_secs(2));

assert!(handle.is_finished());
}