Skip to content

Commit cf3471f

Browse files
committed
Fix (and test) Future creation after a Notifier was notified
After a `Notifier` has been `notify`'d, attempts to `get_future` should return a `Future` which is pre-completed, however this was not the case. This commit simply fixes the behavior, adding a test to demonstrate the issue.
1 parent 7544030 commit cf3471f

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)