Skip to content

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

error instead of panicking when `call` used to call async-lifted export (#14737)

Alex Crichton4c8d4f689538
diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rs
index ae506a737a..9595f102dc 100644
--- a/crates/wasmtime/src/runtime/component/func.rs
+++ b/crates/wasmtime/src/runtime/component/func.rs
@@ -247,6 +247,8 @@ impl Func {
/// * The function calls a host function which returns an error
/// * The `store` used requires the use of [`Func::call_async`] instead. See
/// [store documentation](crate#async) for more information.
+ /// * If the function is async-lifted, in which case either
+ /// [`Self::call_async`] or [`Self::call_concurrent`] must be used.
///
/// See [`TypedFunc::call`] for more information in addition to
/// [`wasmtime::Func::call`](crate::Func::call).
@@ -342,9 +344,9 @@ impl Func {
self.check_params_results(store.as_context_mut(), params, results)?;
if self.abi_async(store.0) {
- unreachable!(
- "async-lifted exports should have failed validation \
- when `component-model-async` feature disabled"
+ bail!(
+ "cannot synchronously call an async-lifted export; \
+ use `call_async` or `call_concurrent` instead"
);
}
diff --git a/crates/wasmtime/src/runtime/component/func/typed.rs b/crates/wasmtime/src/runtime/component/func/typed.rs
index d0ccbf59aa..597afcfe0f 100644
--- a/crates/wasmtime/src/runtime/component/func/typed.rs
+++ b/crates/wasmtime/src/runtime/component/func/typed.rs
@@ -126,6 +126,8 @@ where
/// instance is currently calling a host function.
/// * If `store` requires using [`Self::call_async`] instead, see
/// [crate documentation](crate#async) for more info.
+ /// * If the function is async-lifted, in which case either
+ /// [`Self::call_async`] or [`Self::call_concurrent`] must be used.
///
/// In general there are many ways that things could go wrong when copying
/// types in and out of a wasm module with the canonical ABI, and certain
@@ -208,7 +210,10 @@ where
let mut store = store.as_context_mut();
if self.func.abi_async(store.0) {
- bail!("must enable the `component-model-async` feature to call async-lifted exports")
+ bail!(
+ "cannot synchronously call an async-lifted export; \
+ use `call_async` or `call_concurrent` instead"
+ );
}
// Note that this is in theory simpler than it might read at this time.
diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rs
index cb07a798b0..5929405601 100644
--- a/tests/all/component_model/async.rs
+++ b/tests/all/component_model/async.rs
@@ -2101,6 +2101,55 @@ async fn resource_drop_call_hook_error() -> Result<()> {
Ok(())
}
+#[test]
+#[cfg_attr(miri, ignore)]
+fn sync_call_async_typed() -> Result<()> {
+ const WAT: &str = r#"
+(component
+ (core func $task.return (canon task.return (result u32)))
+ (core module $m
+ (import "" "task.return" (func $task.return (param i32)))
+ (func (export "run") (result i32)
+ (call $task.return (i32.const 42))
+ (i32.const 0 (; EXIT ;)))
+ (func (export "run-cb") (param i32 i32 i32) (result i32)
+ unreachable))
+ (core instance $i (instantiate $m
+ (with "" (instance (export "task.return" (func $task.return))))))
+ (func (export "run") async (result u32)
+ (canon lift (core func $i "run") async (callback (core func $i "run-cb"))))
+)
+"#;
+
+ let engine = Engine::default();
+ let component = Component::new(&engine, WAT)?;
+ let mut store = Store::new(&engine, ());
+ let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
+
+ let func = instance.get_func(&mut store, "run").unwrap();
+ let mut results = [Val::U32(0)];
+ let error = func.call(&mut store, &[], &mut results).unwrap_err();
+ assert!(
+ error
+ .to_string()
+ .contains("cannot synchronously call an async-lifted export"),
+ "{error}"
+ );
+
+ let func = instance
+ .get_typed_func::<(), (u32,)>(&mut store, "run")
+ .unwrap();
+ let error = func.call(&mut store, ()).unwrap_err();
+ assert!(
+ error
+ .to_string()
+ .contains("cannot synchronously call an async-lifted export"),
+ "{error}"
+ );
+
+ Ok(())
+}
+
/// This is a regression test for
/// https://github.com/bytecodealliance/wasmtime/issues/14504, in which the host
/// panicked due to a too-early deletion of a task group.