Skip to content

Stop freePort handing out the same port twice - #21

Merged
sahilkalgutkar merged 1 commit into
mainfrom
fix/port-race-in-app-tests
Sep 9, 2026
Merged

sahilkalgutkar merged 1 commit into
mainfrom
fix/port-race-in-app-tests

Conversation

@sahilkalgutkar

Copy link
Copy Markdown
Owner

The defect

freePort asks the kernel for port 0, records the address and closes the listener before Run binds it. The kernel will not offer a port that is still open, but it does reuse one released a moment ago — so two calls can name the same port, and the second bind fails with address already in use.

Why now

raftlite had the identical helper and it took main red twice this week. There the collision was easy to hit because startCluster asked for six ports in a row before binding any of them.

This repo is less exposed: three tests call freePort, one port each, and nothing in the file calls t.Parallel(). It has never actually failed — the last 41 CI runs are all green. This is closing a latent hole, not chasing a live break.

The fix

freePort records every address it issues and skips one already handed out, so no two callers can be pointed at the same port.

The window between releasing a port and Run binding it remains, exactly as the existing comment describes: Run owns its own listener and cannot be handed one already bound. That is the rarer external race, not the suite colliding with itself, and closing it would mean changing Run's signature.

Verification

go vet ./... and go build ./... pass locally. The suite needs a real Postgres and Docker is unavailable on this machine, so CI is the check that matters here.

freePort asks the kernel for port 0, records the address and closes the
listener before Run binds it. The kernel will not offer a port that is still
open, but it does reuse one released a moment ago, so two calls could name the
same port and the second bind would fail with "address already in use".

It now records every address it issues and skips one already handed out, so
no two callers can be pointed at the same port. The window between releasing a
port and Run binding it stays, as the comment already noted -- Run owns its
listener and cannot be handed one already bound -- but that is the rarer
external case, not the suite colliding with itself.

Same defect and same fix as raftlite's cli and server tests, where the
six-ports-in-a-row case failed CI outright. This one has never fired: three
tests call freePort, one port each, and none run in parallel.
@codecov

codecov Bot commented Sep 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@sahilkalgutkar
sahilkalgutkar merged commit 2bd7b71 into main Sep 9, 2026
3 checks passed
@sahilkalgutkar
sahilkalgutkar deleted the fix/port-race-in-app-tests branch September 9, 2026 03:20
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