Skip to content

Commit f795a15

Browse files
authored
test(network): avoid DNS socket bind race (#2996)
This changes the DNS resolver test fixture to retry until TCP and UDP can share an ephemeral port. It fixes an issue seen in CI where a port that was successfully assigned for one protocol failed for the other. Signed-off-by: Kris Hicks <khicks@nvidia.com>
1 parent 1e9ee47 commit f795a15

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

  • crates/openshell-supervisor-network/src/policy_dns

crates/openshell-supervisor-network/src/policy_dns/resolver.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,23 @@ mod tests {
315315
use openshell_core::net::set_tcp_nodelay_best_effort;
316316
use tokio::net::TcpListener;
317317

318+
async fn bind_dns_test_server() -> (UdpSocket, TcpListener, SocketAddr) {
319+
const MAX_BIND_ATTEMPTS: usize = 100;
320+
321+
for _ in 0..MAX_BIND_ATTEMPTS {
322+
// Port zero only guarantees availability for the protocol being bound.
323+
let tcp = TcpListener::bind("127.0.0.1:0").await.unwrap();
324+
let server = tcp.local_addr().unwrap();
325+
match UdpSocket::bind(server).await {
326+
Ok(udp) => return (udp, tcp, server),
327+
Err(error) if error.kind() == std::io::ErrorKind::AddrInUse => {}
328+
Err(error) => panic!("failed to bind DNS test UDP socket: {error}"),
329+
}
330+
}
331+
332+
panic!("failed to bind DNS test TCP and UDP sockets to the same port");
333+
}
334+
318335
#[test]
319336
fn resolver_address_deduplication_preserves_answer_order() {
320337
let mut addresses = vec![
@@ -383,9 +400,7 @@ mod tests {
383400

384401
#[tokio::test]
385402
async fn truncated_udp_retries_over_tcp_and_follows_cname() {
386-
let udp = UdpSocket::bind("127.0.0.1:0").await.unwrap();
387-
let server = udp.local_addr().unwrap();
388-
let tcp = TcpListener::bind(server).await.unwrap();
403+
let (udp, tcp, server) = bind_dns_test_server().await;
389404

390405
let udp_task = tokio::spawn(async move {
391406
let mut wire = [0_u8; MAX_DNS_MESSAGE_BYTES];

0 commit comments

Comments
 (0)