Skip to content

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

Prevent `subtask.cancel` from being called twice (#14684)

Alex Crichton2683ae703573
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index fe229a4ee9..7a6c7556a4 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -4532,6 +4532,13 @@ impl Instance {
} else {
let guest_task = TableId::<GuestTask>::new(rep);
let task = concurrent_state.get_mut(guest_task)?;
+
+ // Cancellation was already requested, so fail as the task can't be
+ // cancelled twice.
+ if task.cancel_requested {
+ bail!(Trap::SubtaskCancelAfterTerminal);
+ }
+
if !task.already_lowered_parameters() {
store.cancel_guest_subtask_without_lowered_parameters(
self.runtime_instance(caller_instance),
@@ -4543,9 +4550,9 @@ impl Instance {
// `CANCELLED` event
//
// Note that this might overwrite an event that was set earlier
- // (e.g. `Event::None` if the task is yielding, or
- // `Event::Cancelled` if it was already cancelled), but that's
+ // (e.g. `Event::None` if the task is yielding), but that's
// okay -- this should supersede the previous state.
+ task.cancel_requested = true;
task.event = Some(Event::Cancelled);
let runtime_instance = task.instance;
for thread in task.threads.clone() {
@@ -5467,6 +5474,11 @@ pub(crate) struct GuestTask {
/// cancellation request has been delivered to the task, and thus whether
/// the task is permitted to call `task.cancel`).
cancel_request_delivered: bool,
+ /// Whether or not the caller has requested cancellation of this task via
+ /// `subtask.cancel` while it was still unresolved. A subsequent
+ /// `subtask.cancel` of the same task traps. This matches the spec's
+ /// `Subtask.cancellation_requested`.
+ cancel_requested: bool,
/// Whether or not we've sent a `Status::Starting` event to any current or
/// future waiters for this waitable.
starting_sent: bool,
@@ -5541,6 +5553,7 @@ impl GuestTask {
call_context: CallContext::default(),
sync_result: SyncResult::NotProduced,
cancel_request_delivered: false,
+ cancel_requested: false,
starting_sent: false,
instance,
event: None,
diff --git a/tests/misc_testsuite/component-model/async/double-cancel-guest-subtask.wast b/tests/misc_testsuite/component-model/async/double-cancel-guest-subtask.wast
new file mode 100644
index 0000000000..4b17552c48
--- /dev/null
+++ b/tests/misc_testsuite/component-model/async/double-cancel-guest-subtask.wast
@@ -0,0 +1,49 @@
+;;! component_model_async = true
+;;! component_model_more_async_builtins = true
+
+;; `$B` calls `$A`'s `f`, which never finishes, and requests its cancellation
+;; with an async `subtask.cancel`, which blocks since `f` ignores the request.
+;; Requesting cancellation of the same subtask a second time traps.
+(component
+ (component $A
+ (core module $m
+ (func (export "f") (result i32)
+ (i32.const 1 (; YIELD ;)))
+ (func (export "f-cb") (param i32 i32 i32) (result i32)
+ (i32.const 1 (; YIELD ;))))
+ (core instance $i (instantiate $m))
+ (func (export "f") async
+ (canon lift (core func $i "f") async (callback (core func $i "f-cb")))))
+
+ (component $B
+ (import "f" (func $f async))
+ (core func $f (canon lower (func $f) async))
+ (core func $subtask.cancel (canon subtask.cancel async))
+ (core module $m
+ (import "" "f" (func $f (result i32)))
+ (import "" "subtask.cancel" (func $subtask.cancel (param i32) (result i32)))
+ (func (export "run") (result i32)
+ (local $ret i32) (local $subtask i32)
+ (local.set $ret (call $f))
+ (if (i32.ne (i32.and (local.get $ret) (i32.const 0xf))
+ (i32.const 1 (; STARTED ;)))
+ (then unreachable))
+ (local.set $subtask (i32.shr_u (local.get $ret) (i32.const 4)))
+ (if (i32.ne (call $subtask.cancel (local.get $subtask))
+ (i32.const -1 (; BLOCKED ;)))
+ (then unreachable))
+ (drop (call $subtask.cancel (local.get $subtask)))
+ unreachable)
+ (func (export "run-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))))))
+ (func (export "run") async
+ (canon lift (core func $i "run") async (callback (core func $i "run-cb")))))
+
+ (instance $a (instantiate $A))
+ (instance $b (instantiate $B (with "f" (func $a "f"))))
+ (export "run" (func $b "run"))
+)
+
+(assert_trap (invoke "run") "`subtask.cancel` called after terminal status delivered")