Skip to content

Commit 24344df

Browse files
authored
time: fix race condition leading to lost timers (#6683)
1 parent 14c17fc commit 24344df

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

tokio/src/runtime/time/mod.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,13 @@ impl Driver {
190190
assert!(!handle.is_shutdown());
191191

192192
// Finds out the min expiration time to park.
193-
let expiration_time = (0..rt_handle.time().inner.get_shard_size())
194-
.filter_map(|id| {
195-
let lock = rt_handle.time().inner.lock_sharded_wheel(id);
196-
lock.next_expiration_time()
197-
})
193+
let locks = (0..rt_handle.time().inner.get_shard_size())
194+
.map(|id| rt_handle.time().inner.lock_sharded_wheel(id))
195+
.collect::<Vec<_>>();
196+
197+
let expiration_time = locks
198+
.iter()
199+
.filter_map(|lock| lock.next_expiration_time())
198200
.min();
199201

200202
rt_handle
@@ -203,6 +205,9 @@ impl Driver {
203205
.next_wake
204206
.store(next_wake_time(expiration_time));
205207

208+
// Safety: After updating the `next_wake`, we drop all the locks.
209+
drop(locks);
210+
206211
match expiration_time {
207212
Some(when) => {
208213
let now = handle.time_source.now(rt_handle.clock());

0 commit comments

Comments
 (0)