authoravatarclover caruso<git@paperclover.net>2026-10-19 12:45:49-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:57:03-07:00
log61379e39c843ec6319c35620736e33904e058572
tree6c193a212af9871f25f164e2836f236f6fa97fc6
parent9e5508385b28d6be4fef73d7e6afb795261a690d
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix(live): a dial meets its peer again at once after a meeting

A dial to a peer on the network waits twice as long before each new try, up to 30 seconds, and nothing brought the wait back down once the peers met. A guest whose first tries were refused, as when it opens the notebook before the host has started sharing, so waited 8 seconds or more before meeting again whenever its connection to the host broke later. The wait now starts over at a second after a meeting that lasted past an opening's time; one the peer ends at once, as when it refuses a duplicate after its hello, still counts as a failed try, so such a peer is not dialled every second. A test meets a host only on the fourth try, holds the meeting, breaks it, and checks that the two meet again within 4 seconds, where they took 8 before. - A Live Share guest on the same network as the host reconnects within a second or two after a dropped connection. Assisted-by: claude-opus-5.5

3 files changed, 81 insertions(+), 13 deletions(-)

crates/notebook/src/live.rs+24-8
......@@ -137,6 +137,15 @@ struct Link {
137137 pipe: Arc<dyn Pipe>,
138138}
139139
140/// How a connection to a peer went, as the next dial to it takes it.
141enum Ran {
142 Met,
143 /// It broke before the peers met.
144 Failed,
145 /// Another try would end the same way.
146 Hopeless,
147}
148
140149/// What a connection's writer sends next.
141150enum Out {
142151 /// The newest presence.
......@@ -561,8 +570,12 @@ impl Shared {
561570 let Some(live) = shared.upgrade() else {
562571 return;
563572 };
564 if !live.run(Arc::new(stream), Side::Initiator, &tag) {
565 break;
573 let began = Instant::now();
574 match live.run(Arc::new(stream), Side::Initiator, &tag) {
575 // One the peer ends at once, as a duplicate it refuses, is no meeting.
576 Ran::Met if began.elapsed() >= OPENING => wait = Duration::from_secs(1),
577 Ran::Met | Ran::Failed => {}
578 Ran::Hopeless => break,
566579 }
567580 }
568581 thread::park_timeout(wait);
......@@ -590,11 +603,11 @@ impl Shared {
590603 }
591604
592605 /// Meets the peer at the other end of `pipe` in room `tag`, then reads from it until it
593 /// goes: whether the next dial may try again.
594 fn run(self: Arc<Self>, pipe: Arc<dyn Pipe>, side: Side, tag: &str) -> bool {
606 /// goes.
607 fn run(self: Arc<Self>, pipe: Arc<dyn Pipe>, side: Side, tag: &str) -> Ran {
595608 if self.stopped.load(Ordering::Acquire) || self.state.lock().unwrap().burned {
596609 pipe.shutdown();
597 return false;
610 return Ran::Hopeless;
598611 }
599612 let mut stream: &dyn Pipe = &*pipe;
600613 let met = (|| {
......@@ -635,7 +648,10 @@ impl Shared {
635648 pipe.broken();
636649 }
637650 pipe.shutdown();
638 return other.is_none() && error.kind() != io::ErrorKind::InvalidData;
651 return match other.is_none() && error.kind() != io::ErrorKind::InvalidData {
652 true => Ran::Failed,
653 false => Ran::Hopeless,
654 };
639655 }
640656 };
641657 let peer = hello.peer;
......@@ -657,7 +673,7 @@ impl Shared {
657673 if !kept {
658674 drop(state);
659675 pipe.shutdown();
660 return false;
676 return Ran::Hopeless;
661677 }
662678 state.peers.insert(
663679 peer,
......@@ -733,7 +749,7 @@ impl Shared {
733749 (self.events)(&self, Event::Left(&hello));
734750 (self.events)(&self, Event::Changed);
735751 }
736 true
752 Ran::Met
737753 }
738754
739755 /// Sends the newest presence when woken, at most every `PRESENCE_EVERY`, frames as they
crates/notebook/src/live/share/handover_tests.rs+2-5
......@@ -82,7 +82,7 @@ struct Handover {
8282 shared: Arc<Shared>,
8383 guest: Arc<Inner>,
8484 pipes: Vec<Arc<HandoverPipe>>,
85 readers: Vec<(mpsc::Receiver<()>, thread::JoinHandle<bool>)>,
85 readers: Vec<(mpsc::Receiver<()>, thread::JoinHandle<()>)>,
8686 retired: (mpsc::Sender<()>, mpsc::Receiver<()>),
8787 observed: mpsc::Receiver<()>,
8888 releases: Vec<mpsc::Sender<()>>,
......@@ -165,9 +165,8 @@ impl Handover {
165165 let reader_tag = tag.clone();
166166 let (finished, done) = mpsc::channel();
167167 let reader = thread::spawn(move || {
168 let retry = shared.run(pipe, wire::Side::Responder, &reader_tag);
168 shared.run(pipe, wire::Side::Responder, &reader_tag);
169169 let _ = finished.send(());
170 retry
171170 });
172171 self.readers.push((done, reader));
173172 let (mut send, mut receive) = wire::open(
......@@ -594,7 +593,6 @@ fn handover_deferred_presence_cannot_replace_promoted_host_room() {
594593 entered.send(()).unwrap();
595594 guest.join_presence(&source, &from, secret);
596595 let _ = finished.send(());
597 true
598596 });
599597 fixture.readers.push((done, installer));
600598 started.recv_timeout(OPENING).unwrap();
......@@ -640,7 +638,6 @@ fn handover_presence_install_seeds_latest_here() {
640638 entered.send(()).unwrap();
641639 guest.join_presence(&source, &from, secret);
642640 let _ = finished.send(());
643 true
644641 });
645642 fixture.readers.push((done, installer));
646643 started.recv_timeout(OPENING).unwrap();
crates/notebook/src/live/tests.rs+55
......@@ -1143,3 +1143,58 @@ fn a_guest_back_through_the_relay_replaces_its_silent_stream() {
11431143 );
11441144 assert_eq!(host.peers().len(), 1);
11451145}
1146
1147/// A dial that took a few tries to meet its peer meets it again at once when the meeting,
1148/// having lasted, breaks, not after the wait its first tries grew to.
1149#[test]
1150fn lan_redials_at_once_after_a_meeting() {
1151 let room = Room::Notebook([78; 16]);
1152 let host = Live::start(hello("Host"), &room, None, None, |_| {}).unwrap();
1153 let address = TcpListener::bind("127.0.0.1:0")
1154 .unwrap()
1155 .local_addr()
1156 .unwrap();
1157 let (met, meetings) = mpsc::channel();
1158 let guest = Live::start(hello("Guest"), &room, None, None, move |event| {
1159 if let Event::Met(..) = event {
1160 let _ = met.send(Instant::now());
1161 }
1162 })
1163 .unwrap();
1164 guest.connect(address);
1165 // Refused three times, the dial waits 8 seconds before a fourth try.
1166 thread::sleep(Duration::from_millis(3500));
1167 let listener = TcpListener::bind(address).unwrap();
1168 let upstream = host.address();
1169 let clients = Arc::new(Mutex::new(Vec::new()));
1170 let accepted = Arc::clone(&clients);
1171 thread::spawn(move || {
1172 for client in listener.incoming() {
1173 let (Ok(client), Ok(server)) = (client, TcpStream::connect(upstream)) else {
1174 return;
1175 };
1176 accepted.lock().unwrap().push(client.try_clone().unwrap());
1177 for (mut from, mut to) in [
1178 (client.try_clone().unwrap(), server.try_clone().unwrap()),
1179 (server, client),
1180 ] {
1181 thread::spawn(move || {
1182 let _ = io::copy(&mut from, &mut to);
1183 let _ = to.shutdown(Shutdown::Both);
1184 });
1185 }
1186 }
1187 });
1188 meetings.recv_timeout(Duration::from_secs(15)).unwrap();
1189 thread::sleep(OPENING + Duration::from_millis(500));
1190 for client in clients.lock().unwrap().drain(..) {
1191 client.shutdown(Shutdown::Both).unwrap();
1192 }
1193 let broken = Instant::now();
1194 let again = meetings.recv_timeout(Duration::from_secs(15)).unwrap();
1195 assert!(
1196 again - broken < Duration::from_secs(4),
1197 "met again {:?} after the connection broke",
1198 again - broken
1199 );
1200}