Skip to content

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

Fix spurious `call_async` guests running (#14825)

Alex Crichton68498946e7ef
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index bdb4e4ce5c..c4e7c48699 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -1303,13 +1303,7 @@ impl<T> StoreContextMut<'_, T> {
trap_on_idle: bool,
) -> Result<R> {
debug_assert!(self.0.concurrency_support());
- let already_running = self
- .0
- .concurrent_state_mut_already_forced_current_thread()
- .event_loop_running;
- if already_running {
- bail!("Recursive `StoreContextMut::run_concurrent` calls not supported")
- }
+ self.0.check_event_loop_not_running()?;
let token = StoreToken::new(self.as_context_mut());
struct Dropper<'a, T: 'static, V> {
@@ -1807,6 +1801,16 @@ impl<T> StoreContextMut<'_, T> {
pub type EnteredHostTask = Option<QualifiedThreadId>;
impl StoreOpaque {
+ pub(crate) fn check_event_loop_not_running(&mut self) -> Result<()> {
+ if self
+ .concurrent_state_mut_already_forced_current_thread()
+ .event_loop_running
+ {
+ bail!("Recursive `StoreContextMut::run_concurrent` calls not supported")
+ }
+ Ok(())
+ }
+
/// Returns the currently-running thread, promoting any deferred lazy guest
/// thread into a fully-materialized `CurrentThread`. Deferred [`HostTask`]s
/// are not materialized.
diff --git a/crates/wasmtime/src/runtime/component/concurrent/func.rs b/crates/wasmtime/src/runtime/component/concurrent/func.rs
index 31285d1219..70998bed8c 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/func.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/func.rs
@@ -234,6 +234,8 @@ where
Return: 'static,
{
let mut store = store.as_context_mut();
+ // See `Func::call_async` for why this is checked up front.
+ store.0.check_event_loop_not_running()?;
let ptr = SendSyncPtr::from(NonNull::from(&params).cast::<u8>());
let prepared = self.prepare_call(store.as_context_mut(), move |cx, ty, dst| {
// SAFETY: The goal here is to get `Params`, a non-`'static`
diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rs
index e758653311..2f27b75edf 100644
--- a/crates/wasmtime/src/runtime/component/func.rs
+++ b/crates/wasmtime/src/runtime/component/func.rs
@@ -295,6 +295,10 @@ impl Func {
#[cfg(feature = "component-model-async")]
if store.0.concurrency_support() {
+ // Fail before the call is staged if the event loop is already
+ // running, since otherwise the staged call would still run later
+ // despite this call returning an error.
+ store.0.check_event_loop_not_running()?;
let call = self.start_call_concurrent(&mut store, params, results)?;
return store
.run_concurrent_trap_on_idle(async |store| {
diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rs
index e0601afeb6..f242d9d780 100644
--- a/tests/all/component_model/async.rs
+++ b/tests/all/component_model/async.rs
@@ -3429,64 +3429,49 @@ async fn task_group_hook_fails_to_enter_call_async() -> Result<()> {
Ok(())
}
-/// `call_async` from within a host function, while the event loop is already
-/// running, fails, and dropping that call's future (whose start has already
-/// been queued) doesn't panic. The dropped call fails once the event loop
-/// reaches it, here in the next call.
+/// `Func::call_async` and `TypedFunc::call_async` from within a host function,
+/// while the event loop is already running, fail without queueing the call to
+/// run later.
#[tokio::test]
#[cfg_attr(miri, ignore)]
async fn call_async_from_host_function() -> Result<()> {
use futures::FutureExt as _;
- let engine = Engine::new(Config::new().wasm_component_model_async(true))?;
+ let engine = Engine::default();
let component = Component::new(
&engine,
r#"(component
(import "host" (func $host))
- (component $Nop
- (core func $task.return (canon task.return))
- (core module $m
- (import "" "task.return" (func $task.return))
- (func (export "nop") (result i32)
- (call $task.return)
- (i32.const 0 (; EXIT ;)))
- (func (export "cb") (param i32 i32 i32) (result i32) unreachable))
- (core instance $i (instantiate $m
- (with "" (instance (export "task.return" (func $task.return))))))
- (func (export "nop") async
- (canon lift (core func $i "nop") async (callback (core func $i "cb")))))
- (instance $nop (instantiate $Nop))
(core func $host (canon lower (func $host)))
(core module $m
(import "" "host" (func $host))
- (func (export "run") (call $host)))
+ (func (export "run") (call $host))
+ (func (export "trap") unreachable))
(core instance $i (instantiate $m (with "" (instance (export "host" (func $host))))))
- (export "nop" (func $nop "nop"))
(func (export "run") (canon lift (core func $i "run")))
+ (func (export "trap") (canon lift (core func $i "trap")))
)"#,
)?;
- let nop = Arc::new(Mutex::new(None::<TypedFunc<(), ()>>));
+ let trap = Arc::new(Mutex::new(None::<Func>));
let mut linker = Linker::new(&engine);
linker.root().func_wrap("host", {
- let nop = nop.clone();
+ let trap = trap.clone();
move |mut store, ()| {
- let nop = nop.lock().unwrap().unwrap();
- let result = nop.call_async(&mut store, ()).now_or_never();
+ let trap = trap.lock().unwrap().unwrap();
+ let result = trap.call_async(&mut store, &[], &mut []).now_or_never();
+ assert!(matches!(result, Some(Err(_))));
+ let trap = trap.typed::<(), ()>(&store)?;
+ let result = trap.call_async(&mut store, ()).now_or_never();
assert!(matches!(result, Some(Err(_))));
Ok(())
}
})?;
let mut store = Store::new(&engine, ());
let instance = linker.instantiate_async(&mut store, &component).await?;
- *nop.lock().unwrap() = Some(instance.get_typed_func::<(), ()>(&mut store, "nop")?);
+ *trap.lock().unwrap() = instance.get_func(&mut store, "trap");
let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;
run.call_async(&mut store, ()).await?;
- let nop = nop.lock().unwrap().unwrap();
- let err = nop.call_async(&mut store, ()).await.unwrap_err();
- assert!(
- format!("{err:?}").contains("dropped before it started"),
- "unexpected error: {err:?}"
- );
+ run.call_async(&mut store, ()).await?;
Ok(())
}