A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
avoid panic when `call_async` future dropped while queued (#14741)
Alex Crichton8edbebb8966e
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex 4afd2bac7e..5e3c2d3ef4 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -2667,9 +2667,39 @@ impl StoreOpaque {let pending = &mut self.instance_state(instance).concurrent_state().pending;let pending_count = pending.len();pending.retain(|thread, _| thread.task != guest_task);- // If there were no pending threads for this task, we're in an error state+if pending.len() == pending_count {- bail!(Trap::SubtaskCancelAfterTerminal);+ // If it wasn't found in the pending set, it might be waiting in one+ // of the task queues or switch items:+ let matches = |item: &WorkItem| match item {+ WorkItem::ResumeThread { thread, .. }+ | WorkItem::GuestCall {+ call: GuestCall { thread, .. },+ ..+ } => thread.task == guest_task,+ _ => false,+ };++ let remove = |queue: &mut VecDeque<WorkItem>| {+ let count = queue.len();+ queue.retain(|item| !matches(item));+ count != queue.len()+ };++ let state = self.concurrent_state_mut()?;+ let mut found = remove(&mut state.high_priority);+ found |= remove(&mut state.low_priority);++ for item in [&mut state.switch_item, &mut state.next_switch_item] {+ if matches!(item, Some(item) if matches(item)) {+ found = true;+ *item = None;+ }+ }++ if !found {+ bail!(Trap::SubtaskCancelAfterTerminal);+ }}Ok(())}diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rsindex 1ff700cd51..487088717b 100644--- a/tests/all/component_model/async.rs+++ b/tests/all/component_model/async.rs@@ -4,10 +4,10 @@ use std::collections::HashMap;use std::future::{self, Future};use std::iter;use std::mem;-use std::pin::Pin;+use std::pin::{Pin, pin};use std::sync::atomic::{AtomicUsize, Ordering};use std::sync::{Arc, Mutex};-use std::task::{Context, Poll};+use std::task::{Context, Poll, Waker};use wasmtime::Result;use wasmtime::component::{TaskGroupHook, TaskGroupId};use wasmtime::{Config, Engine, Store, StoreContextMut, Trap, component::*};@@ -2199,6 +2199,153 @@ async fn resource_drop_call_hook_error() -> Result<()> {Some(&Trap::CannotEnterComponent),"unexpected error: {err:?}");+ Ok(())+}++#[tokio::test]+#[cfg_attr(miri, ignore)]+async fn cancel_not_started() -> Result<()> {+ let engine = Engine::default();+ let component = Component::new(+ &engine,+ r#"+ (component+ (import "a" (func $a))+ (core func $a (canon lower (func $a)))++ (core module $a+ (import "" "a" (func $a))+ (import "" "task.return" (func $task.return))+ (func (export "a") (result i32)+ (call $a)+ (call $task.return)+ (i32.const 0 (; EXIT ;))+ )++ (func (export "a-callback") (param i32 i32 i32) (result i32)+ unreachable+ )+ )+ (canon task.return (core func $task.return))+ (core instance $a (instantiate $a+ (with "" (instance+ (export "a" (func $a))+ (export "task.return" (func $task.return))+ ))+ ))+ (func (export "a") async (canon lift (core func $a "a") async (callback (core func $a "a-callback"))))+ )+ "#,+ )?;+ let mut store = Store::new(&engine, ());+ let mut linker = Linker::new(&engine);+ linker.root().func_wrap_async("a", move |_, ()| {+ Box::new(async move {+ for _ in 0..5 {+ tokio::task::yield_now().await;+ }++ Ok(())+ })+ })?;++ let instance = linker.instantiate_async(&mut store, &component).await?;+ let func = instance.get_typed_func::<(), ()>(&mut store, "a")?;++ // Start a call, which will push a work item on the high-priority task+ // queue:+ func.start_call_concurrent(&mut store, ())?;++ // Start and poll an async call once, which will push another work item on+ // the high-priority task queue, then try to drain the queue. However, the+ // above call will call our host function, which will yield back to us here.+ // Then we'll drop the future for the second call, which will not have+ // started because it's still waiting in the queue.+ //+ // Previously, that would have caused a panic in the runtime.+ let mut call = pin!(func.call_async(&mut store, ()));+ assert!(+ call.as_mut()+ .poll(&mut Context::from_waker(Waker::noop()))+ .is_pending()+ );+ drop(call);+ Ok(())+}++// Dropping a `call_async` future while the start of its task is queued in the+// event loop's run queue (rather than blocked in the instance's pending set)+// must cancel the task cleanly rather than panicking in the drop guard.+#[tokio::test]+#[cfg_attr(miri, ignore)]+async fn drop_call_async_with_queued_start() -> Result<()> {+ let mut config = Config::new();+ config.wasm_component_model_async(true);+ let engine = Engine::new(&config)?;++ let component = Component::new(+ &engine,+ r#"+(component+ (type $f (future))+ (core func $future.new (canon future.new $f))+ (core func $future.read (canon future.read $f async))+ (core func $future.write (canon future.write $f async))+ (core func $new (canon waitable-set.new))+ (core func $join (canon waitable.join))+ (core func $task-return (canon task.return))+ (core module $m+ (import "" "future.new" (func $future.new (result i64)))+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))+ (import "" "new" (func $new (result i32)))+ (import "" "join" (func $join (param i32 i32)))+ (import "" "task-return" (func $task-return))+ (func (export "a") (result i32)+ (local $t i64) (local $r i32) (local $w i32) (local $s i32)+ (call $task-return)+ (local.set $t (call $future.new))+ (local.set $r (i32.wrap_i64 (local.get $t)))+ (local.set $w (i32.wrap_i64 (i64.shr_u (local.get $t) (i64.const 32))))+ (drop (call $future.read (local.get $r) (i32.const 0)))+ (local.set $s (call $new))+ (call $join (local.get $r) (local.get $s))+ (drop (call $future.write (local.get $w) (i32.const 0)))+ ;; WAIT on a set which already has a ready event, queueing a callback+ ;; invocation in the event loop.+ (i32.or (i32.const 2) (i32.shl (local.get $s) (i32.const 4))))+ (func (export "cb") (param i32 i32 i32) (result i32) unreachable)+ (func (export "b") (result i32)+ (call $task-return)+ (i32.const 0)))+ (core instance $i (instantiate $m (with "" (instance+ (export "future.new" (func $future.new))+ (export "future.read" (func $future.read))+ (export "future.write" (func $future.write))+ (export "new" (func $new))+ (export "join" (func $join))+ (export "task-return" (func $task-return))))))+ (func (export "a") async (canon lift (core func $i "a") async (callback (core func $i "cb"))))+ (func (export "b") async (canon lift (core func $i "b") async (callback (core func $i "cb"))))+)+ "#,+ )?;++ let mut store = Store::new(&engine, ());+ let linker = Linker::new(&engine);+ let instance = linker.instantiate_async(&mut store, &component).await?;+ let a = instance.get_typed_func::<(), ()>(&mut store, "a")?;+ let b = instance.get_typed_func::<(), ()>(&mut store, "b")?;+ a.call_async(&mut store, ()).await?;++ // The start of `b` is queued behind the pending callback for `a`, which+ // traps, so `b` never starts and its task is cancelled when the future+ // returned by `call_async` is dropped.+ let err = b.call_async(&mut store, ()).await.unwrap_err();+ assert_eq!(+ err.downcast_ref::<Trap>(),+ Some(&Trap::UnreachableCodeReached)+ );Ok(())}