Repository navigation
Description
Summary
On Windows, dnsserver fails to start about a quarter of the time with "no free UDP+TCP port pair". Windows hands out consecutive UDP ports, so all 20 attempts land in the same TCP-reserved port block and every TCP bind fails. -simulate-zone (the labs) then exits 1, and the lab end-to-end tests fail intermittently on Windows.
Where
internal/dnsserver/server.go:125-140(UDP port chosen by the OS, then TCP bound on the same port, 20 attempts)
Details
for attempt := 0; attempt < 20; attempt++ {
pc, err = lc.ListenPacket(ctx, "udp", net.JoinHostPort(host, "0"))
...
ln, err = lc.Listen(ctx, "tcp", pc.LocalAddr().String())
if err == nil { break }
_ = pc.Close()
}Running a lab scan 40 times on Windows 11 failed 10 times, each with ports that step by 20 (one run per attempt block):
Error: -simulate-zone: starting the local DNS server: no free UDP+TCP port pair: listen tcp 127.0.0.1:50121: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
Error: ... listen tcp 127.0.0.1:50141: bind: ...
Error: ... listen tcp 127.0.0.1:50161: bind: ...
The OS's ephemeral UDP ports are sequential, so retrying with port 0 walks through one reserved TCP range (Hyper-V/WSL reservations, see netsh int ipv4 show excludedportrange protocol=tcp) rather than escaping it.
Impact
Windows users of the labs (-simulate-zone) see random startup failures. TestE2ELabTakeoverHint, TestE2ESARIF and TestDoTRejectsUntrustedCertificate fail intermittently on Windows for this reason. This is a different cause from the timing flakes in #167.
Steps to reproduce
- On Windows with Hyper-V or WSL installed, build subenum.
- Run
subenum -progress=false -type A,CNAME -simulate-zone examples/labs/lab1-first-scan.zone lab.example40 times in a loop. - Observe several "no free UDP+TCP port pair" failures with consecutive ports.
Suggested fix
After a failed TCP bind, try an explicit random port in a high range (for example 20000-60000) for both UDP and TCP instead of asking the OS for another ephemeral UDP port. Alternatively, bind TCP first on a random port and fall back as now. Keep the attempt cap.
Acceptance criteria
- 200 consecutive lab starts on Windows succeed
- A unit test in
internal/dnsservercovers the retry path (for example, by injecting a listen function that fails the first N TCP binds)
Getting started
- Relevant files:
internal/dnsserver/server.go,internal/dnsserver/server_test.go - How to run tests:
go test ./internal/dnsserver/ ./ -run 'Lab|SARIF' - Related: Timing-sensitive tests fail under load (pool spread, lab 2 wildcard, SARIF) #167 (timing flakes, a separate cause), Run unit tests on Windows and macOS in CI; track coverage; make test lacks -race #170 (Windows CI)
Filed by repo-audit/fix-issues at e49aca4. Severity: medium. Effort: S. audit-key: bug|internal/dnsserver/server.go|Start port pair
Activity
TMHSDigital commentedon Nov 9, 2026
Fixed in f6177c6.
dnsserver.Listennow binds throughbindPair: the first attempt lets the OS pick the UDP port, and after a failed TCP bind it retries on random ports in 20000-59999 (up to 32 attempts) instead of asking for the next ephemeral port, which stayed inside the same reserved block.Verified on Windows 11:
-simulate-zonelab 1 starts: 0 failures (before: about 1 in 4).internal/dnsserver/bindpair_test.goinject listeners that fail the first TCP binds and check the retries use explicit ports, the give-up path, and the UDP-unavailable path.go test -race -count=40 -cpu 1,2 -run TestPoolBenchesDeadResolver ./internal/scan/previously failed 10+ times with this error; now 0. Full suite passes, including with-shuffle=on.Pick bind-pair test ports the way the server does (#254)