devices: Fix racy event_loop_test
It is inherently racy to wait on a Condvar without first checking if the
condition is already satisfied. Additionally, Condvar::wait can
spuriously return early even when the condition is not satisfied so it
is necessary to check it in a loop.
BUG=b:190444698
TEST=cargo test
Change-Id: If7703c6e8b9015df3c463147f3ad2a1c10c3a7ed
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2944323
Auto-Submit: Chirantan Ekbote <chirantan@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
diff --git a/devices/src/utils/event_loop.rs b/devices/src/utils/event_loop.rs
index f99d18a..893e4eb 100644
--- a/devices/src/utils/event_loop.rs
+++ b/devices/src/utils/event_loop.rs
@@ -239,7 +239,12 @@
)
.unwrap();
self_evt.write(1).unwrap();
- let _ = h.cvar.wait(h.val.lock().unwrap()).unwrap();
+ {
+ let mut val = h.val.lock().unwrap();
+ while *val < 1 {
+ val = h.cvar.wait(val).unwrap();
+ }
+ }
l.stop();
j.join().unwrap();
assert_eq!(*(h.val.lock().unwrap()), 1);