Skip to content

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

Enable multiple concurrent sync host calls from different threads in the same task (#12735)

Sy Brand35887491ca4c
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index 1740522c73..bdd3579ee4 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -785,14 +785,14 @@ pub(crate) fn poll_and_block<R: Send + Sync + 'static>(
// It did not complete immediately; add it to
// `ConcurrentState::futures` so it will be polled via the event loop;
- // then use `GuestTask::sync_call_set` to wait for the task to
+ // then use `GuestThread::sync_call_set` to wait for the task to
// complete, suspending the current fiber until it does so.
Poll::Pending => {
let state = store.concurrent_state_mut();
state.push_future(future);
let caller = state.get_mut(task)?.caller;
- let set = state.get_mut(caller.task)?.sync_call_set;
+ let set = state.get_mut(caller.thread)?.sync_call_set;
Waitable::Host(task).join(state, Some(set))?;
store.suspend(SuspendReason::Waiting {
@@ -1464,7 +1464,6 @@ impl StoreOpaque {
debug_assert_eq!(instance, guest_caller);
}
let task = GuestTask::new(
- state,
Box::new(move |_, _| bail_bug!("cannot lower params in sync call")),
LiftResult {
lift: Box::new(move |_, _| bail_bug!("cannot lift result in sync call")),
@@ -1487,7 +1486,7 @@ impl StoreOpaque {
)?;
let guest_task = state.push(task)?;
- let new_thread = GuestThread::new_implicit(guest_task);
+ let new_thread = GuestThread::new_implicit(state, guest_task)?;
let guest_thread = state.push(new_thread)?;
Instance::from_wasmtime(self, callee.instance).add_guest_thread_to_instance_table(
guest_thread,
@@ -1879,7 +1878,7 @@ impl StoreOpaque {
let state = self.concurrent_state_mut();
let caller = state.current_guest_thread()?;
let old_set = waitable.common(state)?.set;
- let set = state.get_mut(caller.task)?.sync_call_set;
+ let set = state.get_mut(caller.thread)?.sync_call_set;
waitable.join(state, Some(set))?;
self.suspend(SuspendReason::Waiting {
set,
@@ -2075,14 +2074,24 @@ impl Instance {
guest_thread: QualifiedThreadId,
runtime_instance: RuntimeComponentInstanceIndex,
) -> Result<()> {
- let guest_id = match store
- .concurrent_state_mut()
- .get_mut(guest_thread.thread)?
- .instance_rep
- {
+ let state = store.concurrent_state_mut();
+ let thread_data = state.get_mut(guest_thread.thread)?;
+ let guest_id = match thread_data.instance_rep {
Some(id) => id,
None => bail_bug!("thread must have instance_rep set by now"),
};
+ let sync_call_set = thread_data.sync_call_set;
+
+ // Clean up any pending subtasks in the sync_call_set
+ for waitable in mem::take(&mut state.get_mut(sync_call_set)?.ready) {
+ if let Some(Event::Subtask {
+ status: Status::Returned | Status::ReturnCancelled,
+ }) = waitable.common(state)?.event
+ {
+ waitable.delete_from(state)?;
+ }
+ }
+
store
.instance_state(RuntimeInstance {
instance: self.id().instance(),
@@ -2092,6 +2101,7 @@ impl Instance {
.guest_thread_remove(guest_id)?;
store.concurrent_state_mut().delete(guest_thread.thread)?;
+ store.concurrent_state_mut().delete(sync_call_set)?;
let task = store.concurrent_state_mut().get_mut(guest_thread.task)?;
task.threads.remove(&guest_thread.thread);
Ok(())
@@ -2437,7 +2447,6 @@ impl Instance {
);
let new_task = GuestTask::new(
- state,
Box::new(move |store, dst| {
let mut store = token.as_context_mut(store);
assert!(dst.len() <= MAX_FLAT_PARAMS);
@@ -2542,7 +2551,7 @@ impl Instance {
)?;
let guest_task = state.push(new_task)?;
- let new_thread = GuestThread::new_implicit(guest_task);
+ let new_thread = GuestThread::new_implicit(state, guest_task)?;
let guest_thread = state.push(new_thread)?;
state.get_mut(guest_task)?.threads.insert(guest_thread);
@@ -2660,11 +2669,11 @@ impl Instance {
let state = store.0.concurrent_state_mut();
- // Use the caller's `GuestTask::sync_call_set` to register interest in
+ // Use the caller's `GuestThread::sync_call_set` to register interest in
// the subtask...
let guest_waitable = Waitable::Guest(guest_thread.task);
let old_set = guest_waitable.common(state)?.set;
- let set = state.get_mut(caller.task)?.sync_call_set;
+ let set = state.get_mut(caller.thread)?.sync_call_set;
guest_waitable.join(state, Some(set))?;
// ... and suspend this fiber temporarily while we wait for it to start.
@@ -3281,7 +3290,7 @@ impl Instance {
let current_thread = state.current_guest_thread()?;
let parent_task = current_thread.task;
- let new_thread = GuestThread::new_explicit(parent_task, start_func);
+ let new_thread = GuestThread::new_explicit(state, parent_task, start_func)?;
let thread_id = state.push(new_thread)?;
state.get_mut(parent_task)?.threads.insert(thread_id);
@@ -4339,6 +4348,8 @@ pub struct GuestThread {
/// The index of this thread in the component instance's handle table.
/// This must always be `Some` after initialization.
instance_rep: Option<u32>,
+ /// Scratch waitable set used to watch subtasks during synchronous calls.
+ sync_call_set: TableId<WaitableSet>,
}
impl GuestThread {
@@ -4355,29 +4366,34 @@ impl GuestThread {
Ok(TableId::new(rep))
}
- fn new_implicit(parent_task: TableId<GuestTask>) -> Self {
- Self {
+ fn new_implicit(state: &mut ConcurrentState, parent_task: TableId<GuestTask>) -> Result<Self> {
+ let sync_call_set = state.push(WaitableSet::default())?;
+ Ok(Self {
context: [0; 2],
parent_task,
wake_on_cancel: None,
state: GuestThreadState::NotStartedImplicit,
instance_rep: None,
- }
+ sync_call_set,
+ })
}
fn new_explicit(
+ state: &mut ConcurrentState,
parent_task: TableId<GuestTask>,
start_func: Box<
dyn FnOnce(&mut dyn VMStore, QualifiedThreadId) -> Result<()> + Send + Sync,
>,
- ) -> Self {
- Self {
+ ) -> Result<Self> {
+ let sync_call_set = state.push(WaitableSet::default())?;
+ Ok(Self {
context: [0; 2],
parent_task,
wake_on_cancel: None,
state: GuestThreadState::NotStartedExplicit(start_func),
instance_rep: None,
- }
+ sync_call_set,
+ })
}
}
@@ -4442,8 +4458,6 @@ pub(crate) struct GuestTask {
/// Whether or not we've sent a `Status::Starting` event to any current or
/// future waiters for this waitable.
starting_sent: bool,
- /// Scratch waitable set used to watch subtasks during synchronous calls.
- sync_call_set: TableId<WaitableSet>,
/// The runtime instance to which the exported function for this guest task
/// belongs.
///
@@ -4501,7 +4515,6 @@ impl GuestTask {
}
fn new(
- state: &mut ConcurrentState,
lower_params: RawLower,
lift_result: LiftResult,
caller: Caller,
@@ -4509,7 +4522,6 @@ impl GuestTask {
instance: RuntimeInstance,
async_function: bool,
) -> Result<Self> {
- let sync_call_set = state.push(WaitableSet::default())?;
let host_future_state = match &caller {
Caller::Guest { .. } => HostFutureState::NotApplicable,
Caller::Host {
@@ -4534,7 +4546,6 @@ impl GuestTask {
sync_result: SyncResult::NotProduced,
cancel_sent: false,
starting_sent: false,
- sync_call_set,
instance,
event: None,
exited: false,
@@ -4544,24 +4555,9 @@ impl GuestTask {
})
}
- /// Dispose of this guest task, reparenting any pending subtasks to the
- /// caller.
- fn dispose(self, state: &mut ConcurrentState) -> Result<()> {
- // If there are not-yet-delivered completion events for subtasks in
- // `self.sync_call_set`, recursively dispose of those subtasks as well.
- for waitable in mem::take(&mut state.get_mut(self.sync_call_set)?.ready) {
- if let Some(Event::Subtask {
- status: Status::Returned | Status::ReturnCancelled,
- }) = waitable.common(state)?.event
- {
- waitable.delete_from(state)?;
- }
- }
-
+ /// Dispose of this guest task.
+ fn dispose(self, _state: &mut ConcurrentState) -> Result<()> {
assert!(self.threads.is_empty());
-
- state.delete(self.sync_call_set)?;
-
Ok(())
}
}
@@ -5346,7 +5342,6 @@ pub(crate) fn prepare_call<T, R>(
};
let caller = state.current_thread;
let task = GuestTask::new(
- state,
Box::new(for_any_lower(move |store, params| {
lower_params(handle, token.as_context_mut(store), params)
})),
@@ -5378,7 +5373,8 @@ pub(crate) fn prepare_call<T, R>(
)?;
let task = state.push(task)?;
- let thread = state.push(GuestThread::new_implicit(task))?;
+ let new_thread = GuestThread::new_implicit(state, task)?;
+ let thread = state.push(new_thread)?;
state.get_mut(task)?.threads.insert(thread);
if !store.0.may_enter(instance)? {
diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rs
index 0c31867080..c11c89b7c5 100644
--- a/tests/all/component_model/async.rs
+++ b/tests/all/component_model/async.rs
@@ -1177,3 +1177,93 @@ async fn stream_cancel_read_async_does_not_corrupt_state() -> Result<()> {
}
}
}
+
+/// Regression test: multiple threads may concurrently make a synchronous
+/// call into the same async host function without corrupting state.
+///
+/// Bug: waitable sets for host calls used to be shared across all threads, so if two threads
+/// called a sync-lowered async host function concurrently, the waitable set state got overwritten.
+#[tokio::test]
+#[cfg_attr(miri, ignore)]
+async fn concurrent_sync_calls_to_async_host() -> Result<()> {
+ _ = env_logger::try_init();
+
+ let mut config = Config::new();
+ config.wasm_component_model_async(true);
+ config.wasm_component_model_async_builtins(true);
+ config.wasm_component_model_async_stackful(true);
+ config.wasm_component_model_threading(true);
+ let engine = Engine::new(&config)?;
+ let mut store = Store::new(&engine, 0);
+
+ let component = Component::new(
+ &engine,
+ r#"(component
+ (import "await-three-calls" (func $await-three-calls async))
+
+ (core module $libc
+ (table (export "__indirect_function_table") 1 funcref))
+
+ (core module $m
+ (import "" "await-three-calls" (func $await-three-calls))
+ (import "" "thread.new-indirect" (func $thread-new-indirect (param i32 i32) (result i32)))
+ (import "" "thread.unsuspend" (func $thread-unsuspend (param i32)))
+ (import "libc" "__indirect_function_table" (table $indirect-function-table 1 funcref))
+
+ (func (export "run")
+ (call $thread-new-indirect (i32.const 0) (i32.const 0))
+ (call $thread-unsuspend)
+ (call $thread-new-indirect (i32.const 0) (i32.const 0))
+ (call $thread-unsuspend)
+ (call $await-three-calls)
+ )
+ (func $thread-entry (param i32)
+ (call $await-three-calls)
+ )
+ (elem (table $indirect-function-table) (i32.const 0) func $thread-entry)
+ )
+ ;; Instantiate the libc module to get the table
+ (core instance $libc (instantiate $libc))
+ ;; Get access to `thread.new-indirect` that uses the table from libc
+ (core type $start-func-ty (func (param i32)))
+ (alias core export $libc "__indirect_function_table" (core table $indirect-function-table))
+ (core func $thread-new-indirect
+ (canon thread.new-indirect $start-func-ty (table $indirect-function-table)))
+ (core func $thread-unsuspend (canon thread.unsuspend))
+
+ (core func $await-three-calls (canon lower (func $await-three-calls) ))
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "await-three-calls" (func $await-three-calls))
+ (export "thread.new-indirect" (func $thread-new-indirect))
+ (export "thread.unsuspend" (func $thread-unsuspend))
+ ))
+ (with "libc" (instance $libc))
+ ))
+ (func (export "run") async
+ (canon lift (core func $i "run")))
+ )"#,
+ )?;
+
+ let mut linker = Linker::<i32>::new(&engine);
+ linker
+ .root()
+ .func_wrap_concurrent("await-three-calls", |accessor, (): ()| {
+ Box::pin(async move {
+ accessor.with(|mut s| {
+ *s.data_mut() += 1;
+ });
+ while accessor.with(|mut s| *s.data_mut()) < 3 {
+ tokio::task::yield_now().await;
+ }
+ Ok(())
+ })
+ })?;
+ let instance = linker.instantiate_async(&mut store, &component).await?;
+ let func = instance.get_typed_func::<(), ()>(&mut store, "run")?;
+ func.call_async(&mut store, ()).await?;
+
+ store.assert_concurrent_state_empty();
+
+ Ok(())
+}