Repository navigation
Description
Description
Both TestGracefulShutdown and TestRegistrySupportedCipherSuite in registry/registry_test.go call setupRegistry(...) with hardcoded ports (:5000 and :5001 respectively) instead of ephemeral ports. setupRegistry never checks whether ListenAndServe/ListenAndServeTLS actually succeeded in binding to that port before the test proceeds — it just fires off the listener in a goroutine and then does a fixed time.Sleep(3 * time.Second) ("Wait for some unknown random time for server to start listening").
If anything else on the host is already bound to :5000/:5001 — ListenAndServe fails silently in the background goroutine (the error is dropped/logged, not surfaced to the test), and the test's subsequent net.Dial/TLS handshake ends up talking to the pre-existing server instead of the one the test thinks it started. This produces failures that look like timing/readiness races but are actually a bind collision:
TestGracefulShutdowndials successfully after "stopping" the registry — because it's dialing someone else's server, which was never stopped.TestRegistrySupportedCipherSuitegetstls: first record does not look like a TLS handshake— because the "registry" it's talking to isn't running the TLS config the test just configured.
Confirmed root cause
Reproduced by:
- Starting a local dev registry (
cmd/registrywithconfig-dev.yml, which listens on:5000/:5001). - Running
go test -run 'TestGracefulShutdown|TestRegistrySupportedCipherSuite' -v ./registry/— both tests fail consistently. - Stopping the local dev registry and re-running the exact same test command — both tests pass consistently.
This isolates the trigger to the port collision, not general CPU contention or sandbox timing variance (though the fixed-sleep design is still a real, independent fragility — see below).
Reproduce
# terminal 1
go run ./cmd/registry serve ./cmd/registry/config-dev.yml
# terminal 2
go test -run 'TestGracefulShutdown|TestRegistrySupportedCipherSuite' -v ./registry/
Stop terminal 1 and re-run the go test command — it passes.
Notes
setupRegistry already carries a TODO acknowledging this exact class of problem:
func setupRegistry(tlsCfg *registryTLSConfig, addr string) (*Registry, error) {
config := &configuration.Configuration{}
// TODO: this needs to change to something ephemeral as the test will fail if there is any server
// already listening on port 5000
...
Separately, even without a port collision, the tests rely on fixed sleeps rather than an actual readiness signal:
time.Sleep(3 * time.Second)to assume the listener has bound and is accepting.time.Sleep(100 * time.Millisecond)after triggering shutdown, then a single dial attempt to assert the connection is now rejected — but OS-level accept-backlog behavior can let one connection through even afterClose()has been called on the listener, so this is a secondary source of flakiness independent of the port collision above.
Suggested fix
- Use ephemeral ports (
:0) insetupRegistryand read back the actual bound address (e.g., vianet.Listener.Addr()) instead of hardcoding:5000/:5001. This eliminates the port-collision failure mode entirely. - Surface/check the error from
ListenAndServe/ListenAndServeTLS(e.g., via a buffered error channel) so a genuine bind failure fails the test immediately with a clear message instead of silently falling through to sleep-based assertions against an unrelated server.
Environment
- Go version: go1.25.0
- registry version: v3.1.1
Activity
[-]Flaky tests: TestGracefulShutdown and TestRegistrySupportedCipherSuite rely on fixed sleeps instead of readiness signaling[/-][+]TestGracefulShutdown and TestRegistrySupportedCipherSuite silently connect to whatever is already on :5000/:5001 instead of failing when the port is taken[/+]milosgajdos commentedon Aug 5, 2026
Thanks for the issue, but going forward, please stop slopping this in and open a human readable issue which has reasonably long issue title instead of 3 lines your claude barfed.
test: use ephemeral ports for registry listen tests