From bf95d9da3708d8956d3b801c2840b921465f7822 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Wed, 23 Sep 2026 17:36:42 +0800 Subject: [PATCH] fix(l1): stop racing other tests for a "dead" port MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three tests bind a random port, drop the listener, and connect to the address expecting TCP to refuse. Dropping the listener frees the port, and this file binds a random port in seven places — several of which then run a real SOCKS5 server. When the OS hands the freed port to one of them, the dead proxy answers, and the test sees a handshake instead of a refusal. That is what CI hit on Windows: left: Some(Stage { step: Handshake, peer: Proxy }) right: Some(Stage { step: Tcp, peer: Proxy }) error: "The proxy asks for authentication method 0x01" Something on that port was speaking SOCKS5 — on a port the test had just released. `dead_port` keeps the same trick but checks it: connect once, and if anything answers, take another port. The window is not zero, but it is now bounded by one connect rather than by however long the test runs. Co-Authored-By: Claude Opus 5 --- crates/tw-gateway/src/l1.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/crates/tw-gateway/src/l1.rs b/crates/tw-gateway/src/l1.rs index 81f15923..2dc7db7c 100644 --- a/crates/tw-gateway/src/l1.rs +++ b/crates/tw-gateway/src/l1.rs @@ -896,6 +896,26 @@ mod tests { }) } + /// 一个确实没人在听的本机端口。 + /// + /// **「绑一个随机端口再 drop」本身是有竞态的**:端口一释放,同一批 + /// 并行测试里别的用例就可能绑到它 —— 这个文件里有七处在绑随机端口, + /// 其中几处还起了真的 SOCKS5 服务器。于是「死端口」活了过来, + /// 测试看到的不是 TCP 拒绝而是一次握手。Windows 上撞得尤其勤。 + /// + /// 所以拿到之后先自己连一次确认它是死的,活着就换一个。 + async fn dead_port() -> SocketAddr { + for _ in 0..64 { + let l = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let a = l.local_addr().unwrap(); + drop(l); + if tokio::net::TcpStream::connect(a).await.is_err() { + return a; + } + } + panic!("连着 64 次都没拿到一个没人监听的端口"); + } + /// 起一个只接受连接、什么都不说的 TCP 端口。 async fn dead_ear() -> SocketAddr { let l = TcpListener::bind("127.0.0.1:0").await.unwrap(); @@ -933,9 +953,7 @@ mod tests { #[tokio::test] async fn a_refused_port_fails_at_tcp_and_the_error_names_that_phase() { // 一句「连接失败」说不出该修什么。 - let l = TcpListener::bind("127.0.0.1:0").await.unwrap(); - let a = l.local_addr().unwrap(); - drop(l); + let a = dead_port().await; let r = l1(&format!("http://{a}"), None).await; assert!(!r.ok); assert_eq!( @@ -1270,9 +1288,7 @@ mod tests { async fn a_refused_proxy_port_says_nothing_is_listening_not_just_an_errno() { // 「Connection refused (os error 61)」和「那个端口上没有东西在听」 // 之间隔着一次搜索。 - let l = TcpListener::bind("127.0.0.1:0").await.unwrap(); - let a = l.local_addr().unwrap(); - drop(l); + let a = dead_port().await; let p = hop(tw_config::ProxyKind::Socks5h, a, None); let r = l1_proxy(&p, "api.anthropic.com", 443).await; assert!(!r.ok); @@ -1322,9 +1338,7 @@ mod tests { #[tokio::test] async fn a_dead_proxy_fails_at_the_proxy_hop_not_at_the_upstream() { // 说成「连不上上游」会让人去查上游 —— 而上游根本没被碰过。 - let l = TcpListener::bind("127.0.0.1:0").await.unwrap(); - let a = l.local_addr().unwrap(); - drop(l); + let a = dead_port().await; let p = hop(tw_config::ProxyKind::Socks5h, a, None); let r = l1("https://api.anthropic.com", Some(&p)).await; assert!(!r.ok);