Skip to content

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

change `assert!` to `ensure!` to avoid panic when suspending (#14736)

Alex Crichton73de1953776f
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index 1294bdd0f0..4afd2bac7e 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -2492,7 +2492,9 @@ impl StoreOpaque {
};
let suspend_reason = &mut self.concurrent_state_mut()?.suspend_reason;
- assert!(suspend_reason.is_none());
+ if suspend_reason.is_some() {
+ bail_bug!("suspend reason unexpectedly already set for store");
+ }
*suspend_reason = Some(reason);
// We'll panic if we call `Self::with_blocking` when the fiber is being
diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rs
index 03b5e9f07a..1ff700cd51 100644
--- a/tests/all/component_model/async.rs
+++ b/tests/all/component_model/async.rs
@@ -2203,6 +2203,71 @@ async fn resource_drop_call_hook_error() -> Result<()> {
Ok(())
}
+/// Regression test for case which previously panicked when finishing a call
+/// which was started before an intervening, failing call.
+#[tokio::test]
+#[cfg_attr(miri, ignore)]
+async fn call_between_start_and_finish() -> wasmtime::Result<()> {
+ let mut config = Config::new();
+ config.wasm_component_model_async(true);
+ let engine = Engine::new(&config)?;
+ let component = Component::new(
+ &engine,
+ r#"
+(component
+ (core func $thread.yield (canon thread.yield))
+ (core func $task.return (canon task.return))
+ (core module $m
+ (import "" "thread.yield" (func $thread.yield (result i32)))
+ (import "" "task.return" (func $task.return))
+ ;; sync-typed export which yields
+ (func (export "s") (drop (call $thread.yield)))
+ ;; async-typed, callback-lifted export
+ (func (export "f") (result i32) (call $task.return) (i32.const 1)) ;; YIELD
+ (func (export "f-cb") (param i32 i32 i32) (result i32) (i32.const 0))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "thread.yield" (func $thread.yield))
+ (export "task.return" (func $task.return))))))
+ (func (export "s") (canon lift (core func $i "s")))
+ (func (export "f") async
+ (canon lift (core func $i "f") async (callback (core func $i "f-cb"))))
+)
+"#,
+ )?;
+ let mut store = Store::new(&engine, ());
+ let linker = Linker::new(&engine);
+ let instance = linker.instantiate(&mut store, &component)?;
+ let s = instance.get_typed_func::<(), ()>(&mut store, "s")?;
+ let f = instance.get_typed_func::<(), ()>(&mut store, "f")?;
+
+ let err = store
+ .run_concurrent(async |accessor| {
+ // Queue a call to `f`; its start is now a queued work item for the
+ // instance.
+ let call = accessor.with(|s| f.start_call_concurrent(s, ()))?;
+ // Synchronously call `s`, which yields. Since `s` is sync-typed and
+ // there's a queued work item for its instance, Wasmtime promotes it
+ // and tries to suspend, but we're not on a fiber.
+ let _r = accessor.with(|mut st| s.call(&mut st, ()));
+ // Now keep driving the event loop.
+ f.finish_call_concurrent(accessor, call).await?;
+ wasmtime::Result::<()>::Ok(())
+ })
+ .await
+ .and_then(|r| r)
+ .unwrap_err();
+
+ assert_eq!(
+ err.downcast_ref::<Trap>(),
+ Some(&Trap::CannotEnterComponent),
+ "unexpected error: {err:?}"
+ );
+
+ Ok(())
+}
+
#[tokio::test]
#[cfg_attr(miri, ignore)]
async fn zero_length_reads_and_writes() -> Result<()> {