Skip to content

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

cm-async: Clear pending waitable-set on drop (#13663)

Alex Crichton6670d59e23b1
diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
index 6b04aaccdb..d63e530f11 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -2437,6 +2437,7 @@ impl StoreOpaque {
/// Drop the read end of a stream or future read from the host.
fn host_drop_reader(&mut self, id: TableId<TransmitHandle>, kind: TransmitKind) -> Result<()> {
let state = self.concurrent_state_mut();
+ Waitable::Transmit(id).join(state, None)?;
let transmit_id = state.get_mut(id)?.state;
let transmit = state
.get_mut(transmit_id)
@@ -2513,6 +2514,7 @@ impl StoreOpaque {
on_drop_open: Option<fn() -> Result<()>>,
) -> Result<()> {
let state = self.concurrent_state_mut();
+ Waitable::Transmit(id).join(state, None)?;
let transmit_id = state.get_mut(id)?.state;
let transmit = state
.get_mut(transmit_id)
diff --git a/tests/misc_testsuite/component-model/async/waitable-set-stale-entry.wast b/tests/misc_testsuite/component-model/async/waitable-set-stale-entry.wast
new file mode 100644
index 0000000000..8138d36581
--- /dev/null
+++ b/tests/misc_testsuite/component-model/async/waitable-set-stale-entry.wast
@@ -0,0 +1,133 @@
+;;! component_model_async = true
+
+(component definition $A
+ (core module $libc (memory (export "m") 1))
+ (core instance $libc (instantiate $libc))
+
+ (type $s (stream u8))
+ (core func $stream.new (canon stream.new $s))
+ (core func $stream.drop-readable (canon stream.drop-readable $s))
+ (core func $stream.drop-writable (canon stream.drop-writable $s))
+ (core func $waitable-set.new (canon waitable-set.new))
+ (core func $waitable-set.drop (canon waitable-set.drop))
+ (core func $waitable.join (canon waitable.join))
+ (core func $waitable-set.poll (canon waitable-set.poll (memory $libc "m")))
+ (core func $waitable-set.wait (canon waitable-set.wait (memory $libc "m")))
+
+ (core module $m
+ (import "" "m" (memory 1))
+ (import "" "stream.new" (func $stream.new (result i64)))
+ (import "" "stream.drop-readable" (func $stream.drop-readable (param i32)))
+ (import "" "stream.drop-writable" (func $stream.drop-writable (param i32)))
+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
+ (import "" "waitable-set.drop" (func $waitable-set.drop (param i32)))
+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))
+ (import "" "waitable-set.poll" (func $waitable-set.poll (param i32 i32) (result i32)))
+ (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32)))
+
+ ;; drop a readable handle with a pending event that's part of a waitable set,
+ ;; and `poll` should not see that event.
+ (func (export "drop-read-then-poll")
+ (local $tmp i64) (local $r i32) (local $w i32) (local $set i32)
+ (local.set $tmp (call $stream.new))
+ (local.set $r (i32.wrap_i64 (local.get $tmp)))
+ (local.set $w (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))
+ (local.set $set (call $waitable-set.new))
+ (call $waitable.join (local.get $r) (local.get $set))
+ (call $stream.drop-writable (local.get $w))
+ (call $stream.drop-readable (local.get $r))
+ (call $waitable-set.poll (local.get $set) (i32.const 8))
+ (if (i32.ne (i32.const 0)) (then unreachable)))
+
+ (func (export "drop-read-then-wait")
+ (local $tmp i64) (local $r i32) (local $w i32) (local $set i32)
+ (local.set $tmp (call $stream.new))
+ (local.set $r (i32.wrap_i64 (local.get $tmp)))
+ (local.set $w (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))
+ (local.set $set (call $waitable-set.new))
+ (call $waitable.join (local.get $r) (local.get $set))
+ (call $stream.drop-writable (local.get $w))
+ (call $stream.drop-readable (local.get $r))
+ (drop (call $waitable-set.wait (local.get $set) (i32.const 8))))
+
+ (func (export "drop-write-then-poll")
+ (local $tmp i64) (local $r i32) (local $w i32) (local $set i32)
+ (local.set $tmp (call $stream.new))
+ (local.set $r (i32.wrap_i64 (local.get $tmp)))
+ (local.set $w (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))
+ (local.set $set (call $waitable-set.new))
+ (call $waitable.join (local.get $w) (local.get $set))
+ (call $stream.drop-readable (local.get $r))
+ (call $stream.drop-writable (local.get $w))
+ (call $waitable-set.poll (local.get $set) (i32.const 8))
+ (if (i32.ne (i32.const 0)) (then unreachable)))
+
+ (func (export "drop-read-then-wait-cb") (result i32)
+ (local $tmp i64) (local $r i32) (local $w i32) (local $set i32)
+ (local.set $tmp (call $stream.new))
+ (local.set $r (i32.wrap_i64 (local.get $tmp)))
+ (local.set $w (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))
+ (local.set $set (call $waitable-set.new))
+ (call $waitable.join (local.get $r) (local.get $set))
+ (call $stream.drop-writable (local.get $w))
+ (call $stream.drop-readable (local.get $r))
+ ;; (set << 4) | CallbackCode.WAIT(2)
+ (i32.or (i32.shl (local.get $set) (i32.const 4)) (i32.const 2)))
+ (func (export "cb") (param i32 i32 i32) (result i32) unreachable)
+
+ ;; Also test that simply dropping one half leaves the waitable-set properly
+ (func (export "drop-readable-leaves-set")
+ (local $tmp i64) (local $r i32) (local $w i32) (local $set i32)
+ (local.set $tmp (call $stream.new))
+ (local.set $r (i32.wrap_i64 (local.get $tmp)))
+ (local.set $w (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))
+ (local.set $set (call $waitable-set.new))
+ (call $waitable.join (local.get $r) (local.get $set))
+ (call $stream.drop-readable (local.get $r))
+ (call $waitable-set.drop (local.get $set))
+ (call $stream.drop-writable (local.get $w)))
+ (func (export "drop-writable-leaves-set")
+ (local $tmp i64) (local $r i32) (local $w i32) (local $set i32)
+ (local.set $tmp (call $stream.new))
+ (local.set $r (i32.wrap_i64 (local.get $tmp)))
+ (local.set $w (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))
+ (local.set $set (call $waitable-set.new))
+ (call $waitable.join (local.get $w) (local.get $set))
+ (call $stream.drop-writable (local.get $w))
+ (call $waitable-set.drop (local.get $set))
+ (call $stream.drop-readable (local.get $r)))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "m" (memory $libc "m"))
+ (export "stream.new" (func $stream.new))
+ (export "stream.drop-readable" (func $stream.drop-readable))
+ (export "stream.drop-writable" (func $stream.drop-writable))
+ (export "waitable-set.new" (func $waitable-set.new))
+ (export "waitable-set.drop" (func $waitable-set.drop))
+ (export "waitable.join" (func $waitable.join))
+ (export "waitable-set.poll" (func $waitable-set.poll))
+ (export "waitable-set.wait" (func $waitable-set.wait))
+ ))
+ ))
+ (func (export "drop-read-then-poll") async (canon lift (core func $i "drop-read-then-poll")))
+ (func (export "drop-read-then-wait") async (canon lift (core func $i "drop-read-then-wait")))
+ (func (export "drop-write-then-poll") async (canon lift (core func $i "drop-write-then-poll")))
+ (func (export "drop-read-then-wait-cb") async
+ (canon lift (core func $i "drop-read-then-wait-cb") async (callback (func $i "cb")))
+ )
+ (func (export "drop-readable-leaves-set") async (canon lift (core func $i "drop-readable-leaves-set")))
+ (func (export "drop-writable-leaves-set") async (canon lift (core func $i "drop-writable-leaves-set")))
+)
+(component instance $A $A)
+(assert_return (invoke "drop-read-then-poll"))
+(component instance $A $A)
+(assert_trap (invoke "drop-read-then-wait") "deadlock detected")
+(component instance $A $A)
+(assert_return (invoke "drop-write-then-poll"))
+(component instance $A $A)
+(assert_trap (invoke "drop-read-then-wait-cb") "deadlock detected")
+(component instance $A $A)
+(assert_return (invoke "drop-readable-leaves-set"))
+(component instance $A $A)
+(assert_return (invoke "drop-writable-leaves-set"))