authoravatarclover caruso<git@paperclover.net>2026-10-19 12:46:19-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:57:03-07:00
logaa378b005697f7ce0e295aa865a7d46afa0e3f78
tree1d5265ebe40d033f2a6b75cf794b8615680cfe1d
parent61379e39c843ec6319c35620736e33904e058572
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

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

2 files changed, 66 insertions(+), 4 deletions(-)

crates/notebook/src/live.rs+37-4
...@@ -555,13 +555,19 @@ impl Shared {...@@ -555,13 +555,19 @@ impl Shared {
555 let shared = Arc::downgrade(self);555 let shared = Arc::downgrade(self);
556 let dialing = thread::spawn(move || {556 let dialing = thread::spawn(move || {
557 let mut wait = Duration::from_secs(1);557 let mut wait = Duration::from_secs(1);
558 let ours = |state: &State| {
559 (state.dialing.get(&address))
560 .is_some_and(|dial| dial.id() == thread::current().id())
561 };
558 loop {562 loop {
559 let Some(live) = shared.upgrade() else {563 let Some(live) = shared.upgrade() else {
560 return;564 return;
561 };565 };
562 if live.stopped.load(Ordering::Acquire) || live.state.lock().unwrap().burned {566 let state = live.state.lock().unwrap();
567 if live.stopped.load(Ordering::Acquire) || state.burned || !ours(&state) {
563 break;568 break;
564 }569 }
570 drop(state);
565 let Some(tag) = live.tag() else {571 let Some(tag) = live.tag() else {
566 break;572 break;
567 };573 };
...@@ -582,12 +588,22 @@ impl Shared {...@@ -582,12 +588,22 @@ impl Shared {
582 wait = (wait * 2).min(PATIENCE);588 wait = (wait * 2).min(PATIENCE);
583 }589 }
584 if let Some(live) = shared.upgrade() {590 if let Some(live) = shared.upgrade() {
585 live.state.lock().unwrap().dialing.remove(&address);591 let mut state = live.state.lock().unwrap();
592 if ours(&state) {
593 state.dialing.remove(&address);
594 }
586 }595 }
587 });596 });
588 state.dialing.insert(address, dialing.thread().clone());597 state.dialing.insert(address, dialing.thread().clone());
589 }598 }
590599
600 /// Stops dialling `address` once its connection, if any, ends.
601 fn undial(&self, address: SocketAddr) {
602 if let Some(dial) = self.state.lock().unwrap().dialing.remove(&address) {
603 dial.unpark();
604 }
605 }
606
591 /// Counts a meeting that failed on the secret: the end sharing a code burns it after too607 /// Counts a meeting that failed on the secret: the end sharing a code burns it after too
592 /// many, and an end that typed one gives up at once.608 /// many, and an end that typed one gives up at once.
593 fn failed(self: &Arc<Self>) {609 fn failed(self: &Arc<Self>) {
...@@ -823,9 +839,20 @@ fn discover(...@@ -823,9 +839,20 @@ fn discover(
823 thread::Builder::new()839 thread::Builder::new()
824 .name("live discovery".into())840 .name("live discovery".into())
825 .spawn(move || {841 .spawn(move || {
842 // Where each peer found was dialled, to stop once it is no longer advertised.
843 let mut dialled = BTreeMap::new();
826 while let Ok(event) = found.recv() {844 while let Ok(event) = found.recv() {
827 let ServiceEvent::ServiceResolved(service) = event else {845 let service = match event {
828 continue;846 ServiceEvent::ServiceResolved(service) => service,
847 ServiceEvent::ServiceRemoved(_, name) => {
848 if let (Some(address), Some(shared)) =
849 (dialled.remove(&name), shared.upgrade())
850 {
851 shared.undial(address);
852 }
853 continue;
854 }
855 _ => continue,
829 };856 };
830 let Some(shared) = shared.upgrade() else {857 let Some(shared) = shared.upgrade() else {
831 return;858 return;
...@@ -844,6 +871,12 @@ fn discover(...@@ -844,6 +871,12 @@ fn discover(
844 addresses.sort_by_key(|ip| (!ip.is_ipv4(), !ip.is_loopback()));871 addresses.sort_by_key(|ip| (!ip.is_ipv4(), !ip.is_loopback()));
845 if let Some(ip) = addresses.first() {872 if let Some(ip) = addresses.first() {
846 let address = SocketAddr::new(*ip, service.port);873 let address = SocketAddr::new(*ip, service.port);
874 // A peer found again at another address is no longer at the old one.
875 if let Some(old) = dialled.insert(service.fullname.clone(), address)
876 && old != address
877 {
878 shared.undial(old);
879 }
847 shared.dial(address);880 shared.dial(address);
848 }881 }
849 }882 }
crates/notebook/src/live/tests.rs+29
...@@ -1198,3 +1198,32 @@ fn lan_redials_at_once_after_a_meeting() {...@@ -1198,3 +1198,32 @@ fn lan_redials_at_once_after_a_meeting() {
1198 again - broken1198 again - broken
1199 );1199 );
1200}1200}
1201
1202/// A peer found by mDNS that quits is no longer dialled once discovery hears it go, as it
1203/// comes back, if at all, at another address.
1204#[test]
1205#[ignore = "multicasts mDNS on the loopback interface"]
1206fn lan_stops_dialling_a_peer_discovery_lost() {
1207 let room = Room::Notebook([79; 16]);
1208 let mut guest = hello("Guest");
1209 guest.peer = [1; 16];
1210 let mut host = hello("Host");
1211 host.peer = [2; 16];
1212 let guest = Live::start(guest, &room, Some(Reach::Loopback), None, |_| {}).unwrap();
1213 let host = Live::start(host, &room, Some(Reach::Loopback), None, |_| {}).unwrap();
1214 until(&guest, |peers| peers.len() == 1);
1215 let address = host.address();
1216 host.leave("left");
1217 let deadline = Instant::now() + Duration::from_secs(10);
1218 while !guest.shared.state.lock().unwrap().dialing.is_empty() {
1219 assert!(Instant::now() < deadline, "the gone peer is still dialled");
1220 thread::sleep(Duration::from_millis(20));
1221 }
1222 let listener = TcpListener::bind(address).unwrap();
1223 listener.set_nonblocking(true).unwrap();
1224 thread::sleep(Duration::from_secs(3));
1225 assert_eq!(
1226 listener.accept().unwrap_err().kind(),
1227 io::ErrorKind::WouldBlock
1228 );
1229}