From fa9872370dfb4566ea85a9d909497dd2944f868a Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 19 Oct 2026 12:21:52 -0700 Subject: [PATCH] fix(live): a host is back within seconds of its relay restarting A host waits twice as long before each try at a relay that won't answer, up to 30 seconds, so one that sat through a relay's restart came back up to half a minute after the relay did. Meanwhile its code still showed and guests typing it were told no one shares with it, which looked as though the code had died until sharing was stopped and started again. The wait before joining a relay again now stops growing at 5 seconds, and each wait is drawn between half its length and all of it, so the guests and rooms a restart hangs up on all at once come back spread apart rather than in step; a relay's Retry-After is the least any wait then lasts. A unit test checks that no wait passes 5 seconds however long the relay stays away, that waits differ, and that none undercuts a Retry-After; it fails with the old 30-second ceiling. The relay proxy the Live Share tests share no longer aborts the process when a test that fails poisons its lock. - After the Live Share relay restarts, a sharing computer's code works again within seconds instead of up to half a minute later. Assisted-by: claude-opus-5.5 --- crates/notebook/src/live/relay.rs | 41 +++++++++++++++++++++++---- crates/notebook/tests/support/live.rs | 5 +++- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/crates/notebook/src/live/relay.rs b/crates/notebook/src/live/relay.rs index 92badc5607b271b115ac7c5522da9e5a6f0c942f..b476b27b5ad11fd20dc133b5ab92e635d4767d4e 100644 --- a/crates/notebook/src/live/relay.rs +++ b/crates/notebook/src/live/relay.rs @@ -7,8 +7,8 @@ //! peers then meet afresh. use super::{ - Event, Hello, OPENING, PATIENCE, Paced, Peer, Pipe, Presence, Relayed as Answer, Room, Shared, - Side, code_parts, group, + Event, Hello, OPENING, Paced, Peer, Pipe, Presence, Relayed as Answer, Room, Shared, Side, + code_parts, group, transport::{self, Address, Failure, parse}, wire::kind, }; @@ -25,6 +25,8 @@ use std::{ const KEEPALIVE: Duration = Duration::from_secs(30); /// A connection that lasted this long was no failure, so the next waits only a second. const STEADY: Duration = Duration::from_secs(60); +/// The longest wait before joining again, so the room is back soon after a relay restarts. +const REJOIN: Duration = Duration::from_secs(5); /// The most sent in one message, well under any relay's cap. const CHUNK: usize = 64 << 10; /// The largest message read from the relay. @@ -64,6 +66,8 @@ fn keep(shared: &Arc, address: &Address, port: u16) { .flatten(); while !shared.stopped.load(Ordering::Acquire) { let began = Instant::now(); + // The least the relay asked this end to wait. + let mut asked = Duration::ZERO; let path = match (owner, shared.tag()) { (false, Some(tag)) => format!("{}/v1/room/{tag}", address.path), (false, None) => return, @@ -88,7 +92,7 @@ fn keep(shared: &Arc, address: &Address, port: u16) { if status == 410 { return; } - wait = wait.max(retry.unwrap_or_default()); + asked = retry.unwrap_or_default(); } Err(Failure::Trouble(trouble, error)) => { eprintln!("Live: no relay at {}: {error}", address.authority); @@ -98,11 +102,20 @@ fn keep(shared: &Arc, address: &Address, port: u16) { if began.elapsed() >= STEADY { wait = Duration::from_secs(1); } - thread::sleep(wait); - wait = (wait * 2).min(PATIENCE); + thread::sleep(pause(wait, asked)); + wait = (wait * 2).min(REJOIN); } } +/// How long to wait before joining again: between half `wait` and all of it, so peers a +/// restart hung up on come back apart, and never less than the relay `asked`. +fn pause(wait: Duration, asked: Duration) -> Duration { + let mut random = [0; 4]; + let _ = getrandom::fill(&mut random); + let share = f64::from(u32::from_le_bytes(random)) / f64::from(u32::MAX); + wait.mul_f64(0.5 + share / 2.0).max(asked) +} + /// Opens the relay's `path` at `address`: the connection, and what reads its messages. fn connect( shared: &Shared, @@ -565,6 +578,24 @@ impl Pipe for Relayed { mod tests { use super::*; + /// However long a relay stays away, the next try comes within seconds of its return, + /// tries spread apart, and none comes before the relay's Retry-After. + #[test] + fn rejoining_waits_a_little_spread_and_as_asked() { + let mut wait = Duration::from_secs(1); + let mut pauses = Vec::new(); + for _ in 0..12 { + pauses.push(pause(wait, Duration::ZERO)); + wait = (wait * 2).min(REJOIN); + } + assert!( + pauses.iter().all(|pause| *pause <= Duration::from_secs(5)), + "{pauses:?}" + ); + assert!(pauses.windows(2).any(|two| two[0] != two[1])); + assert!(pause(wait, Duration::from_secs(30)) >= Duration::from_secs(30)); + } + #[test] fn relay_addresses_parse() { let address = parse("wss://live.example.net").unwrap(); diff --git a/crates/notebook/tests/support/live.rs b/crates/notebook/tests/support/live.rs index 37b7cbb050cd208138665b6e5ac12f6df45847a6..12f9917c05309e6829134600de4ebc1eae182887 100644 --- a/crates/notebook/tests/support/live.rs +++ b/crates/notebook/tests/support/live.rs @@ -285,7 +285,10 @@ impl PresenceRelay { impl Drop for PresenceRelay { fn drop(&mut self) { { - let mut state = self.state.lock().unwrap(); + let mut state = self + .state + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); state.stopped = true; for (_, socket) in &state.sockets { let _ = socket.shutdown(std::net::Shutdown::Both); -- 2.54.1