A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Remove thread-switching around completed futures/streams (#14906)
Alex Crichtonffc2f36d04e3
diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rsindex 33134839e6..c0de956b67 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs@@ -1,8 +1,6 @@use super::table::{TableDebug, TableId};use super::{Event, GlobalErrorContextRefCount, Waitable, WaitableCommon};-use crate::component::concurrent::{- ConcurrentState, CurrentThread, QualifiedThreadId, WorkItem, tls,-};+use crate::component::concurrent::{ConcurrentState, CurrentThread, WorkItem, tls};use crate::component::func::{self, LiftContext, LowerContext};use crate::component::matching::InstanceType;use crate::component::resources::{HostResourceIndex, HostResourceTables};@@ -242,7 +240,6 @@ fn get_mut_by_index_from(fn lower<T: func::Lower + Send + 'static, B: WriteBuffer<T>, U: 'static>(mut store: StoreContextMut<U>,instance: Instance,- caller_thread: QualifiedThreadId,options: OptionsIndex,ty: TransmitIndex,address: usize,@@ -251,20 +248,10 @@ fn lower<T: func::Lower + Send + 'static, B: WriteBuffer<T>, U: 'static>() -> Result<()> {let count = buffer.remaining().len().min(count);- // If lowering may call realloc in the guest, then the guest may need- // to access its thread context, so we need to set the current thread before lowering- // and restore the old one afterward.- let (lower, old_thread) = if T::MAY_REQUIRE_REALLOC {- let old_thread = store.0.set_thread(caller_thread)?;- (- &mut LowerContext::new(store.as_context_mut(), options, instance),- Some(old_thread),- )+ let lower = &mut if T::MAY_REQUIRE_REALLOC {+ LowerContext::new(store.as_context_mut(), options, instance)} else {- (- &mut LowerContext::new_without_realloc(store.as_context_mut(), options, instance),- None,- )+ LowerContext::new_without_realloc(store.as_context_mut(), options, instance)};// Note that validation only happens with operations that have a payload.@@ -281,10 +268,6 @@ fn lower<T: func::Lower + Send + 'static, B: WriteBuffer<T>, U: 'static>(T::linear_store_list_to_memory(lower, ty, address, &buffer.remaining()[..count])?;}- if let Some(old_thread) = old_thread {- store.0.set_thread(old_thread)?;- }-buffer.skip(count);Ok(())@@ -2295,7 +2278,6 @@ enum ReadState {GuestReady {ty: TransmitIndex,caller_instance: RuntimeComponentInstanceIndex,- caller_thread: QualifiedThreadId,flat_abi: Option<FlatAbi>,instance: Instance,options: OptionsIndex,@@ -3110,7 +3092,6 @@ async fn write<D: 'static, P: Send + 'static, T: func::Lower + 'static, B: Writehandle,instance,caller_instance,- caller_thread,} => {let guest_offset = match guest_offset {Some(i) => i,@@ -3132,7 +3113,6 @@ async fn write<D: 'static, P: Send + 'static, T: func::Lower + 'static, B: Writelower::<T, B, D>(store.as_context_mut(),instance,- caller_thread,options,ty,address + (T::SIZE32 * guest_offset.as_usize()),@@ -3203,7 +3183,6 @@ async fn write<D: 'static, P: Send + 'static, T: func::Lower + 'static, B: Writehandle,instance,caller_instance,- caller_thread,};crate::error::Ok(())@@ -3390,7 +3369,6 @@ impl Instance {write_options: OptionsIndex,write_address: usize,read_runtime_instance: RuntimeInstance,- read_caller_thread: QualifiedThreadId,read_ty: TransmitIndex,read_options: OptionsIndex,read_address: usize,@@ -3473,10 +3451,6 @@ impl Instance {.transpose()?;if let Some(val) = val {- // Serializing the value may require calling the guest's realloc function, so we- // set the guest's thread context in case realloc requires it, and restore the original- // thread context after the copy is complete.- let old_thread = store.set_thread(read_caller_thread)?;let lower =&mut LowerContext::new(store.as_context_mut(), read_options, read_instance);let ptr = func::validate_inbounds_dynamic(@@ -3489,7 +3463,6 @@ impl Instance {None => bail_bug!("expected read payload type to be present"),};val.store(lower, *ty, ptr)?;- store.set_thread(old_thread)?;}}(TransmitIndex::Stream(_), TransmitIndex::Stream(_)) => {@@ -3559,10 +3532,6 @@ impl Instance {let id = TableId::<TransmitHandle>::new(rep);log::trace!("copy values {values:?} for {id:?}");- // Serializing the value may require calling the guest's realloc function, so we- // set the guest's thread context in case realloc requires it, and restore the original- // thread context after the copy is complete.- let old_thread = store.set_thread(read_caller_thread)?;let lower =&mut LowerContext::new(store.as_context_mut(), read_options, read_instance);let mut ptr = read_address;@@ -3570,7 +3539,6 @@ impl Instance {value.store(lower, *read_payload_ty, ptr)?;ptr += usize::try_from(read_abi.size32)?;}- store.set_thread(old_thread)?;}}_ => bail_bug!("mismatched transmit types in copy"),@@ -3692,7 +3660,6 @@ impl Instance {handle: read_handle,instance: read_instance,caller_instance: read_caller_instance,- caller_thread: read_caller_thread,} => {if flat_abi != read_flat_abi {bail_bug!("expected flat ABI calculations to be the same");@@ -3740,7 +3707,6 @@ impl Instance {options,address,read_instance.runtime_instance(read_caller_instance),- read_caller_thread,read_ty,read_options,read_address,@@ -3789,7 +3755,6 @@ impl Instance {handle: read_handle,instance: read_instance,caller_instance: read_caller_instance,- caller_thread: read_caller_thread,};}@@ -3900,7 +3865,6 @@ impl Instance {*state = TransmitLocalState::Busy;let transmit_handle = TableId::<TransmitHandle>::new(rep);- let caller_thread = store.0.current_guest_thread()?;let concurrent_state = store.0.concurrent_state_mut()?;let transmit_id = concurrent_state.get_mut(transmit_handle)?.state;let transmit = concurrent_state.get_mut(transmit_id)?;@@ -3943,7 +3907,6 @@ impl Instance {handle,instance: self,caller_instance,- caller_thread,};Ok::<_, crate::Error>(())};@@ -3987,7 +3950,6 @@ impl Instance {write_options,write_address,self.runtime_instance(caller_instance),- caller_thread,ty,options,address,diff --git a/tests/misc_testsuite/component-model/async/pending-read-after-task-exit.wast b/tests/misc_testsuite/component-model/async/pending-read-after-task-exit.wastnew file mode 100644index 0000000000..64d5d8d308--- /dev/null+++ b/tests/misc_testsuite/component-model/async/pending-read-after-task-exit.wast@@ -0,0 +1,73 @@+;;! component_model_async = true++;; A pending `future.read` belongs to the instance's handle, not to the task+;; which started it. Here `$B.start-read` starts a read and returns while it's+;; still pending, then `$A` creates a stream, which may reuse the exited task's+;; and thread's slots in the store, and finally `$B.finish` writes the future,+;; which completes the read.+(component+ (component $B+ (type $f (future u8))+ (core module $libc (memory (export "memory") 1))+ (core instance $libc (instantiate $libc))+ (core func $future.new (canon future.new $f))+ (core func $future.read+ (canon future.read $f async (memory (core memory $libc "memory"))))+ (core func $future.write+ (canon future.write $f async (memory (core memory $libc "memory"))))+ (core module $m+ (import "libc" "memory" (memory 1))+ (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)))+ (global $writer (mut i32) (i32.const 0))+ (func (export "start-read")+ (local $ends i64)+ (local.set $ends (call $future.new))+ (global.set $writer (i32.wrap_i64 (i64.shr_u (local.get $ends) (i64.const 32))))+ (if (i32.ne (call $future.read (i32.wrap_i64 (local.get $ends)) (i32.const 0x100))+ (i32.const -1 (; BLOCKED ;)))+ (then unreachable)))+ (func (export "finish")+ (i32.store8 (i32.const 0x200) (i32.const 42))+ (if (i32.ne (call $future.write (global.get $writer) (i32.const 0x200))+ (i32.const 0 (; COMPLETED ;)))+ (then unreachable))+ (if (i32.ne (i32.load8_u (i32.const 0x100)) (i32.const 42))+ (then unreachable))))+ (core instance $i (instantiate $m+ (with "libc" (instance $libc))+ (with "" (instance+ (export "future.new" (func $future.new))+ (export "future.read" (func $future.read))+ (export "future.write" (func $future.write))))))+ (func (export "start-read") (canon lift (core func $i "start-read")))+ (func (export "finish") (canon lift (core func $i "finish"))))+ (instance $b (instantiate $B))++ (component $A+ (import "start-read" (func $start-read))+ (import "finish" (func $finish))+ (type $s (stream u8))+ (core func $start-read (canon lower (func $start-read)))+ (core func $finish (canon lower (func $finish)))+ (core func $stream.new (canon stream.new $s))+ (core module $m+ (import "" "start-read" (func $start-read))+ (import "" "finish" (func $finish))+ (import "" "stream.new" (func $stream.new (result i64)))+ (func (export "run")+ (call $start-read)+ (drop (call $stream.new))+ (call $finish)))+ (core instance $i (instantiate $m (with "" (instance+ (export "start-read" (func $start-read))+ (export "finish" (func $finish))+ (export "stream.new" (func $stream.new))))))+ (func (export "run") (canon lift (core func $i "run"))))+ (instance $a (instantiate $A+ (with "start-read" (func $b "start-read"))+ (with "finish" (func $b "finish"))))+ (export "run" (func $a "run")))++(assert_return (invoke "run"))