Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix using a concurrent `JoinHandle` from other threads (#14550)

Alex Crichtond5d7d7a8b08f
diff --git a/crates/wasmtime/src/runtime/component/concurrent/abort.rs b/crates/wasmtime/src/runtime/component/concurrent/abort.rs
index f32d3d2333..3c5b0a8427 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/abort.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/abort.rs
@@ -1,4 +1,4 @@
-use crate::try_mutex::TryMutex;
+use crate::sync::Mutex;
use alloc::sync::Arc;
use core::mem::{self, ManuallyDrop};
use core::pin::Pin;
@@ -14,12 +14,12 @@ use core::task::{Context, Poll, Waker};
/// connected to. A manual invocation of [`JoinHandle::abort`] is required to
/// affect the task.
pub struct JoinHandle {
- // Note that at this time the usage of this type within Wasmtime's async
- // implementation is not expected to ever expose the ability to expose
- // a situation where this lock can be contended. Everything's bound to the
- // store and this is largely just used to satisfy compiler bounds. Hence,
- // lock operations in this module all unwrap.
- state: Arc<TryMutex<JoinState>>,
+ // Note that this handle is `Send` and `Sync` and may be used from any
+ // thread, concurrently with the store's event loop polling or dropping the
+ // task on another thread. This lock is only ever held for short critical
+ // sections which don't run any external code (e.g. wakers are always
+ // invoked after the lock is released).
+ state: Arc<Mutex<JoinState>>,
}
enum JoinState {
@@ -57,29 +57,33 @@ impl JoinHandle {
/// await the result and destruction of the task that this is associated
/// with.
pub fn abort(&self) {
- let mut state = self.state.try_lock().expect("should not be contended");
+ let task = {
+ let mut state = self.state.lock();
- match &mut *state {
- // If this task is still running, then fall through to below to
- // transition it into the `AbortRequested` state. If present the
- // waker for the running task is notified to indicate that an abort
- // signal has been received.
- JoinState::Running {
- waiting_for_abort_signal,
- waiting_for_abort_to_complete,
- } => {
- if let Some(task) = waiting_for_abort_signal.take() {
- task.wake();
+ match &mut *state {
+ // If this task is still running, then transition it into the
+ // `AbortRequested` state. If present the waker for the running
+ // task is notified, after the lock is released, to indicate
+ // that an abort signal has been received.
+ JoinState::Running {
+ waiting_for_abort_signal,
+ waiting_for_abort_to_complete,
+ } => {
+ let task = waiting_for_abort_signal.take();
+ *state = JoinState::AbortRequested {
+ waiting_for_abort_to_complete: waiting_for_abort_to_complete.take(),
+ };
+ task
}
- *state = JoinState::AbortRequested {
- waiting_for_abort_to_complete: waiting_for_abort_to_complete.take(),
- };
+ // If this task has already been aborted or has completed,
+ // nothing is left to do.
+ JoinState::AbortRequested { .. } | JoinState::Complete => None,
}
+ };
- // If this task has already been aborted or has completed, nothing
- // is left to do.
- JoinState::AbortRequested { .. } | JoinState::Complete => {}
+ if let Some(task) = task {
+ task.wake();
}
}
@@ -91,7 +95,7 @@ impl JoinHandle {
F: Future,
{
let handle = JoinHandle {
- state: Arc::new(TryMutex::new(JoinState::Running {
+ state: Arc::new(Mutex::new(JoinState::Running {
waiting_for_abort_signal: None,
waiting_for_abort_to_complete: None,
})),
@@ -108,10 +112,7 @@ impl Future for JoinHandle {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
- let mut state = self
- .state
- .try_lock()
- .expect("this lock should not be contended");
+ let mut state = self.state.lock();
match &mut *state {
// If this task is running or still only has requested an abort,
// wait further for the task to get dropped.
@@ -122,7 +123,12 @@ impl Future for JoinHandle {
| JoinState::AbortRequested {
waiting_for_abort_to_complete,
} => {
- *waiting_for_abort_to_complete = Some(cx.waker().clone());
+ // Note that the previous waker, if any, is dropped after the
+ // lock is released to avoid running arbitrary code while the
+ // lock is held.
+ let prev = waiting_for_abort_to_complete.replace(cx.waker().clone());
+ drop(state);
+ drop(prev);
Poll::Pending
}
@@ -134,7 +140,7 @@ impl Future for JoinHandle {
struct JoinHandleFuture<F> {
future: ManuallyDrop<F>,
- state: Arc<TryMutex<JoinState>>,
+ state: Arc<Mutex<JoinState>>,
}
impl<F> Future for JoinHandleFuture<F>
@@ -155,13 +161,15 @@ where
// First, before polling the future, check to see if we've been
// aborted. If not register our task as awaiting such an abort.
{
- let mut state = state.try_lock().expect("this lock should not be contended");
+ let mut state = state.lock();
match &mut *state {
JoinState::Running {
waiting_for_abort_signal,
..
} => {
- *waiting_for_abort_signal = Some(cx.waker().clone());
+ let prev = waiting_for_abort_signal.replace(cx.waker().clone());
+ drop(state);
+ drop(prev);
}
JoinState::AbortRequested { .. } | JoinState::Complete => {
return Poll::Ready(None);
@@ -186,11 +194,10 @@ impl<F> Drop for JoinHandleFuture<F> {
}
// After the future dropped see if there was a task awaiting its
- // destruction. Simultaneously flag this state as complete.
- let prev = mem::replace(
- &mut *self.state.try_lock().expect("should not be contended"),
- JoinState::Complete,
- );
+ // destruction. Simultaneously flag this state as complete. Note that
+ // the previous state, and its wakers, are dropped/invoked after the
+ // lock is released.
+ let prev = mem::replace(&mut *self.state.lock(), JoinState::Complete);
let task = match prev {
JoinState::Running {
waiting_for_abort_to_complete,
diff --git a/crates/wasmtime/src/sync_nostd.rs b/crates/wasmtime/src/sync_nostd.rs
index a0fe267c26..5c94a7e773 100644
--- a/crates/wasmtime/src/sync_nostd.rs
+++ b/crates/wasmtime/src/sync_nostd.rs
@@ -134,6 +134,65 @@ impl Drop for OnceLockGuard<'_> {
}
}
+#[derive(Debug)]
+pub struct Mutex<T> {
+ val: UnsafeCell<T>,
+ lock: raw::Mutex,
+}
+
+unsafe impl<T: Send> Send for Mutex<T> {}
+unsafe impl<T: Send> Sync for Mutex<T> {}
+
+impl<T> Mutex<T> {
+ pub const fn new(val: T) -> Mutex<T> {
+ Mutex {
+ val: UnsafeCell::new(val),
+ lock: raw::Mutex::new(),
+ }
+ }
+
+ pub fn lock(&self) -> impl DerefMut<Target = T> + '_ {
+ self.lock.lock();
+ MutexGuard { lock: self }
+ }
+}
+
+impl<T: Default> Default for Mutex<T> {
+ fn default() -> Mutex<T> {
+ Mutex::new(T::default())
+ }
+}
+
+struct MutexGuard<'a, T> {
+ lock: &'a Mutex<T>,
+}
+
+impl<T> Deref for MutexGuard<'_, T> {
+ type Target = T;
+
+ fn deref(&self) -> &T {
+ // SAFETY: We hold the lock
+ unsafe { &*self.lock.val.get() }
+ }
+}
+
+impl<T> DerefMut for MutexGuard<'_, T> {
+ fn deref_mut(&mut self) -> &mut T {
+ // SAFETY: We hold the lock
+ unsafe { &mut *self.lock.val.get() }
+ }
+}
+
+impl<T> Drop for MutexGuard<'_, T> {
+ fn drop(&mut self) {
+ // SAFETY: This type represents the lock being held, so it's safe to
+ // perform the unlock here at the end.
+ unsafe {
+ self.lock.lock.unlock();
+ }
+ }
+}
+
#[derive(Debug)]
pub struct RwLock<T> {
val: UnsafeCell<T>,
@@ -421,6 +480,14 @@ mod tests {
assert_eq!(*lock.get_or_init(|| 1), 1);
}
+ #[test]
+ fn smoke_mutex() {
+ let lock = Mutex::new(1);
+ assert_eq!(*lock.lock(), 1);
+ *lock.lock() = 2;
+ assert_eq!(*lock.lock(), 2);
+ }
+
#[test]
fn smoke_rwlock() {
let lock = RwLock::new(1);
diff --git a/crates/wasmtime/src/sync_std.rs b/crates/wasmtime/src/sync_std.rs
index c2f7cd8cb7..0da67443c2 100644
--- a/crates/wasmtime/src/sync_std.rs
+++ b/crates/wasmtime/src/sync_std.rs
@@ -36,6 +36,30 @@ impl<T> Default for OnceLock<T> {
}
}
+/// Small wrapper around `std::sync::Mutex` which undoes poisoning.
+#[cfg_attr(
+ not(feature = "component-model-async"),
+ expect(dead_code, reason = "only used by component-model-async at this time")
+)]
+#[derive(Debug, Default)]
+pub struct Mutex<T>(std::sync::Mutex<T>);
+
+#[cfg_attr(
+ not(feature = "component-model-async"),
+ expect(dead_code, reason = "only used by component-model-async at this time")
+)]
+impl<T> Mutex<T> {
+ #[inline]
+ pub const fn new(val: T) -> Mutex<T> {
+ Mutex(std::sync::Mutex::new(val))
+ }
+
+ #[inline]
+ pub fn lock(&self) -> impl DerefMut<Target = T> + '_ {
+ self.0.lock().unwrap_or_else(|e| e.into_inner())
+ }
+}
+
/// Small wrapper around `std::sync::RwLock` which undoes poisoning.
#[derive(Debug, Default)]
pub struct RwLock<T>(std::sync::RwLock<T>);
diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rs
index eae7ca0012..03228b54af 100644
--- a/tests/all/component_model/async.rs
+++ b/tests/all/component_model/async.rs
@@ -1612,3 +1612,55 @@ fn inter_component_stream_is_not_intra_component() -> Result<()> {
Ok(())
}
+
+#[tokio::test]
+#[cfg_attr(miri, ignore)]
+async fn join_handle_used_from_other_threads() -> Result<()> {
+ use std::task::Waker;
+ use std::time::Duration;
+
+ let mut config = Config::new();
+ config.wasm_component_model_async(true);
+ let engine = Engine::new(&config)?;
+ let mut store = Store::new(&engine, ());
+
+ // Spawn some tasks and hand each `JoinHandle` to its own thread, which
+ // polls, aborts, and drops it while the event loop (below) is driving and
+ // dropping the same tasks. The two race, so this doesn't reproduce the
+ // underlying lock-contention bug on every run.
+ let remaining = Arc::new(AtomicUsize::new(4));
+ let mut threads = Vec::new();
+ for _ in 0..4 {
+ let mut handle = store.spawn(async |_| std::future::pending::<Result<()>>().await)?;
+ let remaining = remaining.clone();
+ threads.push(std::thread::spawn(move || {
+ let mut cx = Context::from_waker(Waker::noop());
+ assert!(Pin::new(&mut handle).poll(&mut cx).is_pending());
+ handle.abort();
+ // Wait for the event loop to drop the aborted task, racing it.
+ while Pin::new(&mut handle).poll(&mut cx).is_pending() {
+ std::hint::spin_loop();
+ }
+ remaining.fetch_sub(1, Ordering::Relaxed);
+ }));
+ }
+
+ // Drive the event loop until every thread is done, bounded by a timeout so
+ // a hang fails the test rather than blocking forever.
+ tokio::time::timeout(
+ Duration::from_secs(60),
+ store.run_concurrent(async |_| {
+ while remaining.load(Ordering::Relaxed) != 0 {
+ tokio::task::yield_now().await;
+ }
+ }),
+ )
+ .await
+ .expect("timed out driving the event loop")?;
+
+ for thread in threads {
+ thread.join().unwrap();
+ }
+
+ Ok(())
+}