Merge pull request #1758 from TheBlueMatt/2022-10-fix-pre-notified-future
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Fri, 7 Oct 2022 17:38:50 +0000 (17:38 +0000)
committerGitHub <noreply@github.com>
Fri, 7 Oct 2022 17:38:50 +0000 (17:38 +0000)
Fix (and test) `Future` creation after a `Notifier` was notified

lightning/src/util/wakers.rs

index 166771948fad62bc2f10078b5360fb67207f50f5..e55ba37ffd84c99ba19ddc625a5cfa7e9f23ed56 100644 (file)
@@ -103,7 +103,7 @@ impl Notifier {
                        Future {
                                state: Arc::new(Mutex::new(FutureState {
                                        callbacks: Vec::new(),
-                                       complete: false,
+                                       complete: true,
                                }))
                        }
                } else if let Some(existing_state) = &lock.1 {
@@ -217,6 +217,20 @@ mod tests {
        use core::future::Future as FutureTrait;
        use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
 
+       #[test]
+       fn notifier_pre_notified_future() {
+               // Previously, if we generated a future after a `Notifier` had been notified, the future
+               // would never complete. This tests this behavior, ensuring the future instead completes
+               // immediately.
+               let notifier = Notifier::new();
+               notifier.notify();
+
+               let callback = Arc::new(AtomicBool::new(false));
+               let callback_ref = Arc::clone(&callback);
+               notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
+               assert!(callback.load(Ordering::SeqCst));
+       }
+
        #[cfg(feature = "std")]
        #[test]
        fn test_wait_timeout() {