From 1eac4d4b77c484ba7bf8b0494da4e8cd6cef98a3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 11 Oct 2026 15:14:31 -0700 Subject: [PATCH 1/2] fix(tokio): make the dead-address test fixture fail fast on macOS A SYN to a socket that is bound but not listening is refused on Linux but silently dropped on macOS, so each dial ran to its attempt bound or the 30s connect timeout. Listen and hang up on every connection instead. Co-Authored-By: Claude Opus 5.5 --- rs/moq-tokio/src/connection.rs | 36 ++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/rs/moq-tokio/src/connection.rs b/rs/moq-tokio/src/connection.rs index eb6b01c2514..dce7c890da8 100644 --- a/rs/moq-tokio/src/connection.rs +++ b/rs/moq-tokio/src/connection.rs @@ -2134,22 +2134,24 @@ mod tests { value.parse().expect("valid url") } - /// A loopback TCP port with nothing listening on it, which refuses instantly. + /// A loopback TCP port that hangs up on every connection, so dialing it fails fast. /// - /// A dead address that *refuses* rather than black-holes is what keeps these + /// A dead address that fails fast rather than black-holes is what keeps these /// tests fast and deterministic: the walk itself is what's under test, and /// bounding a black-holed attempt is covered by /// [`only_a_candidate_with_a_fallback_is_bounded`] as a pure function. /// - /// The returned socket is bound but never listens, so the port refuses while - /// staying reserved: no other listener (or an ephemeral self-connect) can take - /// it mid-test. Hold it for as long as the URL is dialed. + /// It listens rather than leaving the port merely bound: only Linux refuses a + /// SYN to a socket that is bound but not listening, while macOS drops it and + /// each attempt waits out its timeout. Listening also keeps the port reserved, + /// so no other listener (or an ephemeral self-connect) can take it mid-test. #[cfg(feature = "tcp")] - fn refused() -> (tokio::net::TcpSocket, Url) { - let socket = tokio::net::TcpSocket::new_v4().expect("tcp socket"); - socket.bind("127.0.0.1:0".parse().unwrap()).expect("bind"); - let addr = socket.local_addr().expect("local addr"); - (socket, url(&format!("tcp://{addr}/"))) + async fn dead() -> Url { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.expect("bind"); + let addr = listener.local_addr().expect("local addr"); + // Dropping each stream unread hangs up before the qmux handshake completes. + tokio::spawn(async move { while listener.accept().await.is_ok() {} }); + url(&format!("tcp://{addr}/")) } /// A bound stream listener and the URL that reaches it. @@ -2196,8 +2198,8 @@ mod tests { let (mut server, live) = live().await; tokio::spawn(async move { while server.accept().await.is_some() {} }); - let (_a, dead_a) = refused(); - let (_b, dead_b) = refused(); + let dead_a = dead().await; + let dead_b = dead().await; let addrs = Addrs::collect([dead_a, dead_b, live.clone()]).expect("not empty"); let shared = shared(); let mut draining = None; @@ -2218,8 +2220,8 @@ mod tests { async fn dial_any_reports_failure_when_nothing_answers() { let client = client(); - let (_a, dead_a) = refused(); - let (_b, dead_b) = refused(); + let dead_a = dead().await; + let dead_b = dead().await; let addrs = Addrs::collect([dead_a, dead_b]).expect("not empty"); let shared = shared(); let mut draining = None; @@ -2230,7 +2232,7 @@ mod tests { ) .await .expect("the walk must not hang"); - assert!(result.is_err(), "every address refused, so the walk must fail"); + assert!(result.is_err(), "every address hung up, so the walk must fail"); } /// Dialing must not write the LAN mesh's membership credential to the log. @@ -2247,7 +2249,7 @@ mod tests { const SECRET: &str = "b91d7fe20c4a"; let client = client(); - let (_dead, mut target) = refused(); + let mut target = dead().await; target.set_path(&format!("/.cluster/{SECRET}")); let addrs = Addrs::new(target); @@ -2255,7 +2257,7 @@ mod tests { let mut draining = None; // `Session` isn't `Debug`, so unwrap the failure by hand. let Err(err) = Connection::dial_any(&shared, &client, &addrs, &mut draining, None).await else { - panic!("a refused address must fail"); + panic!("a dead address must fail"); }; // The log line did happen, so the assertion below isn't vacuously true. From d854d8d2aa1cc5a5eafc3a1e074e61018998dc71 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 12 Oct 2026 18:18:21 -0700 Subject: [PATCH 2/2] test(tokio): give the refused-dial test a port that refuses on every OS #5237 added a_refused_tcp_dial_keeps_retrying on the old refused() fixture, which this branch removes. Refusal is what that test checks, so it binds an ephemeral port and releases it rather than using dead(). Co-Authored-By: Claude Opus 5.5 --- rs/moq-tokio/src/connection.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rs/moq-tokio/src/connection.rs b/rs/moq-tokio/src/connection.rs index dce7c890da8..914804f2d3b 100644 --- a/rs/moq-tokio/src/connection.rs +++ b/rs/moq-tokio/src/connection.rs @@ -2312,7 +2312,12 @@ mod tests { #[cfg(feature = "tcp")] #[tokio::test(start_paused = true)] async fn a_refused_tcp_dial_keeps_retrying() { - let (_socket, target) = refused(); + // Refusal is what's under test, so this can't use [`dead`]. A port nothing is + // bound to refuses on every OS, unlike one that is bound but not listening. + let target = { + let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind"); + url(&format!("tcp://{}/", listener.local_addr().expect("local addr"))) + }; let mut client = client(); client.backoff.initial = Duration::from_millis(50); client.backoff.max = Duration::from_millis(50);