A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Preserve low-priority tasks across an `await` (#14749)
Alex Crichtonb68f0c695ca3
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex e3198656ce..3d684703c2 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -1556,35 +1556,9 @@ impl<T> StoreContextMut<'_, T> {// The future we were passed has not yet completed, so handle// any work items and then loop again.PollResult::ProcessWork {- ready,+ mut ready,low_priority,} => {- struct Dispose<'a, T: 'static> {- store: StoreContextMut<'a, T>,- ready: Option<WorkItem>,- }-- impl<'a, T> Drop for Dispose<'a, T> {- fn drop(&mut self) {- if let Some(item) = self.ready.take() {- match item {- WorkItem::ResumeFiber { mut fiber, .. } => {- fiber.dispose(self.store.0);- }- WorkItem::PushFuture(future) => {- tls::set(self.store.0, move || drop(future))- }- _ => {}- }- }- }- }-- let mut dispose = Dispose {- store: self.as_context_mut(),- ready,- };-// If we're about to run a low-priority task, first yield to// the executor. This ensures that it won't be starved of// the ability to e.g. update the readiness of sockets,@@ -1606,23 +1580,29 @@ impl<T> StoreContextMut<'_, T> {// performance issues, this could be optimized such that we// only yield periodically (e.g. for batches of low priority// items) and not for each and every individual item.+ //+ // While we're yielded put the item back on the queue so it+ // stays live within the store, and then if/when we come+ // back take it back out. Note that the `await` point here+ // owns the entire `StoreContextMut` so what we push should+ // be what we pop.if low_priority {- dispose.store.0.yield_now().await;+ if let Some(item) = ready.take() {+ self.0.concurrent_state_mut()?.low_priority.push_back(item);+ }+ self.0.yield_now().await;turns_without_yield = 0;+ ready = self.0.concurrent_state_mut()?.low_priority.pop_back();}- if let Some(item) = dispose.ready.take() {- dispose- .store- .as_context_mut()- .handle_work_item(item)- .await?;+ if let Some(item) = ready {+ self.as_context_mut().handle_work_item(item).await?;}turns_without_yield += 1;if turns_without_yield == MAX_TURNS_WITHOUT_YIELD {turns_without_yield = 0;- dispose.store.0.yield_now().await;+ self.0.yield_now().await;}}}diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rsindex 487088717b..3edc8e8140 100644--- a/tests/all/component_model/async.rs+++ b/tests/all/component_model/async.rs@@ -3280,3 +3280,48 @@ async fn call_async_from_host_function() -> Result<()> {);Ok(())}++#[tokio::test]+#[cfg_attr(miri, ignore)]+async fn yield_callback_after_dropped_event_loop() -> Result<()> {+ let component = r#"+(component+ (core func $task.return (canon task.return))+ (core module $m+ (import "" "task.return" (func $task.return))+ (global $n (mut i32) (i32.const 0))+ (func (export "run") (result i32) (i32.const 1 (; YIELD ;)))+ (func (export "run-cb") (param i32 i32 i32) (result i32)+ (global.set $n (i32.add (global.get $n) (i32.const 1)))+ (if (i32.ge_u (global.get $n) (i32.const 3))+ (then+ (call $task.return)+ (return (i32.const 0 (; EXIT ;)))))+ (i32.const 1 (; YIELD ;)))+ )+ (core instance $i (instantiate $m (with "" (instance+ (export "task.return" (func $task.return))+ ))))+ (func (export "run") async+ (canon lift (core func $i "run") async (callback (core func $i "run-cb"))))+)+"#;++ let engine = super::async_engine();+ let component = Component::new(&engine, component)?;+ let mut store = Store::new(&engine, ());+ let instance = Linker::new(&engine)+ .instantiate_async(&mut store, &component)+ .await?;+ let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;++ // Drop the call when the event loop yields before calling the callback+ // again, then check that the next event loop finishes the task.+ let future = run.call_async(&mut store, ());+ assert!(PollOnce::new(Box::pin(future)).await.is_err());+ store+ .run_concurrent(async |_| yield_times(10).await)+ .await?;+ store.assert_concurrent_state_empty();+ Ok(())+}