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

Address issue 2473 #2587

Merged
merged 3 commits into from
Jul 26, 2020
Merged
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
28 changes: 18 additions & 10 deletions tokio/src/time/delay_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,22 @@ impl<T> DelayQueue<T> {
}
}

/// Removes the key fom the expired queue or the timer wheel
/// depending on its expiration status
///
/// # Panics
/// Panics if the key is not contained in the expired queue or the wheel
fn remove_key(&mut self, key: &Key) {
use crate::time::wheel::Stack;

// Special case the `expired` queue
if self.slab[key.index].expired {
self.expired.remove(&key.index, &mut self.slab);
} else {
self.wheel.remove(&key.index, &mut self.slab);
}
}

/// Removes the item associated with `key` from the queue.
///
/// There must be an item associated with `key`. The function returns the
Expand Down Expand Up @@ -456,15 +472,7 @@ impl<T> DelayQueue<T> {
/// # }
/// ```
pub fn remove(&mut self, key: &Key) -> Expired<T> {
use crate::time::wheel::Stack;

// Special case the `expired` queue
if self.slab[key.index].expired {
self.expired.remove(&key.index, &mut self.slab);
} else {
self.wheel.remove(&key.index, &mut self.slab);
}

self.remove_key(key);
let data = self.slab.remove(key.index);

Expired {
Expand Down Expand Up @@ -508,7 +516,7 @@ impl<T> DelayQueue<T> {
/// # }
/// ```
pub fn reset_at(&mut self, key: &Key, when: Instant) {
self.wheel.remove(&key.index, &mut self.slab);
self.remove_key(key);

// Normalize the deadline. Values cannot be set to expire in the past.
let when = self.normalize_deadline(when);
Expand Down
21 changes: 21 additions & 0 deletions tokio/tests/time_delay_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,27 @@ async fn reset_later_after_slot_starts() {
assert_eq!(entry, "foo");
}

#[tokio::test]
async fn reset_inserted_expired() {
time::pause();
let mut queue = task::spawn(DelayQueue::new());
let now = Instant::now();

let key = queue.insert_at("foo", now - ms(100));

// this causes the panic described in #2473
queue.reset_at(&key, now + ms(100));

assert_eq!(1, queue.len());

delay_for(ms(200)).await;

let entry = assert_ready_ok!(poll!(queue)).into_inner();
assert_eq!(entry, "foo");

assert_eq!(queue.len(), 0);
}

#[tokio::test]
async fn reset_earlier_after_slot_starts() {
time::pause();
Expand Down