Skip to content

TestGracefulShutdown and TestRegistrySupportedCipherSuite silently connect to whatever is already on :5000/:5001 instead of failing when the port is taken #4928

Closed

Description

@tushar19

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:

  • TestGracefulShutdown dials successfully after "stopping" the registry — because it's dialing someone else's server, which was never stopped.
  • TestRegistrySupportedCipherSuite gets tls: 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:

  1. Starting a local dev registry (cmd/registry with config-dev.yml, which listens on :5000/:5001).
  2. Running go test -run 'TestGracefulShutdown|TestRegistrySupportedCipherSuite' -v ./registry/ — both tests fail consistently.
  3. 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 after Close() 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) in setupRegistry and read back the actual bound address (e.g., via net.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

  1. changed the title [-]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[/+] on Aug 5, 2026
  2. milosgajdos commented on Aug 5, 2026

    @milosgajdos
    Member

    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.

  3. added a commit that references this issue on Aug 27, 2026
    21e0c31
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions