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 @@...@@ -7,8 +7,8 @@
7//! peers then meet afresh.7//! peers then meet afresh.
88
9use super::{9use super::{
10 Event, Hello, OPENING, PATIENCE, Paced, Peer, Pipe, Presence, Relayed as Answer, Room, Shared,10 Event, Hello, OPENING, Paced, Peer, Pipe, Presence, Relayed as Answer, Room, Shared, Side,
11 Side, code_parts, group,11 code_parts, group,
12 transport::{self, Address, Failure, parse},12 transport::{self, Address, Failure, parse},
13 wire::kind,13 wire::kind,
14};14};
...@@ -25,6 +25,8 @@ use std::{...@@ -25,6 +25,8 @@ use std::{
25const KEEPALIVE: Duration = Duration::from_secs(30);25const KEEPALIVE: Duration = Duration::from_secs(30);
26/// A connection that lasted this long was no failure, so the next waits only a second.26/// A connection that lasted this long was no failure, so the next waits only a second.
27const STEADY: Duration = Duration::from_secs(60);27const 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);
28/// The most sent in one message, well under any relay's cap.30/// The most sent in one message, well under any relay's cap.
29const CHUNK: usize = 64 << 10;31const CHUNK: usize = 64 << 10;
30/// The largest message read from the relay.32/// The largest message read from the relay.
...@@ -64,6 +66,8 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) {...@@ -64,6 +66,8 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) {
64 .flatten();66 .flatten();
65 while !shared.stopped.load(Ordering::Acquire) {67 while !shared.stopped.load(Ordering::Acquire) {
66 let began = Instant::now();68 let began = Instant::now();
69 // The least the relay asked this end to wait.
70 let mut asked = Duration::ZERO;
67 let path = match (owner, shared.tag()) {71 let path = match (owner, shared.tag()) {
68 (false, Some(tag)) => format!("{}/v1/room/{tag}", address.path),72 (false, Some(tag)) => format!("{}/v1/room/{tag}", address.path),
69 (false, None) => return,73 (false, None) => return,
...@@ -88,7 +92,7 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) {...@@ -88,7 +92,7 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) {
88 if status == 410 {92 if status == 410 {
89 return;93 return;
90 }94 }
91 wait = wait.max(retry.unwrap_or_default());95 asked = retry.unwrap_or_default();
92 }96 }
93 Err(Failure::Trouble(trouble, error)) => {97 Err(Failure::Trouble(trouble, error)) => {
94 eprintln!("Live: no relay at {}: {error}", address.authority);98 eprintln!("Live: no relay at {}: {error}", address.authority);
...@@ -98,11 +102,20 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) {...@@ -98,11 +102,20 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) {
98 if began.elapsed() >= STEADY {102 if began.elapsed() >= STEADY {
99 wait = Duration::from_secs(1);103 wait = Duration::from_secs(1);
100 }104 }
101 thread::sleep(wait);105 thread::sleep(pause(wait, asked));
102 wait = (wait * 2).min(PATIENCE);106 wait = (wait * 2).min(REJOIN);
103 }107 }
104}108}
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
106/// Opens the relay's `path` at `address`: the connection, and what reads its messages.119/// Opens the relay's `path` at `address`: the connection, and what reads its messages.
107fn connect(120fn connect(
108 shared: &Shared,121 shared: &Shared,
...@@ -565,6 +578,24 @@ impl Pipe for Relayed {...@@ -565,6 +578,24 @@ impl Pipe for Relayed {
565mod tests {578mod tests {
566 use super::*;579 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
568 #[test]599 #[test]
569 fn relay_addresses_parse() {600 fn relay_addresses_parse() {
570 let address = parse("wss://live.example.net").unwrap();601 let address = parse("wss://live.example.net").unwrap();
crates/notebook/tests/support/live.rs+4-1
...@@ -285,7 +285,10 @@ impl PresenceRelay {...@@ -285,7 +285,10 @@ impl PresenceRelay {
285impl Drop for PresenceRelay {285impl Drop for PresenceRelay {
286 fn drop(&mut self) {286 fn drop(&mut self) {
287 {287 {
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);
289 state.stopped = true;292 state.stopped = true;
290 for (_, socket) in &state.sockets {293 for (_, socket) in &state.sockets {
291 let _ = socket.shutdown(std::net::Shutdown::Both);294 let _ = socket.shutdown(std::net::Shutdown::Both);