Skip to content

Lab DNS server fails to bind a UDP+TCP port pair on Windows #204

Closed

Description

@TMHSDigital

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

  1. On Windows with Hyper-V or WSL installed, build subenum.
  2. Run subenum -progress=false -type A,CNAME -simulate-zone examples/labs/lab1-first-scan.zone lab.example 40 times in a loop.
  3. 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/dnsserver covers the retry path (for example, by injecting a listen function that fails the first N TCP binds)

Getting started


Filed by repo-audit/fix-issues at e49aca4. Severity: medium. Effort: S. audit-key: bug|internal/dnsserver/server.go|Start port pair

Activity

  1. added
    bug
    good first issue
    testing
    severity: medium
    on Nov 9, 2026
  2. TMHSDigital commented on Nov 9, 2026

    @TMHSDigital
    OwnerAuthor

    Fixed in f6177c6.

    dnsserver.Listen now binds through bindPair: 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:

    • 200 consecutive -simulate-zone lab 1 starts: 0 failures (before: about 1 in 4).
    • New unit tests in internal/dnsserver/bindpair_test.go inject 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggood first issueseverity: mediumtesting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions