From 36bb25a8ae6695a1f850b346cff8290c33cda613 Mon Sep 17 00:00:00 2001 From: 2witstudios <2witstudios@gmail.com> Date: Mon, 30 Nov 2026 14:27:22 -0600 Subject: [PATCH] fix(win-app): check the generation and take a mux outcome under one lock A reset could still fall between a waiter's generation check and its taking the outcome. Both now happen while the slot's lock is held, so a reset either precedes them (the waiter gets the stale-attempt error) or follows the return, when it has closed the session: the caller's channel open then fails as SessionClosed, the connector retries once, and the retry makes a fresh attempt. A test covers that last path. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QyQdJ2REppCunF9PidNt3K --- .../src-tauri/src/daemon/mux.rs | 61 ++++++++++++++++--- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/apps/purepoint-windows/src-tauri/src/daemon/mux.rs b/apps/purepoint-windows/src-tauri/src/daemon/mux.rs index ed657026..1fff510a 100644 --- a/apps/purepoint-windows/src-tauri/src/daemon/mux.rs +++ b/apps/purepoint-windows/src-tauri/src/daemon/mux.rs @@ -249,13 +249,22 @@ impl MuxSlot { Err(known) => return known, }; loop { - if let Some(outcome) = pending.borrow_and_update().clone() { - // The slot may have been reset (or its host changed) after the attempt stored - // its outcome but before this waiter took it: that outcome is for what the slot - // no longer is, and a session in it was closed by the invalidation. - if self.lock().generation != generation { - return Err(stale_attempt()); - } + // The slot may have been reset (or its host changed) after the attempt stored its + // outcome but before this waiter took it: that outcome is for what the slot no + // longer is, and a session in it was closed by the invalidation. The check and the + // take happen under the slot's lock, so no reset falls between them; a reset after + // this returns closes the session, whose next channel open fails and is retried. + let taken = { + let state = self.lock(); + pending.borrow_and_update().clone().map(|outcome| { + if state.generation == generation { + outcome + } else { + Err(stale_attempt()) + } + }) + }; + if let Some(outcome) = taken { return outcome; } if pending.changed().await.is_err() { @@ -921,4 +930,42 @@ mod tests { assert!(matches!(outcome, Err(DaemonError::NotRunning(_)))); assert_eq!(slot.mode(), RelayMode::Unknown); } + + #[tokio::test] + async fn given_reset_after_a_waiter_took_its_session_should_close_it_for_a_fresh_attempt() { + // given: a caller holding the session it was handed + let slot = Arc::new(MuxSlot::default()); + let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let old = live_client().await; + let Ok(Session::Ready(taken)) = slot + .session_with( + HOST, + false, + attempt(&calls, Duration::ZERO, Ok(Session::Ready(old.clone()))), + ) + .await + else { + panic!("expected the session"); + }; + + // when: the machine is reset before the caller opens its channel + slot.reset(); + + // then: the open fails as a closed session (the connector retries once), and the + // retry makes a fresh attempt rather than reusing the old session + assert!(matches!( + taken.open().await, + Err(pu_transport::mux::OpenError::SessionClosed(_)) + )); + let fresh = live_client().await; + let retry = slot + .session_with( + HOST, + false, + attempt(&calls, Duration::ZERO, Ok(Session::Ready(fresh.clone()))), + ) + .await; + assert!(matches!(retry, Ok(Session::Ready(ref c)) if !c.is_closed())); + assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 2); + } }