From 010448bef11cf5e5e5d09d6ffb43175438caeec6 Mon Sep 17 00:00:00 2001 From: TheAnimatrix Date: Tue, 1 Dec 2026 10:59:35 +0530 Subject: [PATCH 1/2] fix(caller): a name still resolving holds a ready tailnet candidate back for the LAN head start only A hostname being resolved may turn out to be LAN, so the tailnet waits for it, but now only for LAN_HEAD_START_MS. Before, a slow lookup of an unrelated name held a ready numeric tailnet candidate back until the lookup ended. Public, tunnel and SSH still wait for every name, and a name that resolves to the LAN within the head start still keeps the LAN first. --- crates/mousse-helper/tests/app_routes.rs | 91 ++++++++++++++++++++++++ crates/mousse-net-io/src/caller.rs | 22 ++++-- docs/limits.md | 2 +- docs/phase1b-design.md | 13 ++-- 4 files changed, 116 insertions(+), 12 deletions(-) diff --git a/crates/mousse-helper/tests/app_routes.rs b/crates/mousse-helper/tests/app_routes.rs index 38d66ef9..bd1a359a 100644 --- a/crates/mousse-helper/tests/app_routes.rs +++ b/crates/mousse-helper/tests/app_routes.rs @@ -192,6 +192,21 @@ impl Resolver for Answers { } } +/// A resolver that answers `answer` after `after`. +struct Late { + after: Duration, + answer: Vec, +} + +impl Resolver for Late { + fn resolve<'a>(&'a self, _name: &'a str, _port: u16) -> ResolveFuture<'a> { + Box::pin(async move { + tokio::time::sleep(self.after).await; + Ok(self.answer.clone()) + }) + } +} + async fn within(what: &str, limit: Duration, fut: impl std::future::Future) -> T { tokio::time::timeout(limit, fut) .await @@ -828,6 +843,82 @@ async fn a_name_is_ranked_by_the_address_it_resolves_to() { ); } +/// A name that is still being resolved may be LAN, so it holds a ready +/// tailnet candidate back for the LAN head start, and no longer: a slow +/// lookup of an unrelated name does not delay the tailnet until it ends. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_slow_name_holds_the_tailnet_back_for_the_lan_head_start_only() { + let e = engine().await; + let (tail, _) = relay(e.addr).await; + let mut caller = granted( + &e, + Arc::new(ManualClock::new()), + &[("LAN_HEAD_START_MS", 1_000), ("DIAL_TIMEOUT_MS", 10_000)], + ); + with_tailnet(&mut caller, &[tail]); + caller.use_resolver(Arc::new(Late { + after: Duration::from_secs(60), + answer: Vec::new(), + })); + let named = route("tcp://slow.test:31753"); + let t0 = Instant::now(); + let (_conn, reached) = within( + "the search", + Duration::from_secs(15), + caller.search(Search::of(vec![tcp(tail), named]), e.host.identity()), + ) + .await + .unwrap(); + let took = t0.elapsed(); + assert_eq!(reached.route, tcp(tail)); + assert_eq!(reached.class, RouteClass::Tailnet); + assert!( + took >= Duration::from_secs(1), + "no LAN head start: {took:?}" + ); + assert!( + took < Duration::from_secs(5), + "waited for the name: {took:?}" + ); +} + +/// A name that resolves to the LAN within the head start still keeps the +/// LAN's place: the faster tailnet candidate is never dialled. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_name_resolved_to_the_lan_within_the_head_start_beats_the_tailnet() { + let e = engine().await; + let (lan, _) = slow_relay(e.addr, Duration::from_millis(300)).await; + let (tail, tail_taken) = relay(e.addr).await; + let mut caller = granted( + &e, + Arc::new(ManualClock::new()), + &[("LAN_HEAD_START_MS", 3_000)], + ); + with_tailnet(&mut caller, &[tail]); + caller.use_resolver(Arc::new(Late { + after: Duration::from_millis(300), + answer: vec![lan], + })); + let named = route(&format!("tcp://desk.test:{}", lan.port())); + let (_conn, reached) = within( + "the search", + Duration::from_secs(15), + caller.search( + Search::of(vec![tcp(tail), named.clone()]), + e.host.identity(), + ), + ) + .await + .unwrap(); + assert_eq!(reached.route, named); + assert_eq!(reached.class, RouteClass::Lan); + assert_eq!( + tail_taken.load(Ordering::SeqCst), + 0, + "the tailnet was dialled" + ); +} + /// `ws` candidates start TIER_HEAD_START_MS after `tcp`, with the address /// the caller classified, and the ws pipe carries the session. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] diff --git a/crates/mousse-net-io/src/caller.rs b/crates/mousse-net-io/src/caller.rs index 0405a0c4..8d436290 100644 --- a/crates/mousse-net-io/src/caller.rs +++ b/crates/mousse-net-io/src/caller.rs @@ -58,9 +58,11 @@ //! join as they arrive) still fit. LAN starts at once, `DIAL_PARALLEL` //! tries at a time per class, each `DIAL_TIMEOUT_MS`. Tailnet starts //! `LAN_HEAD_START_MS` later when a LAN candidate was known at the start, -//! or a name resolved to a LAN address before it started (at once -//! otherwise: a browse still running never holds it back). Classes after -//! LAN wait while names are being resolved. +//! a name was still being resolved (it may be LAN), or a name resolved +//! to a LAN address before it started (at once otherwise: a browse still +//! running never holds it back). A name still being resolved after that +//! head start no longer holds the tailnet back; public, tunnel and ssh +//! wait while names are being resolved. //! Public and tunnel candidates start `TIER_HEAD_START_MS` after //! tailnet (at once with no LAN or tailnet candidate), each `ws` try //! `WS_DIAL_TIMEOUT_MS`. `ssh` runs one try at a time, each @@ -1352,7 +1354,14 @@ where || later.is_some(); let mut at = [ start, - tailnet_at, + // A name being resolved may be LAN: it holds the tailnet back for + // the LAN's head start at most, or less once it is resolved and + // every LAN candidate is done. + if ranking.is_empty() { + tailnet_at + } else { + start + lan_head + }, if nearer { tailnet_at + head } else { start }, start + ms(io.dial_fast_tiers_ms()), ]; @@ -1380,8 +1389,9 @@ where } for t in 0..CLASSES { let cap = if t == 3 { 1 } else { parallel }; - // Names being resolved may be LAN: the others wait for them. - while now >= at[t] && running[t] < cap && (t == 0 || ranking.is_empty()) { + // Names being resolved may be LAN: public, tunnel and ssh + // wait for them; the tailnet only until its start above. + while now >= at[t] && running[t] < cap && (t <= 1 || ranking.is_empty()) { let Some((route, targets)) = queues[t].pop_front() else { break; }; diff --git a/docs/limits.md b/docs/limits.md index 74d10f62..d20c5e1f 100644 --- a/docs/limits.md +++ b/docs/limits.md @@ -81,7 +81,7 @@ spec's value: it can only be tightened. | `MOUSSE_NET_DIAL_TIMEOUT_MS` | 2000 | 100 to 60000 | Caller: time one candidate address has to connect and pass step 1 before it is given up (Phase 1a §6). | | `MOUSSE_NET_DIAL_MAX_CANDIDATES` | 16 | 1 to 64 | Caller: candidates dialled in one search, in class order: LAN, tailnet, internet and tunnel, then SSH (Phase 1b §1.4). A hostname counts with the class it resolves to. | | `MOUSSE_NET_DIAL_LAN_RESERVED` | 4 | 0 to 32 | Caller: of those, places only LAN candidates may take, so tailnet and internet hints or a browse result never crowd the LAN out. At most half the candidates. | -| `MOUSSE_NET_LAN_HEAD_START_MS` | 1000 | 0 to 10000 | Caller: wait after LAN candidates start before tailnet ones start, when a LAN candidate is known at the start (a stored or owner address, a fresh browse, a name that resolves to the LAN). At once when there is none or once every LAN candidate failed; a browse still running never holds the tailnet back. | +| `MOUSSE_NET_LAN_HEAD_START_MS` | 1000 | 0 to 10000 | Caller: wait after LAN candidates start before tailnet ones start, when a LAN candidate is known at the start (a stored or owner address, a fresh browse, a name that resolves to the LAN), or while a name is still being resolved (it may be LAN). At once when there is none or once every name is resolved and every LAN candidate failed; a browse still running never holds the tailnet back, and a name still resolving holds it back for this long at most. | | `MOUSSE_NET_TIER_HEAD_START_MS` | 500 | 0 to 10000 | Caller: wait after the tailnet class starts before internet and tunnel candidates start; at once when there is no LAN or tailnet candidate. | | `MOUSSE_NET_DIAL_FAST_TIERS_MS` | 12000 | 1000 to 60000 | Caller: latest start of SSH candidates, from the start of the search; they start earlier once every other class is done, so an SSH login never races a LAN path. The last working route only orders candidates within their class. | | `MOUSSE_NET_DIAL_SEARCH_MS` | 45000 | 5000 to 300000 | Caller: one whole search across every tier; past it the search ends unreachable. | diff --git a/docs/phase1b-design.md b/docs/phase1b-design.md index 6faa0c2f..8a4b765a 100644 --- a/docs/phase1b-design.md +++ b/docs/phase1b-design.md @@ -182,8 +182,9 @@ is an allowlist: an address in none of the first three rows is search, never from a remembered list (§1.5), and filtered through the approvals (§1.3). Each candidate gets the class of its address (`AddrPolicy::class_of`); a hostname gets the class of the best address - it resolves to, within the try's timeout, and the classes after LAN wait - while names resolve. The order is by class: LAN, tailnet, internet and + it resolves to, within the try's timeout. The tailnet waits for names + being resolved for `LAN_HEAD_START_MS` at most; the classes after it + wait while names resolve. The order is by class: LAN, tailnet, internet and tunnel, then SSH. Within a class the last working route comes first, then owner addresses, approved hints, then browse results. At most `DIAL_MAX_CANDIDATES` are dialled, and `DIAL_LAN_RESERVED` of those @@ -198,9 +199,11 @@ is an allowlist: an address in none of the first three rows is results are candidates from the start. 2. **Tailnet**, `LAN_HEAD_START_MS` after LAN when a LAN candidate was known at the start (stored, owner-entered, a fresh browse, or a name - that resolved to the LAN). With no known LAN candidate it starts at - once: a browse still running never holds the tailnet back. It starts - early once every LAN candidate failed. + that resolved to the LAN), or while a name is still being resolved + (it may be LAN). With no known LAN candidate and no name to resolve it + starts at once: a browse still running never holds the tailnet back. + It starts early once every name is resolved and every LAN candidate + failed. 3. **Internet and tunnel**, `TIER_HEAD_START_MS` after the tailnet (at once if there is no LAN or tailnet candidate), or earlier once every class before it is done. From 901b4eeb1f83c2f21e0fe13f3610d3ecae539bc5 Mon Sep 17 00:00:00 2001 From: TheAnimatrix Date: Tue, 1 Dec 2026 11:59:12 +0530 Subject: [PATCH 2/2] fix(caller): only numeric tailnet candidates start before every name is resolved A name resolved to the tailnet may also hold public addresses, so it waits for the other names like public, tunnel and ssh candidates. --- crates/mousse-helper/tests/app_routes.rs | 66 ++++++++++++++++++++++++ crates/mousse-net-io/src/caller.rs | 23 ++++++--- docs/limits.md | 2 +- docs/phase1b-design.md | 17 +++--- 4 files changed, 91 insertions(+), 17 deletions(-) diff --git a/crates/mousse-helper/tests/app_routes.rs b/crates/mousse-helper/tests/app_routes.rs index bd1a359a..4bdf1f29 100644 --- a/crates/mousse-helper/tests/app_routes.rs +++ b/crates/mousse-helper/tests/app_routes.rs @@ -207,6 +207,24 @@ impl Resolver for Late { } } +/// A resolver that answers each name after its own wait. +struct ByName(Vec<(&'static str, Duration, Vec)>); + +impl Resolver for ByName { + fn resolve<'a>(&'a self, name: &'a str, _port: u16) -> ResolveFuture<'a> { + let (after, answer) = self + .0 + .iter() + .find(|(n, _, _)| *n == name) + .map(|(_, after, answer)| (*after, answer.clone())) + .unwrap_or((Duration::ZERO, Vec::new())); + Box::pin(async move { + tokio::time::sleep(after).await; + Ok(answer) + }) + } +} + async fn within(what: &str, limit: Duration, fut: impl std::future::Future) -> T { tokio::time::timeout(limit, fut) .await @@ -919,6 +937,54 @@ async fn a_name_resolved_to_the_lan_within_the_head_start_beats_the_tailnet() { ); } +/// Only numeric tailnet candidates skip the wait for names being resolved. +/// A name resolved to the tailnet may also hold public addresses, so it +/// waits for every name: its public address is not dialled while an +/// unrelated name is still being resolved. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_name_resolved_to_the_tailnet_waits_for_the_other_names() { + let e = engine().await; + // A tailnet address that refuses, then a public one that answers. + let dead = { + let gone = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + gone.local_addr().unwrap() + }; + let (public, public_taken) = relay(e.addr).await; + let mut caller = granted( + &e, + Arc::new(ManualClock::new()), + &[("LAN_HEAD_START_MS", 500), ("DIAL_TIMEOUT_MS", 10_000)], + ); + caller.use_addr_policy( + AddrPolicy::debug() + .with_class_map(&format!("{}=tailnet,{}=public", dead.port(), public.port())) + .unwrap(), + ); + caller.use_resolver(Arc::new(ByName(vec![ + ("mixed.test", Duration::ZERO, vec![dead, public]), + ("slow.test", Duration::from_secs(4), Vec::new()), + ]))); + let mixed = route(&format!("tcp://mixed.test:{}", public.port())); + let slow = route("tcp://slow.test:31753"); + let t0 = Instant::now(); + let (_conn, reached) = within( + "the search", + Duration::from_secs(15), + caller.search(Search::of(vec![mixed.clone(), slow]), e.host.identity()), + ) + .await + .unwrap(); + let took = t0.elapsed(); + assert_eq!(reached.route, mixed); + assert_eq!(public_taken.load(Ordering::SeqCst), 1); + // The slow name resolves to nothing after 4 s; only then may the mixed + // name, and so its public address, be dialled. + assert!( + took >= Duration::from_millis(3_900), + "dialled before the other name was done: {took:?}" + ); +} + /// `ws` candidates start TIER_HEAD_START_MS after `tcp`, with the address /// the caller classified, and the ws pipe carries the session. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] diff --git a/crates/mousse-net-io/src/caller.rs b/crates/mousse-net-io/src/caller.rs index 8d436290..c7b7bfa3 100644 --- a/crates/mousse-net-io/src/caller.rs +++ b/crates/mousse-net-io/src/caller.rs @@ -61,8 +61,9 @@ //! a name was still being resolved (it may be LAN), or a name resolved //! to a LAN address before it started (at once otherwise: a browse still //! running never holds it back). A name still being resolved after that -//! head start no longer holds the tailnet back; public, tunnel and ssh -//! wait while names are being resolved. +//! head start no longer holds numeric tailnet candidates back; a name +//! resolved to the tailnet (it may also hold public addresses), public, +//! tunnel and ssh wait while names are being resolved. //! Public and tunnel candidates start `TIER_HEAD_START_MS` after //! tailnet (at once with no LAN or tailnet candidate), each `ws` try //! `WS_DIAL_TIMEOUT_MS`. `ssh` runs one try at a time, each @@ -1354,9 +1355,9 @@ where || later.is_some(); let mut at = [ start, - // A name being resolved may be LAN: it holds the tailnet back for - // the LAN's head start at most, or less once it is resolved and - // every LAN candidate is done. + // A name being resolved may be LAN: it holds numeric tailnet + // candidates back for the LAN's head start at most, or less once + // it is resolved and every LAN candidate is done. if ranking.is_empty() { tailnet_at } else { @@ -1389,9 +1390,15 @@ where } for t in 0..CLASSES { let cap = if t == 3 { 1 } else { parallel }; - // Names being resolved may be LAN: public, tunnel and ssh - // wait for them; the tailnet only until its start above. - while now >= at[t] && running[t] < cap && (t <= 1 || ranking.is_empty()) { + // Names being resolved may be LAN: the others wait for them, + // except numeric tailnet candidates after the start above. A + // name resolved to the tailnet may also hold public + // addresses, so it waits too. + while now >= at[t] && running[t] < cap { + let numeric = matches!(queues[t].front(), Some((_, None))); + if !(t == 0 || ranking.is_empty() || (t == 1 && numeric)) { + break; + } let Some((route, targets)) = queues[t].pop_front() else { break; }; diff --git a/docs/limits.md b/docs/limits.md index d20c5e1f..b8d9dea1 100644 --- a/docs/limits.md +++ b/docs/limits.md @@ -81,7 +81,7 @@ spec's value: it can only be tightened. | `MOUSSE_NET_DIAL_TIMEOUT_MS` | 2000 | 100 to 60000 | Caller: time one candidate address has to connect and pass step 1 before it is given up (Phase 1a §6). | | `MOUSSE_NET_DIAL_MAX_CANDIDATES` | 16 | 1 to 64 | Caller: candidates dialled in one search, in class order: LAN, tailnet, internet and tunnel, then SSH (Phase 1b §1.4). A hostname counts with the class it resolves to. | | `MOUSSE_NET_DIAL_LAN_RESERVED` | 4 | 0 to 32 | Caller: of those, places only LAN candidates may take, so tailnet and internet hints or a browse result never crowd the LAN out. At most half the candidates. | -| `MOUSSE_NET_LAN_HEAD_START_MS` | 1000 | 0 to 10000 | Caller: wait after LAN candidates start before tailnet ones start, when a LAN candidate is known at the start (a stored or owner address, a fresh browse, a name that resolves to the LAN), or while a name is still being resolved (it may be LAN). At once when there is none or once every name is resolved and every LAN candidate failed; a browse still running never holds the tailnet back, and a name still resolving holds it back for this long at most. | +| `MOUSSE_NET_LAN_HEAD_START_MS` | 1000 | 0 to 10000 | Caller: wait after LAN candidates start before tailnet ones start, when a LAN candidate is known at the start (a stored or owner address, a fresh browse, a name that resolves to the LAN), or while a name is still being resolved (it may be LAN). At once when there is none or once every name is resolved and every LAN candidate failed; a browse still running never holds the tailnet back, and a name still resolving holds numeric tailnet candidates back for this long at most (a name resolved to the tailnet waits for every name). | | `MOUSSE_NET_TIER_HEAD_START_MS` | 500 | 0 to 10000 | Caller: wait after the tailnet class starts before internet and tunnel candidates start; at once when there is no LAN or tailnet candidate. | | `MOUSSE_NET_DIAL_FAST_TIERS_MS` | 12000 | 1000 to 60000 | Caller: latest start of SSH candidates, from the start of the search; they start earlier once every other class is done, so an SSH login never races a LAN path. The last working route only orders candidates within their class. | | `MOUSSE_NET_DIAL_SEARCH_MS` | 45000 | 5000 to 300000 | Caller: one whole search across every tier; past it the search ends unreachable. | diff --git a/docs/phase1b-design.md b/docs/phase1b-design.md index 8a4b765a..86c6ee6b 100644 --- a/docs/phase1b-design.md +++ b/docs/phase1b-design.md @@ -182,14 +182,15 @@ is an allowlist: an address in none of the first three rows is search, never from a remembered list (§1.5), and filtered through the approvals (§1.3). Each candidate gets the class of its address (`AddrPolicy::class_of`); a hostname gets the class of the best address - it resolves to, within the try's timeout. The tailnet waits for names - being resolved for `LAN_HEAD_START_MS` at most; the classes after it - wait while names resolve. The order is by class: LAN, tailnet, internet and - tunnel, then SSH. Within a class the last working route comes first, - then owner addresses, approved hints, then browse results. At most - `DIAL_MAX_CANDIDATES` are dialled, and `DIAL_LAN_RESERVED` of those - places (at most half) are kept for LAN candidates, browse results - included. + it resolves to, within the try's timeout. Numeric tailnet candidates + wait for names being resolved for `LAN_HEAD_START_MS` at most; a name + resolved to the tailnet (it may also hold public addresses) and the + classes after the tailnet wait while names resolve. The order is by + class: LAN, tailnet, internet and tunnel, then SSH. Within a class the + last working route comes first, then owner addresses, approved hints, + then browse results. At most `DIAL_MAX_CANDIDATES` are dialled, and + `DIAL_LAN_RESERVED` of those places (at most half) are kept for LAN + candidates, browse results included. - **Concurrency.** Each class keeps 1a's race, `DIAL_PARALLEL` tries at once. A `tcp` try gets `DIAL_TIMEOUT_MS`, a `ws`/`wss` one `WS_DIAL_TIMEOUT_MS`, an `ssh` one `SSH_DIAL_TIMEOUT_MS`.