A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
support and test synchronous `{stream,future}.cancel-{read,write}` (#11645)
Joel Dice024db5b4e862
diff --git a/crates/misc/component-async-tests/tests/scenario/transmit.rs b/crates/misc/component-async-tests/tests/scenario/transmit.rsindex e6e4b2cc66..98c2526702 100644--- a/crates/misc/component-async-tests/tests/scenario/transmit.rs+++ b/crates/misc/component-async-tests/tests/scenario/transmit.rs@@ -903,22 +903,35 @@ async fn test_transmit_with<Test: TransmitTest + 'static>(component: &str) -> ReOk(())}-mod cancel_transmit {+mod synchronous_transmit {wasmtime::component::bindgen!({path: "wit",- world: "cancel-transmit-guest"+ world: "synchronous-transmit-guest"});}#[tokio::test]pub async fn async_cancel_transmit() -> Result<()> {- let engine = Engine::new(&config())?;+ test_synchronous_transmit(+ test_programs_artifacts::ASYNC_CANCEL_TRANSMIT_COMPONENT,+ true,+ )+ .await+}- let component = make_component(- &engine,- &[test_programs_artifacts::ASYNC_CANCEL_TRANSMIT_COMPONENT],+#[tokio::test]+pub async fn async_synchronous_transmit() -> Result<()> {+ test_synchronous_transmit(+ test_programs_artifacts::ASYNC_SYNCHRONOUS_TRANSMIT_COMPONENT,+ false,)- .await?;+ .await+}++async fn test_synchronous_transmit(component: &str, procrastinate: bool) -> Result<()> {+ let engine = Engine::new(&config())?;++ let component = make_component(&engine, &[component]).await?;let mut linker = Linker::new(&engine);@@ -935,55 +948,69 @@ pub async fn async_cancel_transmit() -> Result<()> {);let instance = linker.instantiate_async(&mut store, &component).await?;- let guest = cancel_transmit::CancelTransmitGuest::new(&mut store, &instance)?;+ let guest = synchronous_transmit::SynchronousTransmitGuest::new(&mut store, &instance)?;let stream_expected = vec![2u8, 4, 6, 8, 9];- let stream = StreamReader::new(- instance,- &mut store,- ProcrastinatingStreamProducer(DelayedStreamProducer {- inner: BufferStreamProducer {- buffer: stream_expected.clone(),- },- sleep: sleep(),- }),- );+ let producer = DelayedStreamProducer {+ inner: BufferStreamProducer {+ buffer: stream_expected.clone(),+ },+ sleep: sleep(),+ };+ let stream = if procrastinate {+ StreamReader::new(+ instance,+ &mut store,+ ProcrastinatingStreamProducer(producer),+ )+ } else {+ StreamReader::new(instance, &mut store, producer)+ };let future_expected = 10;- let future = FutureReader::new(- instance,- &mut store,- ProcrastinatingFutureProducer(DelayedFutureProducer {- inner: ValueFutureProducer {- value: future_expected,- },- sleep: sleep(),- }),- );+ let producer = DelayedFutureProducer {+ inner: ValueFutureProducer {+ value: future_expected,+ },+ sleep: sleep(),+ };+ let future = if procrastinate {+ FutureReader::new(+ instance,+ &mut store,+ ProcrastinatingFutureProducer(producer),+ )+ } else {+ FutureReader::new(instance, &mut store, producer)+ };let result = instance.run_concurrent(&mut store, async move |accessor| {let (stream, stream_expected, future, future_expected) = guest- .local_local_cancel_transmit()+ .local_local_synchronous_transmit().call_start(accessor, stream, stream_expected, future, future_expected).await?;accessor.with(|mut access| {- stream.pipe(- &mut access,- ProcrastinatingStreamConsumer(DelayedStreamConsumer {- inner: BufferStreamConsumer {- expected: stream_expected,- },- sleep: sleep(),- }),- );- future.pipe(- access,- ProcrastinatingFutureConsumer(DelayedFutureConsumer {- inner: ValueFutureConsumer {- expected: future_expected,- },- sleep: sleep(),- }),- );+ let consumer = DelayedStreamConsumer {+ inner: BufferStreamConsumer {+ expected: stream_expected,+ },+ sleep: sleep(),+ };+ if procrastinate {+ stream.pipe(&mut access, ProcrastinatingStreamConsumer(consumer));+ } else {+ stream.pipe(&mut access, consumer);+ }+ let consumer = DelayedFutureConsumer {+ inner: ValueFutureConsumer {+ expected: future_expected,+ },+ sleep: sleep(),+ };+ if procrastinate {+ future.pipe(access, ProcrastinatingFutureConsumer(consumer));+ } else {+ future.pipe(access, consumer);+ }});future::pending::<Result<()>>().await@@ -994,10 +1021,13 @@ pub async fn async_cancel_transmit() -> Result<()> {// `Instance::run_concurrent` and expecting a `Trap::AsyncDeadlock` is// the only way to join all tasks for the `Instance`, so that's what we// do:- assert!(matches!(- result.unwrap_err().downcast::<Trap>(),- Ok(Trap::AsyncDeadlock)- ));+ assert!(+ matches!(+ result.as_ref().unwrap_err().downcast_ref::<Trap>(),+ Some(Trap::AsyncDeadlock)+ ),+ "unexpected error {result:?}"+ );Ok(())}diff --git a/crates/misc/component-async-tests/tests/test_all.rs b/crates/misc/component-async-tests/tests/test_all.rsindex 91cdf6873e..93ebbe3238 100644--- a/crates/misc/component-async-tests/tests/test_all.rs+++ b/crates/misc/component-async-tests/tests/test_all.rs@@ -30,8 +30,8 @@ use scenario::round_trip_many::{use scenario::streams::async_closed_streams;use scenario::transmit::{async_cancel_callee, async_cancel_caller, async_cancel_transmit, async_intertask_communication,- async_poll_stackless, async_poll_synchronous, async_readiness, async_transmit_callee,- async_transmit_caller,+ async_poll_stackless, async_poll_synchronous, async_readiness, async_synchronous_transmit,+ async_transmit_callee, async_transmit_caller,};use scenario::unit_stream::{async_unit_stream_callee, async_unit_stream_caller};use scenario::yield_::{diff --git a/crates/misc/component-async-tests/wit/test.wit b/crates/misc/component-async-tests/wit/test.witindex 7445cde7a4..700f8c83c2 100644--- a/crates/misc/component-async-tests/wit/test.wit+++ b/crates/misc/component-async-tests/wit/test.wit@@ -166,7 +166,7 @@ interface readiness {start: async func(s: stream<u8>, expected: list<u8>) -> tuple<stream<u8>, list<u8>>;}-interface cancel-transmit {+interface synchronous-transmit {start: async func(s: stream<u8>, s-expected: list<u8>, f: future<u8>, f-expected: u8) -> tuple<stream<u8>, list<u8>, future<u8>, u8>;}@@ -320,6 +320,6 @@ world readiness-guest {export readiness;}-world cancel-transmit-guest {- export cancel-transmit;+world synchronous-transmit-guest {+ export synchronous-transmit;}diff --git a/crates/test-programs/src/bin/async_cancel_transmit.rs b/crates/test-programs/src/bin/async_cancel_transmit.rsindex 5d30e3eb88..e96b212c2d 100644--- a/crates/test-programs/src/bin/async_cancel_transmit.rs+++ b/crates/test-programs/src/bin/async_cancel_transmit.rs@@ -1,7 +1,7 @@mod bindings {wit_bindgen::generate!({path: "../misc/component-async-tests/wit",- world: "cancel-transmit-guest",+ world: "synchronous-transmit-guest",});}@@ -17,7 +17,7 @@ use {};#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[task-return][async]start"]fn task_return_start(_: u32, _: *const u8, _: usize, _: u32, _: u8);@@ -28,7 +28,7 @@ unsafe extern "C" fn task_return_start(_: u32, _: *const u8, _: usize, _: u32, _}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[stream-new-0][async]start"]fn stream_new() -> u64;@@ -39,7 +39,7 @@ unsafe extern "C" fn stream_new() -> u64 {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[async-lower][stream-write-0][async]start"]fn stream_write(_: u32, _: *const u8, _: usize) -> u32;@@ -50,7 +50,7 @@ unsafe extern "C" fn stream_write(_: u32, _: *const u8, _: usize) -> u32 {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[async-lower][stream-read-0][async]start"]fn stream_read(_: u32, _: *mut u8, _: usize) -> u32;@@ -61,7 +61,7 @@ unsafe extern "C" fn stream_read(_: u32, _: *mut u8, _: usize) -> u32 {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[stream-cancel-write-0][async]start"]fn stream_cancel_write(_: u32) -> u32;@@ -72,7 +72,7 @@ unsafe extern "C" fn stream_cancel_write(_: u32) -> u32 {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[stream-cancel-read-0][async]start"]fn stream_cancel_read(_: u32) -> u32;@@ -83,7 +83,7 @@ unsafe extern "C" fn stream_cancel_read(_: u32) -> u32 {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[stream-drop-readable-0][async]start"]fn stream_drop_readable(_: u32);@@ -94,7 +94,7 @@ unsafe extern "C" fn stream_drop_readable(_: u32) {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[stream-drop-writable-0][async]start"]fn stream_drop_writable(_: u32);@@ -105,7 +105,7 @@ unsafe extern "C" fn stream_drop_writable(_: u32) {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[future-new-1][async]start"]fn future_new() -> u64;@@ -116,7 +116,7 @@ unsafe extern "C" fn future_new() -> u64 {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[async-lower][future-write-1][async]start"]fn future_write(_: u32, _: *const u8) -> u32;@@ -127,7 +127,7 @@ unsafe extern "C" fn future_write(_: u32, _: *const u8) -> u32 {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[async-lower][future-read-1][async]start"]fn future_read(_: u32, _: *mut u8) -> u32;@@ -138,7 +138,7 @@ unsafe extern "C" fn future_read(_: u32, _: *mut u8) -> u32 {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[future-cancel-write-1][async]start"]fn future_cancel_write(_: u32) -> u32;@@ -149,7 +149,7 @@ unsafe extern "C" fn future_cancel_write(_: u32) -> u32 {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[future-cancel-read-1][async]start"]fn future_cancel_read(_: u32) -> u32;@@ -160,7 +160,7 @@ unsafe extern "C" fn future_cancel_read(_: u32) -> u32 {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[future-drop-readable-1][async]start"]fn future_drop_readable(_: u32);@@ -171,7 +171,7 @@ unsafe extern "C" fn future_drop_readable(_: u32) {}#[cfg(target_arch = "wasm32")]-#[link(wasm_import_module = "[export]local:local/cancel-transmit")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]unsafe extern "C" {#[link_name = "[future-drop-writable-1][async]start"]fn future_drop_writable(_: u32);@@ -203,7 +203,7 @@ enum State {},}-#[unsafe(export_name = "[async-lift]local:local/cancel-transmit#[async]start")]+#[unsafe(export_name = "[async-lift]local:local/synchronous-transmit#[async]start")]unsafe extern "C" fn export_start(stream: u32,stream_expected: u32,@@ -232,7 +232,7 @@ unsafe extern "C" fn export_start(}}-#[unsafe(export_name = "[callback][async-lift]local:local/cancel-transmit#[async]start")]+#[unsafe(export_name = "[callback][async-lift]local:local/synchronous-transmit#[async]start")]unsafe extern "C" fn callback_start(event0: u32, _event1: u32, _event2: u32) -> u32 {unsafe {let state = &mut *(usize::try_from(context_get()).unwrap() as *mut State);diff --git a/crates/test-programs/src/bin/async_synchronous_transmit.rs b/crates/test-programs/src/bin/async_synchronous_transmit.rsnew file mode 100644index 0000000000..89eda8c657--- /dev/null+++ b/crates/test-programs/src/bin/async_synchronous_transmit.rs@@ -0,0 +1,275 @@+mod bindings {+ wit_bindgen::generate!({+ path: "../misc/component-async-tests/wit",+ world: "synchronous-transmit-guest",+ });+}++use {+ std::mem,+ test_programs::async_::{+ CALLBACK_CODE_EXIT, CALLBACK_CODE_YIELD, COMPLETED, DROPPED, EVENT_NONE, context_get,+ context_set,+ },+};++#[cfg(target_arch = "wasm32")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]+unsafe extern "C" {+ #[link_name = "[task-return][async]start"]+ fn task_return_start(_: u32, _: *const u8, _: usize, _: u32, _: u8);+}+#[cfg(not(target_arch = "wasm32"))]+unsafe extern "C" fn task_return_start(_: u32, _: *const u8, _: usize, _: u32, _: u8) {+ unreachable!()+}++#[cfg(target_arch = "wasm32")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]+unsafe extern "C" {+ #[link_name = "[stream-new-0][async]start"]+ fn stream_new() -> u64;+}+#[cfg(not(target_arch = "wasm32"))]+unsafe extern "C" fn stream_new() -> u64 {+ unreachable!()+}++#[cfg(target_arch = "wasm32")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]+unsafe extern "C" {+ #[link_name = "[stream-write-0][async]start"]+ fn stream_write(_: u32, _: *const u8, _: usize) -> u32;+}+#[cfg(not(target_arch = "wasm32"))]+unsafe extern "C" fn stream_write(_: u32, _: *const u8, _: usize) -> u32 {+ unreachable!()+}++#[cfg(target_arch = "wasm32")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]+unsafe extern "C" {+ #[link_name = "[stream-read-0][async]start"]+ fn stream_read(_: u32, _: *mut u8, _: usize) -> u32;+}+#[cfg(not(target_arch = "wasm32"))]+unsafe extern "C" fn stream_read(_: u32, _: *mut u8, _: usize) -> u32 {+ unreachable!()+}++#[cfg(target_arch = "wasm32")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]+unsafe extern "C" {+ #[link_name = "[stream-drop-readable-0][async]start"]+ fn stream_drop_readable(_: u32);+}+#[cfg(not(target_arch = "wasm32"))]+unsafe extern "C" fn stream_drop_readable(_: u32) {+ unreachable!()+}++#[cfg(target_arch = "wasm32")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]+unsafe extern "C" {+ #[link_name = "[stream-drop-writable-0][async]start"]+ fn stream_drop_writable(_: u32);+}+#[cfg(not(target_arch = "wasm32"))]+unsafe extern "C" fn stream_drop_writable(_: u32) {+ unreachable!()+}++#[cfg(target_arch = "wasm32")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]+unsafe extern "C" {+ #[link_name = "[future-new-1][async]start"]+ fn future_new() -> u64;+}+#[cfg(not(target_arch = "wasm32"))]+unsafe extern "C" fn future_new() -> u64 {+ unreachable!()+}++#[cfg(target_arch = "wasm32")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]+unsafe extern "C" {+ #[link_name = "[future-write-1][async]start"]+ fn future_write(_: u32, _: *const u8) -> u32;+}+#[cfg(not(target_arch = "wasm32"))]+unsafe extern "C" fn future_write(_: u32, _: *const u8) -> u32 {+ unreachable!()+}++#[cfg(target_arch = "wasm32")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]+unsafe extern "C" {+ #[link_name = "[future-read-1][async]start"]+ fn future_read(_: u32, _: *mut u8) -> u32;+}+#[cfg(not(target_arch = "wasm32"))]+unsafe extern "C" fn future_read(_: u32, _: *mut u8) -> u32 {+ unreachable!()+}++#[cfg(target_arch = "wasm32")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]+unsafe extern "C" {+ #[link_name = "[future-drop-readable-1][async]start"]+ fn future_drop_readable(_: u32);+}+#[cfg(not(target_arch = "wasm32"))]+unsafe extern "C" fn future_drop_readable(_: u32) {+ unreachable!()+}++#[cfg(target_arch = "wasm32")]+#[link(wasm_import_module = "[export]local:local/synchronous-transmit")]+unsafe extern "C" {+ #[link_name = "[future-drop-writable-1][async]start"]+ fn future_drop_writable(_: u32);+}+#[cfg(not(target_arch = "wasm32"))]+unsafe extern "C" fn future_drop_writable(_: u32) {+ unreachable!()+}++static STREAM_BYTES_TO_WRITE: &[u8] = &[1, 3, 5, 7, 11];+static FUTURE_BYTE_TO_WRITE: u8 = 13;++enum State {+ S0 {+ stream: u32,+ stream_expected: Vec<u8>,+ future: u32,+ future_expected: u8,+ },+ S1 {+ stream_tx: u32,+ stream: u32,+ stream_expected: Vec<u8>,+ future_tx: u32,+ future: u32,+ future_expected: u8,+ },+}++#[unsafe(export_name = "[async-lift]local:local/synchronous-transmit#[async]start")]+unsafe extern "C" fn export_start(+ stream: u32,+ stream_expected: u32,+ stream_expected_len: u32,+ future: u32,+ future_expected: u8,+) -> u32 {+ let stream_expected_len = usize::try_from(stream_expected_len).unwrap();++ unsafe {+ context_set(+ u32::try_from(Box::into_raw(Box::new(State::S0 {+ stream,+ stream_expected: Vec::from_raw_parts(+ stream_expected as usize as *mut u8,+ stream_expected_len,+ stream_expected_len,+ ),+ future,+ future_expected,+ })) as usize)+ .unwrap(),+ );++ callback_start(EVENT_NONE, 0, 0)+ }+}++#[unsafe(export_name = "[callback][async-lift]local:local/synchronous-transmit#[async]start")]+unsafe extern "C" fn callback_start(event0: u32, _event1: u32, _event2: u32) -> u32 {+ unsafe {+ let state = &mut *(usize::try_from(context_get()).unwrap() as *mut State);+ match state {+ &mut State::S0 {+ stream,+ ref mut stream_expected,+ future,+ future_expected,+ } => {+ assert_eq!(event0, EVENT_NONE);++ let pair = stream_new();+ let stream_tx = u32::try_from(pair >> 32).unwrap();+ let stream_rx = u32::try_from(pair & 0xFFFFFFFF_u64).unwrap();++ let pair = future_new();+ let future_tx = u32::try_from(pair >> 32).unwrap();+ let future_rx = u32::try_from(pair & 0xFFFFFFFF_u64).unwrap();++ task_return_start(+ stream_rx,+ STREAM_BYTES_TO_WRITE.as_ptr(),+ STREAM_BYTES_TO_WRITE.len(),+ future_rx,+ FUTURE_BYTE_TO_WRITE,+ );++ *state = State::S1 {+ stream_tx,+ stream,+ stream_expected: mem::take(stream_expected),+ future_tx,+ future,+ future_expected,+ };++ CALLBACK_CODE_YIELD+ }++ &mut State::S1 {+ stream_tx,+ stream,+ ref mut stream_expected,+ future_tx,+ future,+ future_expected,+ } => {+ // Now we synchronously read and write and expect that the+ // operations complete.++ let mut buffer = vec![0_u8; stream_expected.len()];+ let status = stream_read(stream, buffer.as_mut_ptr(), stream_expected.len());+ assert_eq!(+ status,+ DROPPED | u32::try_from(stream_expected.len() << 4).unwrap()+ );+ assert_eq!(&buffer[..], stream_expected);+ stream_drop_readable(stream);++ let status = stream_write(+ stream_tx,+ STREAM_BYTES_TO_WRITE.as_ptr(),+ STREAM_BYTES_TO_WRITE.len(),+ );+ assert_eq!(+ status,+ DROPPED | u32::try_from(STREAM_BYTES_TO_WRITE.len() << 4).unwrap()+ );+ stream_drop_writable(stream_tx);++ let received = &mut 0_u8;+ let status = future_read(future, received);+ assert_eq!(status, COMPLETED);+ assert_eq!(*received, future_expected);+ future_drop_readable(future);++ let status = future_write(future_tx, &FUTURE_BYTE_TO_WRITE);+ assert_eq!(status, COMPLETED);+ future_drop_writable(future_tx);++ CALLBACK_CODE_EXIT+ }+ }+ }+}++// Unused function; required since this file is built as a `bin`:+fn main() {}diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rsindex d5f0f92ea7..61a1e1c703 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs@@ -2752,9 +2752,6 @@ impl Instance {let count = usize::try_from(count).unwrap();let options = Options::new_index(store.0, self, options);self.check_bounds(store.0, &options, ty, address, count)?;- if !options.async_() {- bail!("synchronous stream and future writes not yet supported");- }let (rep, state) = self.id().get_mut(store.0).get_mut_by_index(ty, handle)?;let TransmitLocalState::Write { done } = *state else {bail!(@@ -2801,7 +2798,7 @@ impl Instance {Ok::<_, crate::Error>(())};- let result = match mem::replace(&mut transmit.read, new_state) {+ let mut result = match mem::replace(&mut transmit.read, new_state) {ReadState::GuestReady {ty: read_ty,flat_abi: read_flat_abi,@@ -2935,6 +2932,10 @@ impl Instance {}};+ if result == ReturnCode::Blocked && !options.async_() {+ result = self.wait_for_write(store.0, transmit_handle)?;+ }+if result != ReturnCode::Blocked {*self.id().get_mut(store.0).get_mut_by_index(ty, handle)?.1 =TransmitLocalState::Write {@@ -3004,9 +3005,6 @@ impl Instance {let count = usize::try_from(count).unwrap();let options = Options::new_index(store.0, self, options);self.check_bounds(store.0, &options, ty, address, count)?;- if !options.async_() {- bail!("synchronous stream and future reads not yet supported");- }let (rep, state) = self.id().get_mut(store.0).get_mut_by_index(ty, handle)?;let TransmitLocalState::Read { done } = *state else {bail!("invalid handle {handle}; expected `Read`; got {:?}", *state);@@ -3050,7 +3048,7 @@ impl Instance {Ok::<_, crate::Error>(())};- let result = match mem::replace(&mut transmit.write, new_state) {+ let mut result = match mem::replace(&mut transmit.write, new_state) {WriteState::GuestReady {ty: write_ty,flat_abi: write_flat_abi,@@ -3166,6 +3164,10 @@ impl Instance {WriteState::Dropped => ReturnCode::Dropped(0),};+ if result == ReturnCode::Blocked && !options.async_() {+ result = self.wait_for_read(store.0, transmit_handle)?;+ }+if result != ReturnCode::Blocked {*self.id().get_mut(store.0).get_mut_by_index(ty, handle)?.1 =TransmitLocalState::Read {@@ -3220,6 +3222,24 @@ impl Instance {})}+ fn wait_for_write(+ self,+ store: &mut dyn VMStore,+ handle: TableId<TransmitHandle>,+ ) -> Result<ReturnCode> {+ let waitable = Waitable::Transmit(handle);+ self.wait_for_event(store, waitable)?;+ let event = waitable.take_event(self.concurrent_state_mut(store))?;+ if let Some(event @ (Event::StreamWrite { code, .. } | Event::FutureWrite { code, .. })) =+ event+ {+ waitable.on_delivery(self.id().get_mut(store), event);+ Ok(code)+ } else {+ unreachable!()+ }+ }+/// Cancel a pending stream or future write.fn cancel_write(self,@@ -3262,22 +3282,11 @@ impl Instance {if async_ {ReturnCode::Blocked} else {- let waitable = Waitable::Transmit(- self.concurrent_state_mut(store)- .get_mut(transmit_id)?- .write_handle,- );- self.wait_for_event(store, waitable)?;- let event = waitable.take_event(self.concurrent_state_mut(store))?;- if let Some(- event @ (Event::StreamWrite { code, .. } | Event::FutureWrite { code, .. }),- ) = event- {- waitable.on_delivery(self.id().get_mut(store), event);- code- } else {- unreachable!()- }+ let handle = self+ .concurrent_state_mut(store)+ .get_mut(transmit_id)?+ .write_handle;+ self.wait_for_write(store, handle)?}} else {ReturnCode::Cancelled(0)@@ -3298,6 +3307,24 @@ impl Instance {Ok(code)}+ fn wait_for_read(+ self,+ store: &mut dyn VMStore,+ handle: TableId<TransmitHandle>,+ ) -> Result<ReturnCode> {+ let waitable = Waitable::Transmit(handle);+ self.wait_for_event(store, waitable)?;+ let event = waitable.take_event(self.concurrent_state_mut(store))?;+ if let Some(event @ (Event::StreamRead { code, .. } | Event::FutureRead { code, .. })) =+ event+ {+ waitable.on_delivery(self.id().get_mut(store), event);+ Ok(code)+ } else {+ unreachable!()+ }+ }+/// Cancel a pending stream or future read.fn cancel_read(self,@@ -3340,22 +3367,11 @@ impl Instance {if async_ {ReturnCode::Blocked} else {- let waitable = Waitable::Transmit(- self.concurrent_state_mut(store)- .get_mut(transmit_id)?- .read_handle,- );- self.wait_for_event(store, waitable)?;- let event = waitable.take_event(self.concurrent_state_mut(store))?;- if let Some(- event @ (Event::StreamRead { code, .. } | Event::FutureRead { code, .. }),- ) = event- {- waitable.on_delivery(self.id().get_mut(store), event);- code- } else {- unreachable!()- }+ let handle = self+ .concurrent_state_mut(store)+ .get_mut(transmit_id)?+ .read_handle;+ self.wait_for_read(store, handle)?}} else {ReturnCode::Cancelled(0)diff --git a/tests/misc_testsuite/component-model-async/future-read.wast b/tests/misc_testsuite/component-model-async/future-read.wastindex a780ff0dd4..db0529dcc1 100644--- a/tests/misc_testsuite/component-model-async/future-read.wast+++ b/tests/misc_testsuite/component-model-async/future-read.wast@@ -53,7 +53,8 @@(canon lift (core func $i "run"))))-(assert_trap (invoke "run") "synchronous stream and future reads not yet supported")+;; We expect deadlock since the write end is leaked:+(assert_trap (invoke "run") "deadlock detected: event loop cannot make further progress");; asynchronous future.read; sync lift(component@@ -161,7 +162,8 @@(canon lift (core func $i "run"))))-(assert_trap (invoke "run") "synchronous stream and future reads not yet supported")+;; We expect deadlock since the write end is leaked:+(assert_trap (invoke "run") "deadlock detected: event loop cannot make further progress");; asynchronous future.read; async lift(component