authoravatarclover caruso<git@paperclover.net>2026-10-19 11:43:10-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:57:03-07:00
log5ef7269b5da51f5c9173f07bc7a7587fe5f46c83
tree9fe60973f8ae0ef957d35e80e77d0140359cff17
parentff83f397b54c323bb8e8bf8b767eca746d301928
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

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

2 files changed, 101 insertions(+), 3 deletions(-)

crates/notebook/src/live.rs+3-3
......@@ -647,11 +647,11 @@ impl Shared {
647647 let retired = {
648648 let mut state = self.state.lock().unwrap();
649649 // A peer met both directly and through a relay keeps the direct connection, as
650 // both ends then agree.
650 // both ends then agree. A stream through the relay comes only from a slot newer
651 // than the one it replaces, whose stream the relay may not yet have seen go.
651652 let kept = match state.peers.get(&peer) {
652653 _ if peer == self.me.peer => false,
653 Some(link) if link.pipe.direct() || !pipe.direct() => false,
654 Some(_) => true,
654 Some(link) => !link.pipe.direct(),
655655 None => true,
656656 };
657657 if !kept {
crates/notebook/src/live/tests.rs+98
......@@ -1045,3 +1045,101 @@ fn another_version_is_named_before_any_key() {
10451045 // The older end heard this one's opening, and with it its version.
10461046 assert!(!older.join().unwrap().is_empty());
10471047}
1048
1049/// A TCP proxy to `upstream` whose connections so far fall silent, as a network left behind
1050/// does, when the flag it returns is raised; later ones pass.
1051fn blackhole(upstream: SocketAddr) -> (String, Arc<AtomicU64>) {
1052 let listener = TcpListener::bind("127.0.0.1:0").unwrap();
1053 let url = format!("ws://{}", listener.local_addr().unwrap());
1054 let era = Arc::new(AtomicU64::new(0));
1055 let current = Arc::clone(&era);
1056 thread::spawn(move || {
1057 for client in listener.incoming() {
1058 let (Ok(client), Ok(server)) = (client, TcpStream::connect(upstream)) else {
1059 return;
1060 };
1061 let born = current.load(Ordering::Acquire);
1062 for (from, mut to) in [
1063 (client.try_clone().unwrap(), server.try_clone().unwrap()),
1064 (server, client),
1065 ] {
1066 let current = Arc::clone(&current);
1067 thread::spawn(move || {
1068 let mut buffer = [0; 16 << 10];
1069 while let Ok(read @ 1..) = from.read(&mut buffer) {
1070 if current.load(Ordering::Acquire) == born
1071 && to.write_all(&buffer[..read]).is_err()
1072 {
1073 return;
1074 }
1075 }
1076 });
1077 }
1078 }
1079 });
1080 (url, era)
1081}
1082
1083/// A guest whose network went silent comes back through the relay in a new slot while the
1084/// host still holds its old stream, which the relay has not yet seen go; the host takes the
1085/// new stream.
1086#[test]
1087fn a_guest_back_through_the_relay_replaces_its_silent_stream() {
1088 let (url, upstream) = relay(Default::default());
1089 let (proxied, era) = blackhole(upstream);
1090 let room = Room::Notebook([76; 16]);
1091 let mut host = hello("Host");
1092 host.serves = Some([77; 16]);
1093 let (met, host_met) = mpsc::channel();
1094 let (frame, frames) = mpsc::channel();
1095 let host = Live::start(host, &room, None, Some(&url), move |event| match event {
1096 Event::Met(_, line) => {
1097 let _ = met.send(line.clone());
1098 }
1099 Event::Frame { kind, body, .. } => {
1100 let _ = frame.send((kind, body.to_vec()));
1101 }
1102 _ => {}
1103 })
1104 .unwrap();
1105 let (met, guest_met) = mpsc::channel();
1106 let (frame, guest_frames) = mpsc::channel();
1107 let guest = Live::start(
1108 hello("Guest"),
1109 &room,
1110 None,
1111 Some(&proxied),
1112 move |event| match event {
1113 Event::Met(_, line) => {
1114 let _ = met.send(line.clone());
1115 }
1116 Event::Frame { kind, .. } => {
1117 let _ = frame.send(kind);
1118 }
1119 _ => {}
1120 },
1121 )
1122 .unwrap();
1123 let line = guest_met.recv_timeout(Duration::from_secs(10)).unwrap();
1124 let host_line = host_met.recv_timeout(Duration::from_secs(10)).unwrap();
1125 // The guest notices the silence first, as one whose network changed under it does.
1126 let state = guest.shared.state.lock().unwrap();
1127 let link = state.peers.values().next().unwrap();
1128 link.pipe.set_read_timeout(Duration::from_secs(1)).unwrap();
1129 drop(state);
1130 host_line.send(kind::EDITS, &0_u64).unwrap();
1131 assert_eq!(
1132 guest_frames.recv_timeout(Duration::from_secs(10)).unwrap(),
1133 kind::EDITS
1134 );
1135 era.fetch_add(1, Ordering::AcqRel);
1136 line.send(kind::EDITS, &1_u64).unwrap();
1137 let line = guest_met.recv_timeout(Duration::from_secs(15)).unwrap();
1138 line.send(kind::EDITS, &2_u64).unwrap();
1139 let (kind, body) = frames.recv_timeout(Duration::from_secs(10)).unwrap();
1140 assert_eq!(
1141 (kind, minicbor::decode::<u64>(&body).unwrap()),
1142 (kind::EDITS, 2)
1143 );
1144 assert_eq!(host.peers().len(), 1);
1145}