Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 54 additions & 7 deletions apps/purepoint-windows/src-tauri/src/daemon/mux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}