From c7280167db887bda9d6e6c009bf1c229268254d6 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Mon, 6 Jun 2022 09:40:40 +0200 Subject: [PATCH] sync: fix `will_wake` usage in `Notify` (#4751) --- tokio/src/sync/notify.rs | 2 +- tokio/tests/sync_notify.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/tokio/src/sync/notify.rs b/tokio/src/sync/notify.rs index 31a1df96653..2af9bacae80 100644 --- a/tokio/src/sync/notify.rs +++ b/tokio/src/sync/notify.rs @@ -856,7 +856,7 @@ impl Notified<'_> { // Update the waker, if necessary. if let Some(waker) = waker { let should_update = match w.waker.as_ref() { - Some(current_waker) => current_waker.will_wake(waker), + Some(current_waker) => !current_waker.will_wake(waker), None => true, }; if should_update { diff --git a/tokio/tests/sync_notify.rs b/tokio/tests/sync_notify.rs index 160a568dc40..4236a91d1a9 100644 --- a/tokio/tests/sync_notify.rs +++ b/tokio/tests/sync_notify.rs @@ -207,3 +207,21 @@ fn test_enable_consumes_permit() { let mut future2 = spawn(notify.notified()); future2.enter(|_, fut| assert!(!fut.enable())); } + +#[test] +fn test_waker_update() { + use futures::task::noop_waker; + use std::future::Future; + use std::task::Context; + + let notify = Notify::new(); + let mut future = spawn(notify.notified()); + + let noop = noop_waker(); + future.enter(|_, fut| assert_pending!(fut.poll(&mut Context::from_waker(&noop)))); + + assert_pending!(future.poll()); + notify.notify_one(); + + assert!(future.is_woken()); +}