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 {...@@ -137,6 +137,15 @@ struct Link {
137 pipe: Arc<dyn Pipe>,137 pipe: Arc<dyn Pipe>,
138}138}
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
140/// What a connection's writer sends next.149/// What a connection's writer sends next.
141enum Out {150enum Out {
142 /// The newest presence.151 /// The newest presence.
...@@ -561,8 +570,12 @@ impl Shared {...@@ -561,8 +570,12 @@ impl Shared {
561 let Some(live) = shared.upgrade() else {570 let Some(live) = shared.upgrade() else {
562 return;571 return;
563 };572 };
564 if !live.run(Arc::new(stream), Side::Initiator, &tag) {573 let began = Instant::now();
565 break;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,
566 }579 }
567 }580 }
568 thread::park_timeout(wait);581 thread::park_timeout(wait);
...@@ -590,11 +603,11 @@ impl Shared {...@@ -590,11 +603,11 @@ impl Shared {
590 }603 }
591604
592 /// Meets the peer at the other end of `pipe` in room `tag`, then reads from it until it605 /// 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.606 /// goes.
594 fn run(self: Arc<Self>, pipe: Arc<dyn Pipe>, side: Side, tag: &str) -> bool {607 fn run(self: Arc<Self>, pipe: Arc<dyn Pipe>, side: Side, tag: &str) -> Ran {
595 if self.stopped.load(Ordering::Acquire) || self.state.lock().unwrap().burned {608 if self.stopped.load(Ordering::Acquire) || self.state.lock().unwrap().burned {
596 pipe.shutdown();609 pipe.shutdown();
597 return false;610 return Ran::Hopeless;
598 }611 }
599 let mut stream: &dyn Pipe = &*pipe;612 let mut stream: &dyn Pipe = &*pipe;
600 let met = (|| {613 let met = (|| {
...@@ -635,7 +648,10 @@ impl Shared {...@@ -635,7 +648,10 @@ impl Shared {
635 pipe.broken();648 pipe.broken();
636 }649 }
637 pipe.shutdown();650 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 };
639 }655 }
640 };656 };
641 let peer = hello.peer;657 let peer = hello.peer;
...@@ -657,7 +673,7 @@ impl Shared {...@@ -657,7 +673,7 @@ impl Shared {
657 if !kept {673 if !kept {
658 drop(state);674 drop(state);
659 pipe.shutdown();675 pipe.shutdown();
660 return false;676 return Ran::Hopeless;
661 }677 }
662 state.peers.insert(678 state.peers.insert(
663 peer,679 peer,
...@@ -733,7 +749,7 @@ impl Shared {...@@ -733,7 +749,7 @@ impl Shared {
733 (self.events)(&self, Event::Left(&hello));749 (self.events)(&self, Event::Left(&hello));
734 (self.events)(&self, Event::Changed);750 (self.events)(&self, Event::Changed);
735 }751 }
736 true752 Ran::Met
737 }753 }
738754
739 /// Sends the newest presence when woken, at most every `PRESENCE_EVERY`, frames as they755 /// 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 {...@@ -82,7 +82,7 @@ struct Handover {
82 shared: Arc<Shared>,82 shared: Arc<Shared>,
83 guest: Arc<Inner>,83 guest: Arc<Inner>,
84 pipes: Vec<Arc<HandoverPipe>>,84 pipes: Vec<Arc<HandoverPipe>>,
85 readers: Vec<(mpsc::Receiver<()>, thread::JoinHandle<bool>)>,85 readers: Vec<(mpsc::Receiver<()>, thread::JoinHandle<()>)>,
86 retired: (mpsc::Sender<()>, mpsc::Receiver<()>),86 retired: (mpsc::Sender<()>, mpsc::Receiver<()>),
87 observed: mpsc::Receiver<()>,87 observed: mpsc::Receiver<()>,
88 releases: Vec<mpsc::Sender<()>>,88 releases: Vec<mpsc::Sender<()>>,
...@@ -165,9 +165,8 @@ impl Handover {...@@ -165,9 +165,8 @@ impl Handover {
165 let reader_tag = tag.clone();165 let reader_tag = tag.clone();
166 let (finished, done) = mpsc::channel();166 let (finished, done) = mpsc::channel();
167 let reader = thread::spawn(move || {167 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);
169 let _ = finished.send(());169 let _ = finished.send(());
170 retry
171 });170 });
172 self.readers.push((done, reader));171 self.readers.push((done, reader));
173 let (mut send, mut receive) = wire::open(172 let (mut send, mut receive) = wire::open(
...@@ -594,7 +593,6 @@ fn handover_deferred_presence_cannot_replace_promoted_host_room() {...@@ -594,7 +593,6 @@ fn handover_deferred_presence_cannot_replace_promoted_host_room() {
594 entered.send(()).unwrap();593 entered.send(()).unwrap();
595 guest.join_presence(&source, &from, secret);594 guest.join_presence(&source, &from, secret);
596 let _ = finished.send(());595 let _ = finished.send(());
597 true
598 });596 });
599 fixture.readers.push((done, installer));597 fixture.readers.push((done, installer));
600 started.recv_timeout(OPENING).unwrap();598 started.recv_timeout(OPENING).unwrap();
...@@ -640,7 +638,6 @@ fn handover_presence_install_seeds_latest_here() {...@@ -640,7 +638,6 @@ fn handover_presence_install_seeds_latest_here() {
640 entered.send(()).unwrap();638 entered.send(()).unwrap();
641 guest.join_presence(&source, &from, secret);639 guest.join_presence(&source, &from, secret);
642 let _ = finished.send(());640 let _ = finished.send(());
643 true
644 });641 });
645 fixture.readers.push((done, installer));642 fixture.readers.push((done, installer));
646 started.recv_timeout(OPENING).unwrap();643 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() {...@@ -1143,3 +1143,58 @@ fn a_guest_back_through_the_relay_replaces_its_silent_stream() {
1143 );1143 );
1144 assert_eq!(host.peers().len(), 1);1144 assert_eq!(host.peers().len(), 1);
1145}1145}
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}