Skip to content

Commit 9d12c5c

Browse files
authored
Simplify Miri-compatible sleep in tests (#11566)
This test occasionally gets stuck infinitely on CI and I think it's due to a race with the manual state management so this commit simplifies the sleep implementation to use more standard primitives.
1 parent 389e36a commit 9d12c5c

File tree

1 file changed

+7
-41
lines changed

1 file changed

+7
-41
lines changed

tests/all/pulley.rs

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::Result;
22
use std::ptr::NonNull;
3+
use std::thread;
34
use wasmtime::component::{self, Component};
45
use wasmtime::{
56
Caller, Config, Engine, Func, FuncType, Instance, Module, Store, Trap, Val, ValType,
@@ -411,47 +412,12 @@ fn pulley_provenance_test_components() -> Result<()> {
411412
}
412413

413414
async fn sleep(duration: std::time::Duration) {
414-
// TODO: We should be able to use `tokio::time::sleep` here, but as of this
415-
// writing the miri-compatible version of `wasmtime-fiber` uses threads
416-
// behind the scenes, which means thread-local storage is not preserved when
417-
// we switch fibers, and that confuses Tokio. If we ever fix that we can
418-
// stop using our own, special version of `sleep` and switch back to the
419-
// Tokio version.
420-
421-
use std::{
422-
future,
423-
sync::{
424-
Arc, Mutex,
425-
atomic::{AtomicU32, Ordering::SeqCst},
426-
},
427-
task::Poll,
428-
thread,
429-
};
430-
431-
let state = Arc::new(AtomicU32::new(0));
432-
let waker = Arc::new(Mutex::new(None));
433-
future::poll_fn(move |cx| match state.load(SeqCst) {
434-
0 => {
435-
state.store(1, SeqCst);
436-
let state = state.clone();
437-
*waker.lock().unwrap() = Some(cx.waker().clone());
438-
let waker = waker.clone();
439-
thread::spawn(move || {
440-
thread::sleep(duration);
441-
state.store(2, SeqCst);
442-
let waker = waker.lock().unwrap().clone().unwrap();
443-
waker.wake();
444-
});
445-
Poll::Pending
446-
}
447-
1 => {
448-
*waker.lock().unwrap() = Some(cx.waker().clone());
449-
Poll::Pending
450-
}
451-
2 => Poll::Ready(()),
452-
_ => unreachable!(),
453-
})
454-
.await;
415+
let (tx, rx) = tokio::sync::oneshot::channel();
416+
thread::spawn(move || {
417+
thread::sleep(duration);
418+
tx.send(()).unwrap();
419+
});
420+
rx.await.unwrap()
455421
}
456422

457423
#[tokio::test]

0 commit comments

Comments
 (0)