authoravatarclover caruso<git@paperclover.net>2026-10-19 12:21:52-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:57:03-07:00
logfa9872370dfb4566ea85a9d909497dd2944f868a
tree2edcc0a55cdaa1290be94a3c92c6a1a55fc9a492
parent18a89c7d5115a98335626904ed081efcded373bb
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

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

2 files changed, 40 insertions(+), 6 deletions(-)

crates/notebook/src/live/relay.rs+36-5
......@@ -7,8 +7,8 @@
77//! peers then meet afresh.
88
99use super::{
10 Event, Hello, OPENING, PATIENCE, Paced, Peer, Pipe, Presence, Relayed as Answer, Room, Shared,
11 Side, code_parts, group,
10 Event, Hello, OPENING, Paced, Peer, Pipe, Presence, Relayed as Answer, Room, Shared, Side,
11 code_parts, group,
1212 transport::{self, Address, Failure, parse},
1313 wire::kind,
1414};
......@@ -25,6 +25,8 @@ use std::{
2525const KEEPALIVE: Duration = Duration::from_secs(30);
2626/// A connection that lasted this long was no failure, so the next waits only a second.
2727const STEADY: Duration = Duration::from_secs(60);
28/// The longest wait before joining again, so the room is back soon after a relay restarts.
29const REJOIN: Duration = Duration::from_secs(5);
2830/// The most sent in one message, well under any relay's cap.
2931const CHUNK: usize = 64 << 10;
3032/// The largest message read from the relay.
......@@ -64,6 +66,8 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) {
6466 .flatten();
6567 while !shared.stopped.load(Ordering::Acquire) {
6668 let began = Instant::now();
69 // The least the relay asked this end to wait.
70 let mut asked = Duration::ZERO;
6771 let path = match (owner, shared.tag()) {
6872 (false, Some(tag)) => format!("{}/v1/room/{tag}", address.path),
6973 (false, None) => return,
......@@ -88,7 +92,7 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) {
8892 if status == 410 {
8993 return;
9094 }
91 wait = wait.max(retry.unwrap_or_default());
95 asked = retry.unwrap_or_default();
9296 }
9397 Err(Failure::Trouble(trouble, error)) => {
9498 eprintln!("Live: no relay at {}: {error}", address.authority);
......@@ -98,11 +102,20 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) {
98102 if began.elapsed() >= STEADY {
99103 wait = Duration::from_secs(1);
100104 }
101 thread::sleep(wait);
102 wait = (wait * 2).min(PATIENCE);
105 thread::sleep(pause(wait, asked));
106 wait = (wait * 2).min(REJOIN);
103107 }
104108}
105109
110/// How long to wait before joining again: between half `wait` and all of it, so peers a
111/// restart hung up on come back apart, and never less than the relay `asked`.
112fn pause(wait: Duration, asked: Duration) -> Duration {
113 let mut random = [0; 4];
114 let _ = getrandom::fill(&mut random);
115 let share = f64::from(u32::from_le_bytes(random)) / f64::from(u32::MAX);
116 wait.mul_f64(0.5 + share / 2.0).max(asked)
117}
118
106119/// Opens the relay's `path` at `address`: the connection, and what reads its messages.
107120fn connect(
108121 shared: &Shared,
......@@ -565,6 +578,24 @@ impl Pipe for Relayed {
565578mod tests {
566579 use super::*;
567580
581 /// However long a relay stays away, the next try comes within seconds of its return,
582 /// tries spread apart, and none comes before the relay's Retry-After.
583 #[test]
584 fn rejoining_waits_a_little_spread_and_as_asked() {
585 let mut wait = Duration::from_secs(1);
586 let mut pauses = Vec::new();
587 for _ in 0..12 {
588 pauses.push(pause(wait, Duration::ZERO));
589 wait = (wait * 2).min(REJOIN);
590 }
591 assert!(
592 pauses.iter().all(|pause| *pause <= Duration::from_secs(5)),
593 "{pauses:?}"
594 );
595 assert!(pauses.windows(2).any(|two| two[0] != two[1]));
596 assert!(pause(wait, Duration::from_secs(30)) >= Duration::from_secs(30));
597 }
598
568599 #[test]
569600 fn relay_addresses_parse() {
570601 let address = parse("wss://live.example.net").unwrap();
crates/notebook/tests/support/live.rs+4-1
......@@ -285,7 +285,10 @@ impl PresenceRelay {
285285impl Drop for PresenceRelay {
286286 fn drop(&mut self) {
287287 {
288 let mut state = self.state.lock().unwrap();
288 let mut state = self
289 .state
290 .lock()
291 .unwrap_or_else(std::sync::PoisonError::into_inner);
289292 state.stopped = true;
290293 for (_, socket) in &state.sockets {
291294 let _ = socket.shutdown(std::net::Shutdown::Both);