From aa378b005697f7ce0e295aa865a7d46afa0e3f78 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 19 Oct 2026 12:46:19 -0700 Subject: [PATCH] fix(live): stop dialling a peer discovery no longer advertises A peer found on the network by mDNS was dialled at its address for as long as this end ran, every 30 seconds once its tries failed, though a peer that quits and starts again listens on another port, which discovery finds and dials anew. Each restart of a host so left one more dial knocking on a port no one holds, or someone else holds. Discovery now stops the dial to a peer once it hears the peer's advertisement go, or finds the peer again at another address; a connection open at the time is left to end on its own. A test on the loopback interface, ignored by default like the other one that multicasts, meets a host by mDNS, has it leave, and checks that its old address is no longer dialled. Assisted-by: claude-opus-5.5 --- crates/notebook/src/live.rs | 41 ++++++++++++++++++++++++++++--- crates/notebook/src/live/tests.rs | 29 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/crates/notebook/src/live.rs b/crates/notebook/src/live.rs index cd3a5856b1de3ac0443fad0f3713627dd14b637a..63eaecec6eac5188b8b6ecad3b73bdd5d9c6236c 100644 --- a/crates/notebook/src/live.rs +++ b/crates/notebook/src/live.rs @@ -555,13 +555,19 @@ impl Shared { let shared = Arc::downgrade(self); let dialing = thread::spawn(move || { let mut wait = Duration::from_secs(1); + let ours = |state: &State| { + (state.dialing.get(&address)) + .is_some_and(|dial| dial.id() == thread::current().id()) + }; loop { let Some(live) = shared.upgrade() else { return; }; - if live.stopped.load(Ordering::Acquire) || live.state.lock().unwrap().burned { + let state = live.state.lock().unwrap(); + if live.stopped.load(Ordering::Acquire) || state.burned || !ours(&state) { break; } + drop(state); let Some(tag) = live.tag() else { break; }; @@ -582,12 +588,22 @@ impl Shared { wait = (wait * 2).min(PATIENCE); } if let Some(live) = shared.upgrade() { - live.state.lock().unwrap().dialing.remove(&address); + let mut state = live.state.lock().unwrap(); + if ours(&state) { + state.dialing.remove(&address); + } } }); state.dialing.insert(address, dialing.thread().clone()); } + /// Stops dialling `address` once its connection, if any, ends. + fn undial(&self, address: SocketAddr) { + if let Some(dial) = self.state.lock().unwrap().dialing.remove(&address) { + dial.unpark(); + } + } + /// Counts a meeting that failed on the secret: the end sharing a code burns it after too /// many, and an end that typed one gives up at once. fn failed(self: &Arc) { @@ -823,9 +839,20 @@ fn discover( thread::Builder::new() .name("live discovery".into()) .spawn(move || { + // Where each peer found was dialled, to stop once it is no longer advertised. + let mut dialled = BTreeMap::new(); while let Ok(event) = found.recv() { - let ServiceEvent::ServiceResolved(service) = event else { - continue; + let service = match event { + ServiceEvent::ServiceResolved(service) => service, + ServiceEvent::ServiceRemoved(_, name) => { + if let (Some(address), Some(shared)) = + (dialled.remove(&name), shared.upgrade()) + { + shared.undial(address); + } + continue; + } + _ => continue, }; let Some(shared) = shared.upgrade() else { return; @@ -844,6 +871,12 @@ fn discover( addresses.sort_by_key(|ip| (!ip.is_ipv4(), !ip.is_loopback())); if let Some(ip) = addresses.first() { let address = SocketAddr::new(*ip, service.port); + // A peer found again at another address is no longer at the old one. + if let Some(old) = dialled.insert(service.fullname.clone(), address) + && old != address + { + shared.undial(old); + } shared.dial(address); } } diff --git a/crates/notebook/src/live/tests.rs b/crates/notebook/src/live/tests.rs index f443c52353234cc59d48ceabaec5b7fd29577ceb..68e240db92bda5c9a7ce93ff3da4d11db2d786a1 100644 --- a/crates/notebook/src/live/tests.rs +++ b/crates/notebook/src/live/tests.rs @@ -1198,3 +1198,32 @@ fn lan_redials_at_once_after_a_meeting() { again - broken ); } + +/// A peer found by mDNS that quits is no longer dialled once discovery hears it go, as it +/// comes back, if at all, at another address. +#[test] +#[ignore = "multicasts mDNS on the loopback interface"] +fn lan_stops_dialling_a_peer_discovery_lost() { + let room = Room::Notebook([79; 16]); + let mut guest = hello("Guest"); + guest.peer = [1; 16]; + let mut host = hello("Host"); + host.peer = [2; 16]; + let guest = Live::start(guest, &room, Some(Reach::Loopback), None, |_| {}).unwrap(); + let host = Live::start(host, &room, Some(Reach::Loopback), None, |_| {}).unwrap(); + until(&guest, |peers| peers.len() == 1); + let address = host.address(); + host.leave("left"); + let deadline = Instant::now() + Duration::from_secs(10); + while !guest.shared.state.lock().unwrap().dialing.is_empty() { + assert!(Instant::now() < deadline, "the gone peer is still dialled"); + thread::sleep(Duration::from_millis(20)); + } + let listener = TcpListener::bind(address).unwrap(); + listener.set_nonblocking(true).unwrap(); + thread::sleep(Duration::from_secs(3)); + assert_eq!( + listener.accept().unwrap_err().kind(), + io::ErrorKind::WouldBlock + ); +} -- 2.54.1