From 5ef7269b5da51f5c9173f07bc7a7587fe5f46c83 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 19 Oct 2026 11:43:10 -0700 Subject: [PATCH] fix(live): a guest back through the relay replaces its silent stream A guest whose network changed under it joins the relay again in a new slot, while the relay still counts its old connection in the room and the host still holds the stream through it. The host refused the new stream as a duplicate after the guest had already heard its hello, so the guest believed itself connected while the host dropped everything it sent, until the old stream timed out and the host hung up its own room, about a minute later. A stream through the relay now replaces an earlier one through the relay, as only a newer slot can open it; a direct connection still keeps its place. A test silences a guest's relay connection without closing it and checks that its next stream reaches the host. - A Live Share guest whose network changed is served again as soon as it is back through the relay, instead of about a minute later. Assisted-by: claude-opus-5.5 --- crates/notebook/src/live.rs | 6 +- crates/notebook/src/live/tests.rs | 98 +++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 3 deletions(-) diff --git a/crates/notebook/src/live.rs b/crates/notebook/src/live.rs index f60240fc848c4eca71c7cece8a431146efa5d04d..94bfa0504688c249b9ead2470dc2b5df79b89e49 100644 --- a/crates/notebook/src/live.rs +++ b/crates/notebook/src/live.rs @@ -647,11 +647,11 @@ impl Shared { let retired = { let mut state = self.state.lock().unwrap(); // A peer met both directly and through a relay keeps the direct connection, as - // both ends then agree. + // both ends then agree. A stream through the relay comes only from a slot newer + // than the one it replaces, whose stream the relay may not yet have seen go. let kept = match state.peers.get(&peer) { _ if peer == self.me.peer => false, - Some(link) if link.pipe.direct() || !pipe.direct() => false, - Some(_) => true, + Some(link) => !link.pipe.direct(), None => true, }; if !kept { diff --git a/crates/notebook/src/live/tests.rs b/crates/notebook/src/live/tests.rs index 98876ac993ae662372bc9d290ff315b864f1cf56..c95dd930b550d08cf3fd176afac9300835f11faf 100644 --- a/crates/notebook/src/live/tests.rs +++ b/crates/notebook/src/live/tests.rs @@ -1045,3 +1045,101 @@ fn another_version_is_named_before_any_key() { // The older end heard this one's opening, and with it its version. assert!(!older.join().unwrap().is_empty()); } + +/// A TCP proxy to `upstream` whose connections so far fall silent, as a network left behind +/// does, when the flag it returns is raised; later ones pass. +fn blackhole(upstream: SocketAddr) -> (String, Arc) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("ws://{}", listener.local_addr().unwrap()); + let era = Arc::new(AtomicU64::new(0)); + let current = Arc::clone(&era); + thread::spawn(move || { + for client in listener.incoming() { + let (Ok(client), Ok(server)) = (client, TcpStream::connect(upstream)) else { + return; + }; + let born = current.load(Ordering::Acquire); + for (from, mut to) in [ + (client.try_clone().unwrap(), server.try_clone().unwrap()), + (server, client), + ] { + let current = Arc::clone(¤t); + thread::spawn(move || { + let mut buffer = [0; 16 << 10]; + while let Ok(read @ 1..) = from.read(&mut buffer) { + if current.load(Ordering::Acquire) == born + && to.write_all(&buffer[..read]).is_err() + { + return; + } + } + }); + } + } + }); + (url, era) +} + +/// A guest whose network went silent comes back through the relay in a new slot while the +/// host still holds its old stream, which the relay has not yet seen go; the host takes the +/// new stream. +#[test] +fn a_guest_back_through_the_relay_replaces_its_silent_stream() { + let (url, upstream) = relay(Default::default()); + let (proxied, era) = blackhole(upstream); + let room = Room::Notebook([76; 16]); + let mut host = hello("Host"); + host.serves = Some([77; 16]); + let (met, host_met) = mpsc::channel(); + let (frame, frames) = mpsc::channel(); + let host = Live::start(host, &room, None, Some(&url), move |event| match event { + Event::Met(_, line) => { + let _ = met.send(line.clone()); + } + Event::Frame { kind, body, .. } => { + let _ = frame.send((kind, body.to_vec())); + } + _ => {} + }) + .unwrap(); + let (met, guest_met) = mpsc::channel(); + let (frame, guest_frames) = mpsc::channel(); + let guest = Live::start( + hello("Guest"), + &room, + None, + Some(&proxied), + move |event| match event { + Event::Met(_, line) => { + let _ = met.send(line.clone()); + } + Event::Frame { kind, .. } => { + let _ = frame.send(kind); + } + _ => {} + }, + ) + .unwrap(); + let line = guest_met.recv_timeout(Duration::from_secs(10)).unwrap(); + let host_line = host_met.recv_timeout(Duration::from_secs(10)).unwrap(); + // The guest notices the silence first, as one whose network changed under it does. + let state = guest.shared.state.lock().unwrap(); + let link = state.peers.values().next().unwrap(); + link.pipe.set_read_timeout(Duration::from_secs(1)).unwrap(); + drop(state); + host_line.send(kind::EDITS, &0_u64).unwrap(); + assert_eq!( + guest_frames.recv_timeout(Duration::from_secs(10)).unwrap(), + kind::EDITS + ); + era.fetch_add(1, Ordering::AcqRel); + line.send(kind::EDITS, &1_u64).unwrap(); + let line = guest_met.recv_timeout(Duration::from_secs(15)).unwrap(); + line.send(kind::EDITS, &2_u64).unwrap(); + let (kind, body) = frames.recv_timeout(Duration::from_secs(10)).unwrap(); + assert_eq!( + (kind, minicbor::decode::(&body).unwrap()), + (kind::EDITS, 2) + ); + assert_eq!(host.peers().len(), 1); +} -- 2.54.1