feat: lookup_host_with_cache(): Don't return empty address list (#7596)

All users of this function expect a nonempty list to be returned, otherwise they fail with hard to
understand errors like "No connection attempts were made", so it's better to fail early if DNS
resolution failed and the cache doesn't contain resolutions as well.
This commit is contained in:
iequidoo
2025-12-14 18:01:26 -03:00
committed by iequidoo
parent 7ba4a43253
commit e34fee72a0
2 changed files with 14 additions and 8 deletions

View File

@@ -40,7 +40,7 @@
//! used for successful connection timestamp of
//! retrieving them from in-memory cache is used.
use anyhow::{Context as _, Result};
use anyhow::{Context as _, Result, ensure};
use std::collections::HashMap;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::str::FromStr;
@@ -850,7 +850,7 @@ pub(crate) async fn lookup_host_with_cache(
}
};
if load_cache {
let addrs = if load_cache {
let mut cache = lookup_cache(context, hostname, port, alpn, now).await?;
if let Some(ips) = DNS_PRELOAD.get(hostname) {
for ip in ips {
@@ -861,10 +861,15 @@ pub(crate) async fn lookup_host_with_cache(
}
}
Ok(merge_with_cache(resolved_addrs, cache))
merge_with_cache(resolved_addrs, cache)
} else {
Ok(resolved_addrs)
}
resolved_addrs
};
ensure!(
!addrs.is_empty(),
"Could not find DNS resolutions for {hostname}:{port}. Check server hostname and your network"
);
Ok(addrs)
}
/// Merges results received from DNS with cached results.