Skip to content

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

make async tests using `ready` interface more robust (#12360)

Joel Dice6ca03af14144
diff --git a/crates/misc/component-async-tests/src/lib.rs b/crates/misc/component-async-tests/src/lib.rs
index 24925d9153..86d4d6c3c3 100644
--- a/crates/misc/component-async-tests/src/lib.rs
+++ b/crates/misc/component-async-tests/src/lib.rs
@@ -1,8 +1,5 @@
#![expect(clippy::allow_attributes_without_reason)]
-use std::sync::{Arc, Mutex};
-use std::task::Waker;
-
use wasmtime::component::{HasData, ResourceTable};
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
@@ -21,7 +18,6 @@ pub mod yield_host;
pub struct Ctx {
pub wasi: WasiCtx,
pub table: ResourceTable,
- pub wakers: Arc<Mutex<Option<Vec<Waker>>>>,
pub continue_: bool,
}
diff --git a/crates/misc/component-async-tests/src/yield_host.rs b/crates/misc/component-async-tests/src/yield_host.rs
index ab1fb37346..2cb39990e5 100644
--- a/crates/misc/component-async-tests/src/yield_host.rs
+++ b/crates/misc/component-async-tests/src/yield_host.rs
@@ -1,29 +1,45 @@
use super::Ctx;
use futures::future;
use std::ops::DerefMut;
-use std::task::Poll;
-use wasmtime::component::Accessor;
+use std::sync::{Arc, Mutex};
+use std::task::{Poll, Waker};
+use wasmtime::component::{Accessor, Resource};
pub mod bindings {
wasmtime::component::bindgen!({
path: "wit",
world: "yield-host",
+ imports: { default: trappable },
+ with: {
+ "local:local/ready.thing": super::Thing,
+ },
});
}
+#[derive(Default)]
+pub struct Thing {
+ wakers: Arc<Mutex<Option<Vec<Waker>>>>,
+}
+
impl bindings::local::local::continue_::Host for Ctx {
- fn set_continue(&mut self, v: bool) {
+ fn set_continue(&mut self, v: bool) -> wasmtime::Result<()> {
self.continue_ = v;
+ Ok(())
}
- fn get_continue(&mut self) -> bool {
- self.continue_
+ fn get_continue(&mut self) -> wasmtime::Result<bool> {
+ Ok(self.continue_)
}
}
-impl bindings::local::local::ready::Host for Ctx {
- fn set_ready(&mut self, ready: bool) {
- let mut wakers = self.wakers.lock().unwrap();
+impl bindings::local::local::ready::HostThing for Ctx {
+ fn new(&mut self) -> wasmtime::Result<Resource<Thing>> {
+ Ok(self.table.push(Thing::default())?)
+ }
+
+ fn set_ready(&mut self, thing: Resource<Thing>, ready: bool) -> wasmtime::Result<()> {
+ let thing = self.table.get(&thing)?;
+ let mut wakers = thing.wakers.lock().unwrap();
if ready {
if let Some(wakers) = wakers.take() {
for waker in wakers {
@@ -33,12 +49,24 @@ impl bindings::local::local::ready::Host for Ctx {
} else if wakers.is_none() {
*wakers = Some(Vec::new());
}
+ Ok(())
+ }
+
+ fn drop(&mut self, thing: Resource<Thing>) -> wasmtime::Result<()> {
+ self.table.delete(thing)?;
+ Ok(())
}
}
-impl bindings::local::local::ready::HostWithStore for Ctx {
- async fn when_ready<T>(accessor: &Accessor<T, Self>) {
- let wakers = accessor.with(|mut view| view.get().wakers.clone());
+impl bindings::local::local::ready::HostThingWithStore for Ctx {
+ async fn when_ready<T>(
+ accessor: &Accessor<T, Self>,
+ thing: Resource<Thing>,
+ ) -> wasmtime::Result<()> {
+ let wakers = accessor.with(|mut view| {
+ Ok::<_, wasmtime::Error>(view.get().table.get(&thing)?.wakers.clone())
+ })?;
+
future::poll_fn(move |cx| {
let mut wakers = wakers.lock().unwrap();
if let Some(wakers) = wakers.deref_mut() {
@@ -48,6 +76,10 @@ impl bindings::local::local::ready::HostWithStore for Ctx {
Poll::Ready(())
}
})
- .await
+ .await;
+
+ Ok(())
}
}
+
+impl bindings::local::local::ready::Host for Ctx {}
diff --git a/crates/misc/component-async-tests/tests/scenario/borrowing.rs b/crates/misc/component-async-tests/tests/scenario/borrowing.rs
index 6541205970..5917ff03fa 100644
--- a/crates/misc/component-async-tests/tests/scenario/borrowing.rs
+++ b/crates/misc/component-async-tests/tests/scenario/borrowing.rs
@@ -1,5 +1,4 @@
use std::env;
-use std::sync::{Arc, Mutex};
use std::time::Duration;
use super::util::{config, make_component};
@@ -89,7 +88,6 @@ pub async fn test_run_bool(components: &[&str], v: bool) -> Result<()> {
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(Mutex::new(None)),
},
);
diff --git a/crates/misc/component-async-tests/tests/scenario/post_return.rs b/crates/misc/component-async-tests/tests/scenario/post_return.rs
index 18cd945f79..464a97c4b4 100644
--- a/crates/misc/component-async-tests/tests/scenario/post_return.rs
+++ b/crates/misc/component-async-tests/tests/scenario/post_return.rs
@@ -4,7 +4,6 @@ use component_async_tests::util;
use component_async_tests::{Ctx, sleep};
use std::future;
use std::pin::pin;
-use std::sync::{Arc, Mutex};
use std::task::Poll;
use std::time::Duration;
use wasmtime::Result;
@@ -67,7 +66,6 @@ async fn test_sleep_post_return(components: &[&str]) -> Result<()> {
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(Mutex::new(None)),
},
);
diff --git a/crates/misc/component-async-tests/tests/scenario/round_trip.rs b/crates/misc/component-async-tests/tests/scenario/round_trip.rs
index ac4d444de3..a1314e52bd 100644
--- a/crates/misc/component-async-tests/tests/scenario/round_trip.rs
+++ b/crates/misc/component-async-tests/tests/scenario/round_trip.rs
@@ -1,7 +1,5 @@
-use std::sync::{
- Arc, Mutex,
- atomic::{AtomicU32, Ordering::Relaxed},
-};
+use std::sync::Arc;
+use std::sync::atomic::{AtomicU32, Ordering::Relaxed};
use std::time::Duration;
use super::util::{config, make_component};
@@ -355,7 +353,6 @@ pub async fn test_round_trip(
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(Mutex::new(None)),
},
)
};
diff --git a/crates/misc/component-async-tests/tests/scenario/round_trip_direct.rs b/crates/misc/component-async-tests/tests/scenario/round_trip_direct.rs
index e35ae6345e..1726680903 100644
--- a/crates/misc/component-async-tests/tests/scenario/round_trip_direct.rs
+++ b/crates/misc/component-async-tests/tests/scenario/round_trip_direct.rs
@@ -1,4 +1,3 @@
-use std::sync::{Arc, Mutex};
use std::time::Duration;
use super::util::{config, make_component};
@@ -40,7 +39,6 @@ async fn test_round_trip_direct(
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(Mutex::new(None)),
},
)
};
diff --git a/crates/misc/component-async-tests/tests/scenario/round_trip_many.rs b/crates/misc/component-async-tests/tests/scenario/round_trip_many.rs
index 4a61f58524..980b46a923 100644
--- a/crates/misc/component-async-tests/tests/scenario/round_trip_many.rs
+++ b/crates/misc/component-async-tests/tests/scenario/round_trip_many.rs
@@ -1,8 +1,5 @@
use std::iter;
-use std::sync::{
- Arc, Mutex,
- atomic::{AtomicU32, Ordering::Relaxed},
-};
+use std::sync::atomic::{AtomicU32, Ordering::Relaxed};
use std::time::Duration;
use super::util::{config, make_component};
@@ -208,7 +205,6 @@ async fn test_round_trip_many(
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(Mutex::new(None)),
},
)
};
diff --git a/crates/misc/component-async-tests/tests/scenario/streams.rs b/crates/misc/component-async-tests/tests/scenario/streams.rs
index 9f9175e57b..e025b9af22 100644
--- a/crates/misc/component-async-tests/tests/scenario/streams.rs
+++ b/crates/misc/component-async-tests/tests/scenario/streams.rs
@@ -119,7 +119,6 @@ pub async fn async_closed_streams() -> Result<()> {
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(Mutex::new(None)),
},
);
@@ -280,7 +279,6 @@ pub async fn async_closed_stream() -> Result<()> {
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(Mutex::new(None)),
},
);
@@ -415,7 +413,6 @@ async fn test_async_short_reads(delay: bool) -> Result<()> {
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(Mutex::new(None)),
},
);
diff --git a/crates/misc/component-async-tests/tests/scenario/transmit.rs b/crates/misc/component-async-tests/tests/scenario/transmit.rs
index 57ca2a4a05..606dcc72ea 100644
--- a/crates/misc/component-async-tests/tests/scenario/transmit.rs
+++ b/crates/misc/component-async-tests/tests/scenario/transmit.rs
@@ -1,6 +1,5 @@
use std::future::Future;
use std::pin::Pin;
-use std::sync::{Arc, Mutex};
use std::task::{self, Context, Poll};
use std::time::Duration;
@@ -349,7 +348,6 @@ pub async fn async_readiness() -> Result<()> {
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(Mutex::new(None)),
},
);
@@ -492,7 +490,6 @@ async fn test_cancel(mode: Mode) -> Result<()> {
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(Mutex::new(None)),
},
);
@@ -737,7 +734,6 @@ async fn test_transmit_with<Test: TransmitTest + 'static>(component: &str) -> Re
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(Mutex::new(None)),
},
);
@@ -940,7 +936,6 @@ async fn test_synchronous_transmit(component: &str, procrastinate: bool) -> Resu
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(Mutex::new(None)),
},
);
diff --git a/crates/misc/component-async-tests/tests/scenario/util.rs b/crates/misc/component-async-tests/tests/scenario/util.rs
index 69b69873e8..ac4221adf4 100644
--- a/crates/misc/component-async-tests/tests/scenario/util.rs
+++ b/crates/misc/component-async-tests/tests/scenario/util.rs
@@ -204,7 +204,6 @@ pub async fn test_run_with_count(components: &[&str], count: usize) -> Result<()
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
table: ResourceTable::default(),
continue_: false,
- wakers: Arc::new(std::sync::Mutex::new(None)),
},
);
diff --git a/crates/misc/component-async-tests/wit/test.wit b/crates/misc/component-async-tests/wit/test.wit
index af19a6dd57..c43ce39251 100644
--- a/crates/misc/component-async-tests/wit/test.wit
+++ b/crates/misc/component-async-tests/wit/test.wit
@@ -43,8 +43,11 @@ world round-trip-direct {
}
interface ready {
- set-ready: func(ready: bool);
- when-ready: async func();
+ resource thing {
+ constructor();
+ set-ready: func(ready: bool);
+ when-ready: async func();
+ }
}
interface continue {
diff --git a/crates/test-programs/src/bin/async_poll_stackless.rs b/crates/test-programs/src/bin/async_poll_stackless.rs
index 200cf479d1..edab5cfd31 100644
--- a/crates/test-programs/src/bin/async_poll_stackless.rs
+++ b/crates/test-programs/src/bin/async_poll_stackless.rs
@@ -27,9 +27,10 @@ unsafe extern "C" fn task_return_run() {
unreachable!()
}
-fn async_when_ready() -> u32 {
+fn async_when_ready(handle: u32) -> u32 {
#[cfg(not(target_arch = "wasm32"))]
{
+ _ = handle;
unreachable!()
}
@@ -37,20 +38,36 @@ fn async_when_ready() -> u32 {
{
#[link(wasm_import_module = "local:local/ready")]
unsafe extern "C" {
- #[link_name = "[async-lower]when-ready"]
- fn call_when_ready() -> u32;
+ #[link_name = "[async-lower][method]thing.when-ready"]
+ fn call_when_ready(handle: u32) -> u32;
}
- unsafe { call_when_ready() }
+ unsafe { call_when_ready(handle) }
}
}
enum State {
S0,
- S1 { set: u32 },
- S2 { set: u32, call: u32 },
- S3 { set: u32, call: u32 },
- S4 { set: u32 },
- S5 { set: u32 },
+ S1 {
+ thing: Option<ready::Thing>,
+ set: u32,
+ },
+ S2 {
+ thing: Option<ready::Thing>,
+ set: u32,
+ call: u32,
+ },
+ S3 {
+ thing: Option<ready::Thing>,
+ set: u32,
+ call: u32,
+ },
+ S4 {
+ thing: Option<ready::Thing>,
+ set: u32,
+ },
+ S5 {
+ set: u32,
+ },
}
#[unsafe(export_name = "[async-lift]local:local/run#run")]
@@ -66,44 +83,66 @@ unsafe extern "C" fn callback_run(event0: u32, _: u32, _: u32) -> u32 {
State::S0 => {
assert_eq!(event0, EVENT_NONE);
- ready::set_ready(false);
+ let thing = ready::Thing::new();
+ thing.set_ready(false);
let set = waitable_set_new();
- *state = State::S1 { set };
+ *state = State::S1 {
+ thing: Some(thing),
+ set,
+ };
CALLBACK_CODE_YIELD
}
- &mut State::S1 { set } => {
+ &mut State::S1 { ref mut thing, set } => {
+ let thing = thing.take().unwrap();
let (event0, _, _) = waitable_set_poll(set);
assert_eq!(event0, EVENT_NONE);
- let result = async_when_ready();
+ let result = async_when_ready(thing.handle());
let status = result & 0xf;
let call = result >> 4;
assert!(status != STATUS_RETURNED);
waitable_join(call, set);
- *state = State::S2 { set, call };
+ *state = State::S2 {
+ thing: Some(thing),
+ set,
+ call,
+ };
CALLBACK_CODE_YIELD
}
- &mut State::S2 { set, call } => {
+ &mut State::S2 {
+ ref mut thing,
+ set,
+ call,
+ } => {
+ let thing = thing.take().unwrap();
let (event0, _, _) = waitable_set_poll(set);
assert_eq!(event0, EVENT_NONE);
- ready::set_ready(true);
+ thing.set_ready(true);
- *state = State::S3 { set, call };
+ *state = State::S3 {
+ thing: Some(thing),
+ set,
+ call,
+ };
CALLBACK_CODE_YIELD
}
- &mut State::S3 { set, call } => {
+ &mut State::S3 {
+ ref mut thing,
+ set,
+ call,
+ } => {
let (event0, event1, event2) = waitable_set_poll(set);
if event0 != EVENT_NONE {
@@ -113,18 +152,22 @@ unsafe extern "C" fn callback_run(event0: u32, _: u32, _: u32) -> u32 {
subtask_drop(call);
- *state = State::S4 { set };
+ *state = State::S4 {
+ thing: thing.take(),
+ set,
+ };
}
CALLBACK_CODE_YIELD
}
- &mut State::S4 { set } => {
+ &mut State::S4 { ref mut thing, set } => {
+ let thing = thing.take().unwrap();
let (event0, _, _) = waitable_set_poll(set);
assert_eq!(event0, EVENT_NONE);
- assert_eq!(async_when_ready(), STATUS_RETURNED);
+ assert_eq!(async_when_ready(thing.handle()), STATUS_RETURNED);
*state = State::S5 { set };
diff --git a/crates/test-programs/src/bin/async_poll_synchronous.rs b/crates/test-programs/src/bin/async_poll_synchronous.rs
index 9d5ed7f7cd..c00943bdc8 100644
--- a/crates/test-programs/src/bin/async_poll_synchronous.rs
+++ b/crates/test-programs/src/bin/async_poll_synchronous.rs
@@ -17,9 +17,10 @@ use {
},
};
-fn async_when_ready() -> u32 {
+fn async_when_ready(handle: u32) -> u32 {
#[cfg(not(target_arch = "wasm32"))]
{
+ _ = handle;
unreachable!()
}
@@ -27,10 +28,10 @@ fn async_when_ready() -> u32 {
{
#[link(wasm_import_module = "local:local/ready")]
unsafe extern "C" {
- #[link_name = "[async-lower]when-ready"]
- fn call_when_ready() -> u32;
+ #[link_name = "[async-lower][method]thing.when-ready"]
+ fn call_when_ready(handle: u32) -> u32;
}
- unsafe { call_when_ready() }
+ unsafe { call_when_ready(handle) }
}
}
@@ -39,13 +40,14 @@ struct Component;
impl Guest for Component {
fn run() {
unsafe {
- ready::set_ready(false);
+ let thing = ready::Thing::new();
+ thing.set_ready(false);
let set = waitable_set_new();
assert_eq!(waitable_set_poll(set), (EVENT_NONE, 0, 0));
- let result = async_when_ready();
+ let result = async_when_ready(thing.handle());
let status = result & 0xf;
let call = result >> 4;
assert!(status != STATUS_RETURNED);
@@ -53,7 +55,7 @@ impl Guest for Component {
assert_eq!(waitable_set_poll(set), (EVENT_NONE, 0, 0));
- ready::set_ready(true);
+ thing.set_ready(true);
assert_eq!(thread_yield(), SUSPEND_RESULT_NOT_CANCELLED);
@@ -66,7 +68,7 @@ impl Guest for Component {
assert_eq!(waitable_set_poll(set), (EVENT_NONE, 0, 0));
- assert_eq!(async_when_ready(), STATUS_RETURNED);
+ assert_eq!(async_when_ready(thing.handle()), STATUS_RETURNED);
assert_eq!(waitable_set_poll(set), (EVENT_NONE, 0, 0));
diff --git a/crates/test-programs/src/bin/async_yield_caller.rs b/crates/test-programs/src/bin/async_yield_caller.rs
index a297250d24..1cc8b8cab2 100644
--- a/crates/test-programs/src/bin/async_yield_caller.rs
+++ b/crates/test-programs/src/bin/async_yield_caller.rs
@@ -21,14 +21,15 @@ struct Component;
impl Guest for Component {
async fn run() {
- ready::set_ready(false);
+ let thing = ready::Thing::new();
+ thing.set_ready(false);
continue_::set_continue(true);
- let mut ready = Some(Box::pin(ready::when_ready()));
+ let mut ready = Some(Box::pin(thing.when_ready()));
let mut run = Some(Box::pin(run::run()));
- future::poll_fn(move |cx| {
+ future::poll_fn(|cx| {
let ready_poll = ready.as_mut().map(|v| v.as_mut().poll(cx));
- ready::set_ready(true);
+ thing.set_ready(true);
let run_poll = run.as_mut().map(|v| v.as_mut().poll(cx));
match (run_poll, ready_poll) {
diff --git a/crates/test-programs/src/bin/async_yield_caller_cancel.rs b/crates/test-programs/src/bin/async_yield_caller_cancel.rs
index cc766adbb7..807f644ee5 100644
--- a/crates/test-programs/src/bin/async_yield_caller_cancel.rs
+++ b/crates/test-programs/src/bin/async_yield_caller_cancel.rs
@@ -31,7 +31,8 @@ struct Component;
impl Guest for Component {
async fn run() {
- ready::set_ready(true);
+ let thing = ready::Thing::new();
+ thing.set_ready(true);
continue_::set_continue(true);
unsafe {