Repository navigation
Stop freePort handing out the same port twice - #21
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The defect
freePortasks the kernel for port 0, records the address and closes the listener beforeRunbinds 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 withaddress 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
startClusterasked 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 callst.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
freePortrecords 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
Runbinding it remains, exactly as the existing comment describes:Runowns 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 changingRun's signature.Verification
go vet ./...andgo build ./...pass locally. The suite needs a real Postgres and Docker is unavailable on this machine, so CI is the check that matters here.