mirror of
https://github.com/microsoft/RustTraining.git
synced 2026-05-06 06:06:43 -04:00
Merge pull request #87 from rodrirejala/fixErrors
Fix race condition in "The Waker Contract" example (Fixes #85)
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user