Merge pull request #87 from rodrirejala/fixErrors

Fix race condition in "The Waker Contract" example (Fixes #85)
This commit is contained in:
atulkhare4096
2026-04-11 08:47:33 -07:00
committed by GitHub
+7 -2
View File
@@ -120,12 +120,12 @@ impl Future for Delay {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
// Check if already completed
// Check if already completed before storing waker
if *self.completed.lock().unwrap() {
return Poll::Ready(());
}
// Store the waker so the background thread can wake us
// Store the waker - executor may pass a new one on each poll
*self.waker_stored.lock().unwrap() = Some(cx.waker().clone());
// Start the background timer on first poll
@@ -146,6 +146,11 @@ impl Future for Delay {
});
}
// Double-check completion after storing waker (handles race condition)
if *self.completed.lock().unwrap() {
return Poll::Ready(());
}
Poll::Pending // Not done yet
}
}