A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Run resource destructors with an event loop (#14863)
Alex Crichtonfa20b4c09b30
diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rsindex 2f27b75edf..0e8053b3de 100644--- a/crates/wasmtime/src/runtime/component/func.rs+++ b/crates/wasmtime/src/runtime/component/func.rs@@ -295,14 +295,9 @@ impl Func {#[cfg(feature = "component-model-async")]if store.0.concurrency_support() {- // Fail before the call is staged if the event loop is already- // running, since otherwise the staged call would still run later- // despite this call returning an error.- store.0.check_event_loop_not_running()?;- let call = self.start_call_concurrent(&mut store, params, results)?;return store.run_concurrent_trap_on_idle(async |store| {- self.finish_call_concurrent(store, call).await+ self.call_concurrent(store, params, results).await}).await?;}diff --git a/crates/wasmtime/src/runtime/component/resources/any.rs b/crates/wasmtime/src/runtime/component/resources/any.rsindex c320aee10a..f299a57fd9 100644--- a/crates/wasmtime/src/runtime/component/resources/any.rs+++ b/crates/wasmtime/src/runtime/component/resources/any.rs@@ -304,6 +304,18 @@ impl ResourceAny {#[cfg(feature = "async")]pub async fn resource_drop_async(self, mut store: impl AsContextMut<Data: Send>) -> Result<()> {let mut store = store.as_context_mut();++ // Use the event loop if concurrency support is enabled to handle thread+ // switches, for example, if wasm is run.+ #[cfg(feature = "component-model-async")]+ if store.0.concurrency_support() {+ return store+ .run_concurrent_trap_on_idle(async |store| {+ self.resource_drop_concurrent(store).await+ })+ .await?;+ }+store.on_fiber(|store| self.resource_drop_impl(store)).await?diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rsindex c3d60486ae..e8085ab4c6 100644--- a/tests/all/component_model/async.rs+++ b/tests/all/component_model/async.rs@@ -3688,3 +3688,57 @@ async fn stream_consumer_failed_nested_call_then_yield() -> Result<()> {);Ok(())}++/// A destructor run by `ResourceAny::resource_drop_async` can yield to another+/// task of its instance, more than once.+#[tokio::test]+#[cfg_attr(miri, ignore)]+async fn resource_drop_async_dtor_yields() -> Result<()> {+ let engine = Engine::default();+ let component = Component::new(+ &engine,+ r#"+(component+ (core func $thread.yield (canon thread.yield))+ (core module $dtor+ (import "" "thread.yield" (func $thread.yield (result i32)))+ (func (export "dtor") (param i32)+ (drop (call $thread.yield))+ (drop (call $thread.yield))))+ (core instance $dtor (instantiate $dtor+ (with "" (instance (export "thread.yield" (func $thread.yield))))))+ (type $r (resource (rep i32) (dtor (core func $dtor "dtor"))))+ (core func $resource.new (canon resource.new $r))+ (core func $task.return (canon task.return))+ (core module $m+ (import "" "resource.new" (func $resource.new (param i32) (result i32)))+ (import "" "task.return" (func $task.return))+ (func (export "make") (result i32) (call $resource.new (i32.const 42)))+ ;; Returns right away, but its task keeps yielding forever, leaving work+ ;; queued for this instance.+ (func (export "spin") (result i32)+ (call $task.return)+ (i32.const 1 (; YIELD ;)))+ (func (export "spin-cb") (param i32 i32 i32) (result i32)+ (i32.const 1 (; YIELD ;))))+ (core instance $m (instantiate $m (with "" (instance+ (export "resource.new" (func $resource.new))+ (export "task.return" (func $task.return))))))+ (export $r' "r" (type $r))+ (func (export "make") (result (own $r')) (canon lift (core func $m "make")))+ (func (export "spin") async+ (canon lift (core func $m "spin") async (callback (core func $m "spin-cb"))))+)+"#,+ )?;+ let mut store = Store::new(&engine, ());+ let instance = Linker::new(&engine)+ .instantiate_async(&mut store, &component)+ .await?;+ let make = instance.get_typed_func::<(), (ResourceAny,)>(&mut store, "make")?;+ let spin = instance.get_typed_func::<(), ()>(&mut store, "spin")?;+ let (r,) = make.call_async(&mut store, ()).await?;+ spin.call_async(&mut store, ()).await?;+ r.resource_drop_async(&mut store).await?;+ Ok(())+}diff --git a/tests/all/component_model/resources.rs b/tests/all/component_model/resources.rsindex 99e634615d..0ffbb482f1 100644--- a/tests/all/component_model/resources.rs+++ b/tests/all/component_model/resources.rs@@ -2054,8 +2054,8 @@ mod multiple_host_borrows {(component(import "r" (type $r (sub resource)))(import "mk" (func $mk (result (own $r))))- (import "two" (func $two (param "a" (borrow $r)) (param "b" (borrow $r))))- (import "list" (func $list (param "a" (list (borrow $r)))))+ (import "two" (func $two async (param "a" (borrow $r)) (param "b" (borrow $r))))+ (import "list" (func $list async (param "a" (list (borrow $r)))))(core module $libc (memory (export "memory") 1))(core instance $libc (instantiate $libc))@@ -2133,11 +2133,11 @@ mod multiple_host_borrows {run(Kind::New).await}- async fn drop_val(store: &mut wasmtime::StoreContextMut<'_, ()>, val: &Val) -> Result<()> {+ async fn drop_val(store: &wasmtime::component::Accessor<()>, val: &Val) -> Result<()> {match val {Val::Resource(r) => {assert!(!r.owned());- r.resource_drop_async(&mut *store).await+ r.resource_drop_concurrent(store).await}Val::List(vals) => {for v in vals {@@ -2150,9 +2150,7 @@ mod multiple_host_borrows {}async fn run(kind: Kind) -> Result<()> {- let mut config = Config::new();- config.wasm_component_model_async(true);- let engine = Engine::new(&config)?;+ let engine = Engine::default();let component = Component::new(&engine, COMPONENT)?;let mut linker = Linker::<()>::new(&engine);@@ -2162,18 +2160,18 @@ mod multiple_host_borrows {match kind {Kind::Wrap => {- root.func_wrap_async("two", |mut store, (a, b): (ResourceAny, ResourceAny)| {- Box::new(async move {- a.resource_drop_async(&mut store).await?;- b.resource_drop_async(&mut store).await?;+ root.func_wrap_concurrent("two", |store, (a, b): (ResourceAny, ResourceAny)| {+ Box::pin(async move {+ a.resource_drop_concurrent(store).await?;+ b.resource_drop_concurrent(store).await?;Ok(())})})?;- root.func_wrap_async("list", |mut store, (list,): (Vec<ResourceAny>,)| {- Box::new(async move {+ root.func_wrap_concurrent("list", |store, (list,): (Vec<ResourceAny>,)| {+ Box::pin(async move {assert_eq!(list.len(), 3);for r in list {- r.resource_drop_async(&mut store).await?;+ r.resource_drop_concurrent(store).await?;}Ok(())})@@ -2181,10 +2179,10 @@ mod multiple_host_borrows {}Kind::New => {for name in ["two", "list"] {- root.func_new_async(name, |mut store, _ty, params, _results| {- Box::new(async move {+ root.func_new_concurrent(name, |store, _ty, params, _results| {+ Box::pin(async move {for p in params {- drop_val(&mut store, p).await?;+ drop_val(store, p).await?;}Ok(())})