Skip to content

Stop racing other tests for a "dead" port - #133

Merged
fylorn merged 1 commit into
mainfrom
fix/l1-dead-port-race
Sep 23, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/l1-dead-port-race

Conversation

@fylorn

@fylorn fylorn commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Fixes a flaky test that surfaced on the Windows job in #132 — unrelated to that PR's contents, but it will keep hitting every PR until it is fixed.

The race

Three tests need an address where TCP connect gets refused. They bind a random port, drop the listener, and connect:

let l = TcpListener::bind("127.0.0.1:0").await.unwrap();
let a = l.local_addr().unwrap();
drop(l);            // ← the port is now free for anyone

l1.rs binds a random port in seven places, and several of those then run a real SOCKS5 server. When the OS hands the just-freed port to one of them, the "dead" proxy answers.

That is exactly what CI saw:

left:  Some(Stage { step: Handshake, peer: Proxy })
right: Some(Stage { step: Tcp, peer: Proxy })
error: "The proxy asks for authentication method 0x01"

Something was speaking SOCKS5 on a port the test had released a moment earlier. Windows hits it more often — its port allocation reuses freed ports sooner than Linux's rotation does.

The fix

dead_port() keeps the same trick but verifies it: connect once, and if anything answers, take another port. The window is not zero, but it is now bounded by a single connect rather than by the whole length of the test.

Ran the l1:: tests 20× locally — green every time.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
@fylorn
fylorn force-pushed the fix/l1-dead-port-race branch from 07355d8 to bf95d9d Compare September 23, 2026 09:48
@fylorn
fylorn merged commit 81385b0 into main Sep 23, 2026
2 checks passed
@fylorn
fylorn deleted the fix/l1-dead-port-race branch September 23, 2026 10:00
@fylorn fylorn mentioned this pull request Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant