| ... | @@ -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 | } |
| 590 | | 599 | |
| | 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 too | 607 | /// 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 | } |