From e53cd3f6c7b41ffe80ff849174df93e10c91dfd2 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Wed, 18 Nov 2026 03:26:41 -0500 Subject: [PATCH] fix(net): a lookup waits up to 60 s for the DHCP lease being got back after one was lost and then uses the name's cached address, and each failed lookup is logged with its cached address's family, age and usability, where a lease lost at its renewal failed the lookup at once and ended an online install 25 GB in --- .../stela-runtime/src/platform/net/stack.rs | 61 +++++++++++++++++-- .../src/platform/net/stack/tests.rs | 52 ++++++++++++++++ 2 files changed, 109 insertions(+), 4 deletions(-) diff --git a/crates/stela-runtime/src/platform/net/stack.rs b/crates/stela-runtime/src/platform/net/stack.rs index 8cce96e41e..faf4de21fd 100644 --- a/crates/stela-runtime/src/platform/net/stack.rs +++ b/crates/stela-runtime/src/platform/net/stack.rs @@ -398,6 +398,17 @@ impl Stack { )) } + /// Whether DHCP is getting a new lease for one lost less than + /// [`DHCP_WAIT_US`] ago: IPv4 is expected back, so a lookup waits for + /// it rather than failing. + fn ipv4_pending(&self, now: u64) -> bool { + self.dhcp.is_some() + && self.config.is_none() + && self + .lost_at + .is_some_and(|at| now.saturating_sub(at) < DHCP_WAIT_US) + } + /// Gives the interface `address`/`prefix` and the default router /// `router` by hand instead of by DHCP. IPv6 autoconfiguration goes on. pub fn set_ipv4( @@ -628,7 +639,10 @@ impl Stack { /// An address found in the last [`RESOLVED_US`] is used again; IPv6 is /// asked first when it is ready, then IPv4; and a lookup that fails falls /// back to the last address the name had, so one lost reply does not end - /// a long download. + /// a long download. While DHCP gets back a lease lost less than + /// [`DHCP_WAIT_US`] ago, a failed lookup waits for it and asks again, + /// so a lease that lapses at its renewal does not end one either. Each + /// failure is noted with what the name's cached address was. fn resolve(&mut self, lookup: &mut Lookup) -> Result, NetError> { if let Ok(address) = lookup.name.parse() { return Ok(Some(address)); @@ -657,14 +671,50 @@ impl Stack { Err(error @ NetError::Clock(_)) => return Err(error), Err(error) => error, }; - match known { - Some(index) if self.address_usable(self.resolved[index].1) => { - Ok(Some(self.resolved[index].1)) + let cached = known.map(|index| (self.resolved[index].1, self.resolved[index].2)); + let usable = cached.is_some_and(|(address, _)| self.address_usable(address)); + let pending = self.ipv4_pending(now); + let line = alloc::format!( + "DNS {} failed: {failure}; cached {}, usable={usable} (ipv4 config present={}, \ + ipv6 global ready={}, lease lost {})", + lookup.name, + match cached { + Some((IpAddress::Ipv4(_), at)) => + alloc::format!("IPv4 address {} s old", now.wrapping_sub(at) / 1_000_000), + Some((IpAddress::Ipv6(_), at)) => + alloc::format!("IPv6 address {} s old", now.wrapping_sub(at) / 1_000_000), + None => String::from("none"), + }, + self.config.is_some(), + self.ipv6_global_ready(), + match (self.lost_at, pending) { + (Some(at), true) => alloc::format!( + "{} s ago, waiting for the next", + now.saturating_sub(at) / 1_000_000 + ), + (Some(at), false) => alloc::format!("{} s ago", now.saturating_sub(at) / 1_000_000), + (None, _) => String::from("never"), + }, + ); + self.note(line); + match cached { + Some((address, _)) if usable => Ok(Some(address)), + _ if pending => { + self.await_lease(lookup); + Ok(None) } _ => Err(failure), } } + /// Keep `lookup` waiting until the lease DHCP is getting back is due: + /// [`DHCP_WAIT_US`] after the last was lost. + fn await_lease(&self, lookup: &mut Lookup) { + if let Some(at) = self.lost_at { + lookup.deadline = lookup.deadline.max(at.saturating_add(DHCP_WAIT_US)); + } + } + fn lookup(&mut self, lookup: &mut Lookup, now: u64) -> Result, NetError> { let Some((query, kind)) = lookup.query else { // IPv6 first where it reaches the internet; from a unique local @@ -675,6 +725,9 @@ impl Stack { DnsQueryType::A } else if !lookup.ipv4_only && self.ipv6_ready() { DnsQueryType::Aaaa + } else if self.ipv4_pending(now) { + self.await_lease(lookup); + return Ok(None); } else if now >= lookup.deadline { return Err(NetError::Dns(alloc::format!( "{}: no reachable address family", diff --git a/crates/stela-runtime/src/platform/net/stack/tests.rs b/crates/stela-runtime/src/platform/net/stack/tests.rs index 1d1c6745a0..6886ab1fef 100644 --- a/crates/stela-runtime/src/platform/net/stack/tests.rs +++ b/crates/stela-runtime/src/platform/net/stack/tests.rs @@ -911,3 +911,55 @@ fn a_dhcp_lease_and_its_loss_are_noted() { stack.set_ipv4(CLIENT_IP, 24, None).unwrap(); assert_eq!(stack.dhcp_summary(), None); } + +/// LOG-3: a lease lost at its renewal no longer fails the lookup of a name +/// with a cached IPv4 address: it waits for the next lease and then uses +/// the cached address, where it failed at once and ended a 25 GB download. +/// A lease that does not come back within the DHCP wait still fails it. +#[test] +fn a_lookup_waits_for_a_lost_lease_and_then_uses_the_cached_address() { + let (mut stack, answering, clock) = dhcpd(true); + stack.dhcp().unwrap(); + let address = IpAddress::Ipv4(SERVER_IP); + let now = stack.now().unwrap(); + stack + .resolved + .push((String::from("cdn.example"), address, now)); + answering.set(false); + let dhcp = stack.dhcp.unwrap(); + stack.sockets.get_mut::(dhcp).reset(); + stack.poll().unwrap(); + assert!(stack.config().is_none()); + let mut lookup = Lookup { + name: String::from("cdn.example"), + port: 80, + buffer: 0, + raw: false, + query: None, + asked: 0, + ipv4_only: false, + deadline: 0, + }; + assert_eq!(stack.resolve(&mut lookup), Ok(None)); + assert!(lookup.deadline >= clock.0.get() + DHCP_WAIT_US - 1_000_000); + answering.set(true); + while stack.config().is_none() { + stack.poll().unwrap(); + } + assert_eq!(stack.resolve(&mut lookup), Ok(Some(address))); + // A lease that stays away past the wait: the lookup fails, and says + // what the cached address was. + answering.set(false); + stack.sockets.get_mut::(dhcp).reset(); + stack.poll().unwrap(); + clock.0.set(clock.0.get() + DHCP_WAIT_US); + stack.take_notes(); + assert!(matches!(stack.resolve(&mut lookup), Err(NetError::Dns(_)))); + let notes = stack.take_notes(); + assert!( + notes.iter().any(|line| line.starts_with("DNS cdn.example failed: ") + && line.contains("cached IPv4 address ") + && line.contains("usable=false (ipv4 config present=false, ipv6 global ready=false, lease lost 60 s ago)")), + "{notes:?}" + ); +}