From f04483228726499e15b8da3c0a506b089d4d609e Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 29 Nov 2026 07:03:32 +0100 Subject: [PATCH] cultmesh-rs: Drop on the pump thread signals Close and does not join itself (last-handle-on-pump-thread) Steward decision (coordinator, 2026-11-29): when the client's last handle is dropped by a wake on the pump thread, Drop sends Close and does not join; the pump ends once the wake returns. The module docs tell a wake to hold the client weakly and say why. Co-Authored-By: Claude Opus 5.5 --- packages/cultmesh-rs/src/rudp_subscription.rs | 13 ++++++- .../cultmesh-rs/tests/rudp_subscription.rs | 37 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/cultmesh-rs/src/rudp_subscription.rs b/packages/cultmesh-rs/src/rudp_subscription.rs index 25c810ef..77729acd 100644 --- a/packages/cultmesh-rs/src/rudp_subscription.rs +++ b/packages/cultmesh-rs/src/rudp_subscription.rs @@ -18,7 +18,12 @@ //! event; once per delivered event (a snapshot, a commit, `Faulted`, `Disconnected`), not once per batch, so //! coalescing is the consumer's; and it must not block, because a slow wake delays the pump and so RUDP acks and //! resends. A panic in it is caught and discarded: the pump survives and the event stays queued. It may call -//! `try_drain`, `subscribe`, `unsubscribe` or `resubscribe_all`. It runs once more when the pump thread ends, after +//! `try_drain`, `subscribe`, `unsubscribe` or `resubscribe_all`; the closure exists before the client does, so it +//! reaches the client through a `Weak` (or a slot filled after `connect_with_wake` returns) and should hold it only +//! weakly: if the owner drops its handle while the wake holds an upgraded one, the wake's handle is the last, and +//! `Drop` then runs on the pump thread. There it signals the pump to end and does not join (a thread cannot join +//! itself), so the pump ends once the wake returns. A wake that held a strong handle for good would keep the client, +//! and so the pump, alive forever. It runs once more when the pump thread ends, after //! `close` or `Drop` has sent the unsubscribes and the goodbye, and never again: a consumer blocked in its own wait //! on another thread is released within the bounds above (500 ms of acknowledgement wait, a poll interval, and a //! reconnect abandoned at its next poll) and its next `try_drain` returns what the pump had queued. @@ -370,7 +375,11 @@ impl Drop for CultMeshRudpSubscriptionClient { self.closing.store(true, Ordering::SeqCst); let _ = self.commands.send(Command::Close { wait: false }); if let Some(pump) = self.pump.take() { - let _ = pump.join(); + // The last handle can be dropped by a wake, on the pump thread, which cannot join itself: the Close + // just sent ends the pump once that wake returns. + if pump.thread().id() != thread::current().id() { + let _ = pump.join(); + } } } } diff --git a/packages/cultmesh-rs/tests/rudp_subscription.rs b/packages/cultmesh-rs/tests/rudp_subscription.rs index 825d72f1..79e1d3be 100644 --- a/packages/cultmesh-rs/tests/rudp_subscription.rs +++ b/packages/cultmesh-rs/tests/rudp_subscription.rs @@ -1611,3 +1611,40 @@ fn a_wake_may_drain_unsubscribe_and_resubscribe_without_deadlock() { let client = Arc::try_unwrap(client).ok().expect("no wake holds the client"); assert!(client.close(), "and the pump still shuts down"); } + +#[test] +fn the_last_handle_dropped_on_the_pump_thread_ends_the_pump_cleanly() { + let peer = Peer::start(); + let slot: Arc>> = Arc::new(OnceLock::new()); + let (upgraded, upgraded_seen) = mpsc::channel(); + let (owner_dropped, owner_dropped_seen) = mpsc::channel::<()>(); + let owner_dropped_seen = Mutex::new(owner_dropped_seen); + let finished = Arc::new(AtomicBool::new(false)); + let first = AtomicBool::new(true); + let (wake_slot, wake_finished) = (slot.clone(), finished.clone()); + let client = Arc::new( + CultMeshRudpSubscriptionClient::connect_with_wake(options(&peer), move || { + if !first.swap(false, Ordering::SeqCst) { + return; + } + let Some(held) = wake_slot.get().and_then(Weak::upgrade) else { return }; + upgraded.send(()).unwrap(); + owner_dropped_seen.lock().unwrap().recv_timeout(Duration::from_secs(5)).expect("the owner dropped its handle"); + // The owner's handle is gone: this is the last one, and it drops on the pump thread. + drop(held); + wake_finished.store(true, Ordering::SeqCst); + }) + .unwrap(), + ); + slot.set(Arc::downgrade(&client)).unwrap(); + client.subscribe("sub-1", vector_selection()).unwrap(); + let (message_id, _) = peer.subscribe_id(); + peer.send(&snapshot_answering(&message_id)); + serve_rows(&peer); + upgraded_seen.recv_timeout(Duration::from_secs(5)).expect("the wake upgraded its handle"); + drop(client); + owner_dropped.send(()).unwrap(); + // The pump ends after the wake returns: it sends the unsubscribes and the goodbye. + peer.expect(PeerEvent::Goodbye(b"subscription-closed".to_vec())); + assert!(finished.load(Ordering::SeqCst), "the wake ran to its end: dropping the last handle did not panic"); +}