A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix a `bail_bug!` eagerly cancelling a guest task (#14923)
Alex Crichton0a50af4764b8
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex 863e1cf23e..cfc3ed676e 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -2641,8 +2641,13 @@ impl StoreOpaque {// Clean up the thread within this task as it's now never going// to run.+ //+ // Note that `next_switch_item`, if any, belongs to the canceller and+ // not this thread since this thread never ran, so it's saved and+ // restored here to prevent `cleanup_thread` from taking it.assert_eq!(1, task.threads.len());let thread = *task.threads.iter().next().unwrap();+ let next_switch_item = concurrent_state.save_next_switch_item()?;self.cleanup_thread(QualifiedThreadId {task: guest_task,@@ -2650,6 +2655,8 @@ impl StoreOpaque {},caller_instance,)?;+ self.concurrent_state_mut()?+ .restore_next_switch_item(next_switch_item)?;// Not yet started; cancel and remove from pendinglet pending = &mut self.instance_state(instance).concurrent_state().pending;diff --git a/tests/misc_testsuite/component-model/async/cancel-starting-subtask-before-suspend.wast b/tests/misc_testsuite/component-model/async/cancel-starting-subtask-before-suspend.wastnew file mode 100644index 0000000000..19a3e27ddd--- /dev/null+++ b/tests/misc_testsuite/component-model/async/cancel-starting-subtask-before-suspend.wast@@ -0,0 +1,75 @@+;;! component_model_async = true++;; `$B` is called asynchronously by `$C` and, before it ever suspends, cancels+;; one of its own subtasks that is queued behind backpressure in `$A`, then+;; makes another async call. The cancel returns `CANCELLED_BEFORE_STARTED`, the+;; next call is queued (`STARTING`), and everything returns normally.+(component+ (component $A+ (core func $backpressure.inc (canon backpressure.inc))+ (core func $task.return (canon task.return))+ (core module $m+ (import "" "backpressure.inc" (func $backpressure.inc))+ (import "" "task.return" (func $task.return))+ (func (export "f") (result i32) (call $backpressure.inc) (call $task.return) i32.const 0)+ (func (export "cb") (param i32 i32 i32) (result i32) unreachable))+ (core instance $i (instantiate $m (with "" (instance+ (export "backpressure.inc" (func $backpressure.inc))+ (export "task.return" (func $task.return))))))+ (func (export "f") async (canon lift (core func $i "f") async (callback (core func $i "cb")))))++ (component $B+ (import "f" (func $f async))+ (core module $libc (memory (export "mem") 1))+ (core instance $libc (instantiate $libc))+ (core func $f (canon lower (func $f) async (memory (core memory $libc "mem"))))+ (core func $subtask.cancel (canon subtask.cancel))+ (core func $task.return (canon task.return))+ (core module $m+ (import "" "f" (func $f (result i32)))+ (import "" "subtask.cancel" (func $subtask.cancel (param i32) (result i32)))+ (import "" "task.return" (func $task.return))+ (func (export "run") (result i32)+ ;; returns eagerly (RETURNED = 2), leaving backpressure enabled in $A+ (if (i32.ne (call $f) (i32.const 2)) (then unreachable))+ ;; queued behind backpressure: subtask 1, STARTING = 0+ (if (i32.ne (call $f) (i32.const 0x10)) (then unreachable))+ ;; CANCELLED_BEFORE_STARTED = 3+ (if (i32.ne (call $subtask.cancel (i32.const 1)) (i32.const 3)) (then unreachable))+ ;; queued again: subtask 2, STARTING+ (if (i32.ne (call $f) (i32.const 0x20)) (then unreachable))+ (call $task.return)+ i32.const 0)+ (func (export "cb") (param i32 i32 i32) (result i32) unreachable))+ (core instance $i (instantiate $m (with "" (instance+ (export "f" (func $f))+ (export "subtask.cancel" (func $subtask.cancel))+ (export "task.return" (func $task.return))))))+ (func (export "run") async (canon lift (core func $i "run") async (callback (core func $i "cb")))))++ (component $C+ (import "run" (func $run async))+ (core module $libc (memory (export "mem") 1))+ (core instance $libc (instantiate $libc))+ (core func $run (canon lower (func $run) async (memory (core memory $libc "mem"))))+ (core func $task.return (canon task.return))+ (core module $m+ (import "" "run" (func $run (result i32)))+ (import "" "task.return" (func $task.return))+ (func (export "run") (result i32)+ ;; $B returns without blocking+ (if (i32.ne (call $run) (i32.const 2)) (then unreachable))+ (call $task.return)+ i32.const 0)+ (func (export "cb") (param i32 i32 i32) (result i32) unreachable))+ (core instance $i (instantiate $m (with "" (instance+ (export "run" (func $run))+ (export "task.return" (func $task.return))))))+ (func (export "run") async (canon lift (core func $i "run") async (callback (core func $i "cb")))))++ (instance $a (instantiate $A))+ (instance $b (instantiate $B (with "f" (func $a "f"))))+ (instance $c (instantiate $C (with "run" (func $b "run"))))+ (export "run" (func $c "run")))++(assert_return (invoke "run"))