From 61379e39c843ec6319c35620736e33904e058572 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 19 Oct 2026 12:45:49 -0700 Subject: [PATCH] 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 --- crates/notebook/src/live.rs | 32 ++++++++--- .../notebook/src/live/share/handover_tests.rs | 7 +-- crates/notebook/src/live/tests.rs | 55 +++++++++++++++++++ 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/crates/notebook/src/live.rs b/crates/notebook/src/live.rs index 94bfa0504688c249b9ead2470dc2b5df79b89e49..cd3a5856b1de3ac0443fad0f3713627dd14b637a 100644 --- a/crates/notebook/src/live.rs +++ b/crates/notebook/src/live.rs @@ -137,6 +137,15 @@ struct Link { pipe: Arc, } +/// How a connection to a peer went, as the next dial to it takes it. +enum Ran { + Met, + /// It broke before the peers met. + Failed, + /// Another try would end the same way. + Hopeless, +} + /// What a connection's writer sends next. enum Out { /// The newest presence. @@ -561,8 +570,12 @@ impl Shared { let Some(live) = shared.upgrade() else { return; }; - if !live.run(Arc::new(stream), Side::Initiator, &tag) { - break; + let began = Instant::now(); + match live.run(Arc::new(stream), Side::Initiator, &tag) { + // One the peer ends at once, as a duplicate it refuses, is no meeting. + Ran::Met if began.elapsed() >= OPENING => wait = Duration::from_secs(1), + Ran::Met | Ran::Failed => {} + Ran::Hopeless => break, } } thread::park_timeout(wait); @@ -590,11 +603,11 @@ impl Shared { } /// Meets the peer at the other end of `pipe` in room `tag`, then reads from it until it - /// goes: whether the next dial may try again. - fn run(self: Arc, pipe: Arc, side: Side, tag: &str) -> bool { + /// goes. + fn run(self: Arc, pipe: Arc, side: Side, tag: &str) -> Ran { if self.stopped.load(Ordering::Acquire) || self.state.lock().unwrap().burned { pipe.shutdown(); - return false; + return Ran::Hopeless; } let mut stream: &dyn Pipe = &*pipe; let met = (|| { @@ -635,7 +648,10 @@ impl Shared { pipe.broken(); } pipe.shutdown(); - return other.is_none() && error.kind() != io::ErrorKind::InvalidData; + return match other.is_none() && error.kind() != io::ErrorKind::InvalidData { + true => Ran::Failed, + false => Ran::Hopeless, + }; } }; let peer = hello.peer; @@ -657,7 +673,7 @@ impl Shared { if !kept { drop(state); pipe.shutdown(); - return false; + return Ran::Hopeless; } state.peers.insert( peer, @@ -733,7 +749,7 @@ impl Shared { (self.events)(&self, Event::Left(&hello)); (self.events)(&self, Event::Changed); } - true + Ran::Met } /// Sends the newest presence when woken, at most every `PRESENCE_EVERY`, frames as they diff --git a/crates/notebook/src/live/share/handover_tests.rs b/crates/notebook/src/live/share/handover_tests.rs index d46d64cda29f8c57d4c79587f28fd553b7ca908c..65e49abf61d169b78a9f5e350316fe63954c3a67 100644 --- a/crates/notebook/src/live/share/handover_tests.rs +++ b/crates/notebook/src/live/share/handover_tests.rs @@ -82,7 +82,7 @@ struct Handover { shared: Arc, guest: Arc, pipes: Vec>, - readers: Vec<(mpsc::Receiver<()>, thread::JoinHandle)>, + readers: Vec<(mpsc::Receiver<()>, thread::JoinHandle<()>)>, retired: (mpsc::Sender<()>, mpsc::Receiver<()>), observed: mpsc::Receiver<()>, releases: Vec>, @@ -165,9 +165,8 @@ impl Handover { let reader_tag = tag.clone(); let (finished, done) = mpsc::channel(); let reader = thread::spawn(move || { - let retry = shared.run(pipe, wire::Side::Responder, &reader_tag); + shared.run(pipe, wire::Side::Responder, &reader_tag); let _ = finished.send(()); - retry }); self.readers.push((done, reader)); let (mut send, mut receive) = wire::open( @@ -594,7 +593,6 @@ fn handover_deferred_presence_cannot_replace_promoted_host_room() { entered.send(()).unwrap(); guest.join_presence(&source, &from, secret); let _ = finished.send(()); - true }); fixture.readers.push((done, installer)); started.recv_timeout(OPENING).unwrap(); @@ -640,7 +638,6 @@ fn handover_presence_install_seeds_latest_here() { entered.send(()).unwrap(); guest.join_presence(&source, &from, secret); let _ = finished.send(()); - true }); fixture.readers.push((done, installer)); started.recv_timeout(OPENING).unwrap(); diff --git a/crates/notebook/src/live/tests.rs b/crates/notebook/src/live/tests.rs index c95dd930b550d08cf3fd176afac9300835f11faf..f443c52353234cc59d48ceabaec5b7fd29577ceb 100644 --- a/crates/notebook/src/live/tests.rs +++ b/crates/notebook/src/live/tests.rs @@ -1143,3 +1143,58 @@ fn a_guest_back_through_the_relay_replaces_its_silent_stream() { ); assert_eq!(host.peers().len(), 1); } + +/// A dial that took a few tries to meet its peer meets it again at once when the meeting, +/// having lasted, breaks, not after the wait its first tries grew to. +#[test] +fn lan_redials_at_once_after_a_meeting() { + let room = Room::Notebook([78; 16]); + let host = Live::start(hello("Host"), &room, None, None, |_| {}).unwrap(); + let address = TcpListener::bind("127.0.0.1:0") + .unwrap() + .local_addr() + .unwrap(); + let (met, meetings) = mpsc::channel(); + let guest = Live::start(hello("Guest"), &room, None, None, move |event| { + if let Event::Met(..) = event { + let _ = met.send(Instant::now()); + } + }) + .unwrap(); + guest.connect(address); + // Refused three times, the dial waits 8 seconds before a fourth try. + thread::sleep(Duration::from_millis(3500)); + let listener = TcpListener::bind(address).unwrap(); + let upstream = host.address(); + let clients = Arc::new(Mutex::new(Vec::new())); + let accepted = Arc::clone(&clients); + thread::spawn(move || { + for client in listener.incoming() { + let (Ok(client), Ok(server)) = (client, TcpStream::connect(upstream)) else { + return; + }; + accepted.lock().unwrap().push(client.try_clone().unwrap()); + for (mut from, mut to) in [ + (client.try_clone().unwrap(), server.try_clone().unwrap()), + (server, client), + ] { + thread::spawn(move || { + let _ = io::copy(&mut from, &mut to); + let _ = to.shutdown(Shutdown::Both); + }); + } + } + }); + meetings.recv_timeout(Duration::from_secs(15)).unwrap(); + thread::sleep(OPENING + Duration::from_millis(500)); + for client in clients.lock().unwrap().drain(..) { + client.shutdown(Shutdown::Both).unwrap(); + } + let broken = Instant::now(); + let again = meetings.recv_timeout(Duration::from_secs(15)).unwrap(); + assert!( + again - broken < Duration::from_secs(4), + "met again {:?} after the connection broke", + again - broken + ); +} -- 2.54.1