Skip to content

Commit 5c8e9ad

Browse files
authored
Merge pull request #1758 from TheBlueMatt/2022-10-fix-pre-notified-future
Fix (and test) `Future` creation after a `Notifier` was notified
2 parents 4cd9901 + cf3471f commit 5c8e9ad

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

lightning/src/util/wakers.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl Notifier {
103103
Future {
104104
state: Arc::new(Mutex::new(FutureState {
105105
callbacks: Vec::new(),
106-
complete: false,
106+
complete: true,
107107
}))
108108
}
109109
} else if let Some(existing_state) = &lock.1 {
@@ -217,6 +217,20 @@ mod tests {
217217
use core::future::Future as FutureTrait;
218218
use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
219219

220+
#[test]
221+
fn notifier_pre_notified_future() {
222+
// Previously, if we generated a future after a `Notifier` had been notified, the future
223+
// would never complete. This tests this behavior, ensuring the future instead completes
224+
// immediately.
225+
let notifier = Notifier::new();
226+
notifier.notify();
227+
228+
let callback = Arc::new(AtomicBool::new(false));
229+
let callback_ref = Arc::clone(&callback);
230+
notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
231+
assert!(callback.load(Ordering::SeqCst));
232+
}
233+
220234
#[cfg(feature = "std")]
221235
#[test]
222236
fn test_wait_timeout() {

0 commit comments

Comments
 (0)