A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Keep host subtasks alive until cancellation completes (#14490)
Daniel Macovei1fa72272d873
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex 3b2bb415a1..9f6e8d6fd2 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -3579,9 +3579,11 @@ impl Instance {lower(store.as_context_mut(), result, false, Some(task))?;let state = store.0.concurrent_state_mut()?;match &mut state.get_mut(task)?.state {- // The task is already flagged as finished because it was- // cancelled. No need to transition further.- HostTaskState::CalleeDone { .. } => {}+ // Cancellation is only complete once lowering has finished+ // and the terminal event can be published.+ pending @ HostTaskState::CalleeCancelling => {+ *pending = HostTaskState::CalleeDone { cancelled: true };+ }// Otherwise transition this task to the done state.other => *other = HostTaskState::CalleeDone { cancelled: false },@@ -3844,7 +3846,9 @@ impl Instance {let id = TableId::<HostTask>::new(rep);let task = concurrent_state.get_mut(id)?;match &task.state {- HostTaskState::CalleeRunning(_) => bail!(Trap::SubtaskDropNotResolved),+ HostTaskState::CalleeRunning(_) | HostTaskState::CalleeCancelling => {+ bail!(Trap::SubtaskDropNotResolved)+ }HostTaskState::CalleeDone { .. } => {}HostTaskState::CalleeStarted | HostTaskState::CalleeFinished(_) => {bail_bug!("invalid state for callee in `subtask.drop`")@@ -4282,7 +4286,7 @@ impl Instance {let needs_block;if let Waitable::Host(host_task) = waitable {let state = &mut concurrent_state.get_mut(host_task)?.state;- match mem::replace(state, HostTaskState::CalleeDone { cancelled: true }) {+ match state {// If the callee is still running, signal an abort is requested.//// After cancelling this falls through to block waiting for the@@ -4291,19 +4295,20 @@ impl Instance {// with the task actually getting cancelled or finishing.HostTaskState::CalleeRunning(handle) => {handle.abort();+ *state = HostTaskState::CalleeCancelling;needs_block = true;}// Cancellation was already requested, so fail as the task can't// be cancelled twice.- HostTaskState::CalleeDone { cancelled } => {- if cancelled {- bail!(Trap::SubtaskCancelAfterTerminal);- } else {- // The callee is already done so there's no need to- // block further for an event.- needs_block = false;- }+ HostTaskState::CalleeCancelling | HostTaskState::CalleeDone { cancelled: true } => {+ bail!(Trap::SubtaskCancelAfterTerminal);+ }+ HostTaskState::CalleeDone { cancelled: false } => {+ // The callee is already done so there's no need to+ // block further for an event.+ *state = HostTaskState::CalleeDone { cancelled: true };+ needs_block = false;}// These states should not be possible for a subtask that's@@ -4988,6 +4993,11 @@ enum HostTaskState {/// used to cancel it if the guest requests cancellation.CalleeRunning(JoinHandle),+ /// Cancellation was requested, but the completion worker has not finished+ /// lowering the result and publishing the terminal event. The task must+ /// remain in the table until that worker completes.+ CalleeCancelling,+/// Terminal state used for tasks in `poll_and_block` to store the result of/// their computation. Note that this state is not used for tasks in/// `first_poll`.@@ -6553,3 +6563,137 @@ fn stage_call0<T: 'static>()}}++#[cfg(all(test, feature = "cranelift", feature = "wat"))]+mod tests {+ use super::*;+ use crate::component::{Component, Linker};+ use crate::store::AsStoreOpaque;+ use crate::{Config, Engine};++ fn host_subtask(+ state: HostTaskState,+ event: Option<Event>,+ ) -> Result<(Store<()>, Instance, TableId<HostTask>, u32)> {+ let mut config = Config::new();+ config.wasm_component_model_async(true);+ let engine = Engine::new(&config)?;+ let component = Component::new(&engine, "(component)")?;+ let mut store = Store::new(&engine, ());+ let instance = Linker::new(&engine).instantiate(&mut store, &component)?;+ let store_opaque = store.as_store_opaque();+ let concurrent_state = store_opaque.concurrent_state_mut()?;+ // These tests create an isolated host task without a guest caller, so+ // give it its own task group instead of inheriting the caller's group.+ let group = concurrent_state.make_task_group()?;+ let task = concurrent_state.push(HostTask {+ common: WaitableCommon::default(),+ call_context: CallContext::default(),+ state,+ group,+ })?;+ let handle = store_opaque+ .instance_state(instance.runtime_instance(RuntimeComponentInstanceIndex::from_u32(0)))+ .handle_table()+ .subtask_insert_host(task.rep())?;+ let common = &mut store_opaque.concurrent_state_mut()?.get_mut(task)?.common;+ common.handle = Some(handle);+ common.event = event;+ Ok((store, instance, task, handle))+ }++ #[test]+ fn host_subtask_drop_during_cancellation() -> Result<()> {+ for abort_completed in [false, true] {+ let (handle, future) = JoinHandle::run(future::pending::<()>());+ let mut future = pin!(future);+ let (mut store, instance, task, handle) =+ host_subtask(HostTaskState::CalleeRunning(handle), None)?;+ let store = store.as_store_opaque();+ let caller = RuntimeComponentInstanceIndex::from_u32(0);+ assert_eq!(+ instance.subtask_cancel(store, caller, true, handle)?,+ BLOCKED+ );+ if abort_completed {+ // Even after the abort resolves, the completion worker still+ // needs the task entry to lower the result and publish an event.+ assert!(matches!(+ future+ .as_mut()+ .poll(&mut Context::from_waker(Waker::noop())),+ Poll::Ready(None),+ ));+ }+ for async_ in [false, true] {+ let err = instance+ .subtask_cancel(store, caller, async_, handle)+ .unwrap_err();+ assert_eq!(err.downcast::<Trap>()?, Trap::SubtaskCancelAfterTerminal);+ }+ let err = instance.subtask_drop(store, caller, handle).unwrap_err();+ assert_eq!(err.downcast::<Trap>()?, Trap::SubtaskDropNotResolved);+ assert!(store.concurrent_state_mut()?.get_mut(task).is_ok());+ }+ Ok(())+ }++ #[test]+ fn host_subtask_cancel_after_completion() -> Result<()> {+ for async_ in [false, true] {+ let (mut store, instance, task, handle) = host_subtask(+ HostTaskState::CalleeDone { cancelled: false },+ Some(Event::Subtask {+ status: Status::Returned,+ }),+ )?;+ let store = store.as_store_opaque();+ let caller = RuntimeComponentInstanceIndex::from_u32(0);+ assert_eq!(+ instance.subtask_cancel(store, caller, async_, handle)?,+ Status::Returned as u32,+ );+ let err = instance+ .subtask_cancel(store, caller, async_, handle)+ .unwrap_err();+ assert_eq!(err.downcast::<Trap>()?, Trap::SubtaskCancelAfterTerminal);+ instance.subtask_drop(store, caller, handle)?;+ assert!(store.concurrent_state_mut()?.get_mut(task).is_err());+ }+ Ok(())+ }++ #[test]+ fn host_subtask_drop_requires_terminal_event_delivery() -> Result<()> {+ for (cancelled, status) in [+ (false, Status::Returned),+ (true, Status::Returned),+ (true, Status::ReturnCancelled),+ ] {+ for delivered in [false, true] {+ let event = if delivered {+ None+ } else {+ Some(Event::Subtask { status })+ };+ let (mut store, instance, task, handle) =+ host_subtask(HostTaskState::CalleeDone { cancelled }, event)?;+ let store = store.as_store_opaque();+ let result = instance.subtask_drop(+ store,+ RuntimeComponentInstanceIndex::from_u32(0),+ handle,+ );+ if delivered {+ result?;+ assert!(store.concurrent_state_mut()?.get_mut(task).is_err());+ } else {+ let err = result.unwrap_err();+ assert_eq!(err.downcast::<Trap>()?, Trap::SubtaskDropNotResolved);+ assert!(store.concurrent_state_mut()?.get_mut(task).is_ok());+ }+ }+ }+ Ok(())+ }+}diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rsindex 7d55d0acb4..eae7ca0012 100644--- a/tests/all/component_model/async.rs+++ b/tests/all/component_model/async.rs@@ -703,22 +703,68 @@ async fn require_concurrency_support() -> Result<()> {#[tokio::test]#[cfg_attr(miri, ignore)]async fn cancel_host_task_does_not_leak() -> Result<()> {+ test_cancel_host_task(HostTaskCancelMode::Sync).await+}++#[tokio::test]+#[cfg_attr(miri, ignore)]+async fn async_cancel_host_task_does_not_leak() -> Result<()> {+ test_cancel_host_task(HostTaskCancelMode::Async).await+}++#[tokio::test]+#[cfg_attr(miri, ignore)]+async fn async_cancel_host_task_drop_before_completion_traps() -> Result<()> {+ test_cancel_host_task(HostTaskCancelMode::DropDuringAsyncCancel).await+}++enum HostTaskCancelMode {+ Sync,+ Async,+ DropDuringAsyncCancel,+}++async fn test_cancel_host_task(mode: HostTaskCancelMode) -> Result<()> {let mut config = Config::new();config.wasm_component_model_async(true);+ config.wasm_component_model_more_async_builtins(true);+ config.wasm_component_model_async_stackful(true);let engine = Engine::new(&config)?;let mut store = Store::new(&engine, ());+ let async_cancel = !matches!(mode, HostTaskCancelMode::Sync);+ let drop_early = matches!(mode, HostTaskCancelMode::DropDuringAsyncCancel);+ let cancel_option = if async_cancel { "async" } else { "" };+ let expected_cancel_status = if async_cancel { -1 } else { 4 };+ let early_drop = if drop_early {+ r#"+ ;; Cancellation returned BLOCKED, so dropping must trap.+ (call $drop (local.get $task))+ ;; Stop here if the drop was incorrectly accepted.+ unreachable+ "#+ } else {+ ""+ };let component = Component::new(&engine,- r#"(component+ format!(+ r#"(component(import "f" (func $f async))+ (core module $memory (memory (export "memory") 1))+ (core instance $memory (instantiate $memory))(core module $m+ (import "" "memory" (memory 1))(import "" "f" (func $f (result i32)))(import "" "cancel" (func $cancel (param i32) (result i32)))(import "" "drop" (func $drop (param i32)))+ (import "" "new-set" (func $new-set (result i32)))+ (import "" "join" (func $join (param i32 i32)))+ (import "" "wait" (func $wait (param i32 i32) (result i32)))+ (import "" "drop-set" (func $drop-set (param i32)))(func (export "run")- (local i32)+ (local $task i32) (local $status i32) (local $set i32);; start the subtask, asserting it's `STARTED`call $f@@ -735,26 +781,46 @@ async fn cancel_host_task_does_not_leak() -> Result<()> {i32.shr_ulocal.set 0- ;; cancel the subtask asserting it's `RETURN_CANCELLED`- local.get 0- call $cancel- i32.const 4 ;; RETURN_CANCELLED- i32.ne- if unreachable end+ ;; Request cancellation and check its status.+ (local.set $status (call $cancel (local.get $task)))+ (if (i32.ne (local.get $status) (i32.const {expected_cancel_status}))+ (then unreachable))+ {early_drop}+ (if (i32.eq (local.get $status) (i32.const -1))+ (then+ (local.set $set (call $new-set))+ (call $join (local.get $task) (local.get $set))+ (if (i32.ne (call $wait (local.get $set) (i32.const 0)) (i32.const 1))+ (then unreachable))+ (if (i32.ne (i32.load (i32.const 0)) (local.get $task))+ (then unreachable))+ (local.set $status (i32.load (i32.const 4)))))+ (if (i32.ne (local.get $status) (i32.const 4)) ;; RETURN_CANCELLED+ (then unreachable));; drop the subtasklocal.get 0call $drop+ (if (local.get $set) (then (call $drop-set (local.get $set))))))(core func $f (canon lower (func $f) async))- (core func $cancel (canon subtask.cancel))+ (core func $cancel (canon subtask.cancel {cancel_option}))(core func $drop (canon subtask.drop))+ (core func $new-set (canon waitable-set.new))+ (core func $join (canon waitable.join))+ (core func $wait (canon waitable-set.wait (memory (core memory $memory "memory"))))+ (core func $drop-set (canon waitable-set.drop))(core instance $i (instantiate $m(with "" (instance+ (export "memory" (memory $memory "memory"))(export "f" (func $f))(export "cancel" (func $cancel))(export "drop" (func $drop))+ (export "new-set" (func $new-set))+ (export "join" (func $join))+ (export "wait" (func $wait))+ (export "drop-set" (func $drop-set))))))@@ -762,7 +828,8 @@ async fn cancel_host_task_does_not_leak() -> Result<()> {(canon lift (core func $i "run")))- )"#,+ )"#+ ),)?;let mut linker = Linker::new(&engine);@@ -774,7 +841,7 @@ async fn cancel_host_task_does_not_leak() -> Result<()> {})?;let instance = linker.instantiate_async(&mut store, &component).await?;let func = instance.get_typed_func::<(), ()>(&mut store, "f")?;- store+ let result = store.run_concurrent(async |store| -> wasmtime::Result<()> {func.call_concurrent(store, ()).await?;@@ -783,10 +850,17 @@ async fn cancel_host_task_does_not_leak() -> Result<()> {}Ok(())})- .await??;+ .await+ .and_then(std::convert::identity);- // The host task was cancelled, nothing should remain.- store.assert_concurrent_state_empty();+ if drop_early {+ let err = result.unwrap_err();+ assert_eq!(err.downcast::<Trap>()?, Trap::SubtaskDropNotResolved);+ } else {+ result?;+ // The host task was cancelled, nothing should remain.+ store.assert_concurrent_state_empty();+ }Ok(())}