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

sync: fix panic in Notified::enable #4747

Merged
merged 3 commits into from Jun 5, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion tokio/src/sync/notify.rs
Expand Up @@ -855,7 +855,11 @@ impl Notified<'_> {
} else {
// Update the waker, if necessary.
if let Some(waker) = waker {
if !w.waker.as_ref().unwrap().will_wake(waker) {
let should_update = match w.waker.as_ref() {
Some(current_waker) => current_waker.will_wake(waker),
None => true,
};
if should_update {
w.waker = Some(waker.clone());
}
}
Expand Down
53 changes: 53 additions & 0 deletions tokio/tests/sync_notify.rs
Expand Up @@ -154,3 +154,56 @@ fn notify_one_after_dropped_all() {

assert_ready!(notified2.poll());
}

#[test]
fn test_notify_one_not_enabled() {
let notify = Notify::new();
let mut future = spawn(notify.notified());

notify.notify_one();
assert_ready!(future.poll());
}

#[test]
fn test_notify_one_after_enable() {
let notify = Notify::new();
let mut future = spawn(notify.notified());

future.enter(|_, fut| assert!(!fut.enable()));

notify.notify_one();
assert_ready!(future.poll());
future.enter(|_, fut| assert!(fut.enable()));
}

#[test]
fn test_poll_after_enable() {
let notify = Notify::new();
let mut future = spawn(notify.notified());

future.enter(|_, fut| assert!(!fut.enable()));
assert_pending!(future.poll());
}

#[test]
fn test_enable_after_poll() {
let notify = Notify::new();
let mut future = spawn(notify.notified());

assert_pending!(future.poll());
future.enter(|_, fut| assert!(!fut.enable()));
}

#[test]
fn test_enable_consumes_permit() {
let notify = Notify::new();

// Add a permit.
notify.notify_one();

let mut future1 = spawn(notify.notified());
future1.enter(|_, fut| assert!(fut.enable()));

let mut future2 = spawn(notify.notified());
future2.enter(|_, fut| assert!(!fut.enable()));
}