A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Don't deliver spurious DROPPED events to futures (#14676)
Alex Crichton958bc1b67376
diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rsindex 0b1d74e661..05f484f7f5 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs@@ -2432,6 +2432,7 @@ impl StoreOpaque {};let write_handle = transmit.write_handle;+ let done = transmit.done;match mem::replace(&mut transmit.write, new_state) {// If a guest is waiting to write, notify it that the read end has@@ -2452,6 +2453,9 @@ impl StoreOpaque {)?;}+ // Otherwise notify the idle write end that the read end has been+ // dropped, unless it's already done.+ WriteState::Open if done => {}WriteState::Open => {state.update_event(write_handle.rep(),@@ -2528,6 +2532,7 @@ impl StoreOpaque {};let read_handle = transmit.read_handle;+ let done = transmit.done;// Swap in the new read statematch mem::replace(&mut transmit.read, new_state) {@@ -2551,7 +2556,8 @@ impl StoreOpaque {)?;}- // If the read state is open, then there are no registered readers of the stream/future+ // Notify the read-end of the drop, unless we're already done.+ ReadState::Open if done => {}ReadState::Open => {self.concurrent_state_mut()?.update_event(read_handle.rep(),diff --git a/tests/misc_testsuite/component-model/async/future-drop-after-done.wast b/tests/misc_testsuite/component-model/async/future-drop-after-done.wastnew file mode 100644index 0000000000..5a66d5bd6f--- /dev/null+++ b/tests/misc_testsuite/component-model/async/future-drop-after-done.wast@@ -0,0 +1,93 @@+;;! component_model_async = true++;; Once a future's value has been transferred, and the end that completed last+;; has received its COMPLETED event, both ends are done. Dropping one end must+;; then not deliver a DROPPED event to the other.+(component+ (core module $libc (memory (export "memory") 1))+ (core instance $libc (instantiate $libc))+ (type $FT (future u8))+ (core func $future.new (canon future.new $FT))+ (core func $future.read+ (canon future.read $FT async (memory (core memory $libc "memory"))))+ (core func $future.write+ (canon future.write $FT async (memory (core memory $libc "memory"))))+ (core func $future.drop-readable (canon future.drop-readable $FT))+ (core func $future.drop-writable (canon future.drop-writable $FT))+ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable-set.poll+ (canon waitable-set.poll (memory (core memory $libc "memory"))))+ (core func $waitable.join (canon waitable.join))+ (core module $m+ (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)))+ (import "" "future.drop-readable" (func $future.drop-readable (param i32)))+ (import "" "future.drop-writable" (func $future.drop-writable (param i32)))+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))+ (import "" "waitable-set.poll" (func $waitable-set.poll (param i32 i32) (result i32)))+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))++ ;; The write blocks and the read completes it. After the writer receives+ ;; FUTURE_WRITE, dropping it must not notify the reader.+ (func (export "drop-writable") (result i32)+ (local $futures i64) (local $r i32) (local $w i32) (local $set i32)+ (local.set $futures (call $future.new))+ (local.set $r (i32.wrap_i64 (local.get $futures)))+ (local.set $w (i32.wrap_i64 (i64.shr_u (local.get $futures) (i64.const 32))))+ (if (i32.ne (call $future.write (local.get $w) (i32.const 0))+ (i32.const -1 (; BLOCKED ;)))+ (then unreachable))+ (if (i32.ne (call $future.read (local.get $r) (i32.const 0))+ (i32.const 0 (; COMPLETED ;)))+ (then unreachable))+ (local.set $set (call $waitable-set.new))+ (call $waitable.join (local.get $w) (local.get $set))+ (if (i32.ne (call $waitable-set.poll (local.get $set) (i32.const 0))+ (i32.const 5 (; FUTURE_WRITE ;)))+ (then unreachable))+ (call $waitable.join (local.get $r) (local.get $set))+ (call $future.drop-writable (local.get $w))+ ;; Expect no event (0).+ (call $waitable-set.poll (local.get $set) (i32.const 0)))++ ;; The read blocks and the write completes it. After the reader receives+ ;; FUTURE_READ, dropping it must not notify the writer.+ (func (export "drop-readable") (result i32)+ (local $futures i64) (local $r i32) (local $w i32) (local $set i32)+ (local.set $futures (call $future.new))+ (local.set $r (i32.wrap_i64 (local.get $futures)))+ (local.set $w (i32.wrap_i64 (i64.shr_u (local.get $futures) (i64.const 32))))+ (if (i32.ne (call $future.read (local.get $r) (i32.const 0))+ (i32.const -1 (; BLOCKED ;)))+ (then unreachable))+ (if (i32.ne (call $future.write (local.get $w) (i32.const 0))+ (i32.const 0 (; COMPLETED ;)))+ (then unreachable))+ (local.set $set (call $waitable-set.new))+ (call $waitable.join (local.get $r) (local.get $set))+ (if (i32.ne (call $waitable-set.poll (local.get $set) (i32.const 0))+ (i32.const 4 (; FUTURE_READ ;)))+ (then unreachable))+ (call $waitable.join (local.get $w) (local.get $set))+ (call $future.drop-readable (local.get $r))+ ;; Expect no event (0).+ (call $waitable-set.poll (local.get $set) (i32.const 0)))+ )+ (core instance $i (instantiate $m (with "" (instance+ (export "future.new" (func $future.new))+ (export "future.read" (func $future.read))+ (export "future.write" (func $future.write))+ (export "future.drop-readable" (func $future.drop-readable))+ (export "future.drop-writable" (func $future.drop-writable))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable-set.poll" (func $waitable-set.poll))+ (export "waitable.join" (func $waitable.join))))))+ (func (export "drop-writable") (result u32)+ (canon lift (core func $i "drop-writable")))+ (func (export "drop-readable") (result u32)+ (canon lift (core func $i "drop-readable")))+)++(assert_return (invoke "drop-writable") (u32.const 0))+(assert_return (invoke "drop-readable") (u32.const 0))