Skip to content

test: close the ephemeral-port window that failed a CI run - #392

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/freeaddr-ephemeral-race
Aug 8, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/freeaddr-ephemeral-race

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

testhttp.FreeAddr binds 127.0.0.1:0, records the address, then closes the listener — which returns the port to the ephemeral pool. Between that close and the caller's bind the kernel can hand the port to anything else asking for one, and on #391 it did:

--- FAIL: TestStartMetricsBindsTheEndpoint
    metrics_wiring_test.go:43: startMetrics: failed to start metrics collector:
      binding the metrics endpoint on 127.0.0.1:43573: listen tcp 127.0.0.1:43573:
      bind: address already in use

The competing binder is in the same package: miniredis.RunT(t) in cache_selection_test.go:38, which asks for an ephemeral port in parallel. Nothing in the failing test was wrong — the address was stale by the time it was used. (Probed first: 400 concurrent FreeAddr calls return 400 distinct addresses, so it is not FreeAddr racing itself; macOS's ephemeral range starts at 49152 and does not reproduce it, where the CI failure was on 43573, inside Linux's.)

The fix is to remove the window, not shrink it

internal/metrics/collector.go already records the address the listener actually got (boundAddr) and reports it via Addr(). So every caller that binds now configures 127.0.0.1:0 and reads back where the kernel put it. The kernel picks at the moment of the bind; there is no interval to lose.

What that costs, and why it costs nothing that matters

One assertion wanted the port in advance: Addr() == addr, the regression check for #211. It is now testhttp.SameHost, which compares hosts.

That is what #211 was always about. fmt.Sprintf(":%d", Port) could only produce a wildcard host — an unauthenticated /metrics and /debug/operations on every interface the machine routes — and a scrape of 127.0.0.1 passes against a wildcard bind, because a wildcard answers on loopback too. A wildcard reads back as 0.0.0.0 or [::], never as the configured loopback address, so the host comparison catches precisely the defect; the port half never participated. internal/metrics/wiring_test.go already carried the comment // fixed port: the assertion is about the host half.

Mutation-verified, not assumed

Mutation Result
bind ":"+port instead of the configured addr (#211's defect) both wiring tests fail: bound host ::; the configuration said 127.0.0.1
delete the Serve goroutine, keep boundAddr the scrape fails: nothing ever answered … connection refused

FreeAddr survives, for one caller

TestStartMetricsHonorsDisabled needs an address before Start and never binds it — the assertion is that nothing is listening. A competing ephemeral bind cannot make that pass, since passing it requires answering HTTP 200 on /metrics. FreeAddr's doc comment now says that, instead of describing the window as something nothing could eliminate.

Gates

  • go test -race -count=8 -parallel 16 ./internal/adapter/ ./internal/metrics/ — ok, with the miniredis tests running alongside
  • gofmt -l, go vet, golangci-lint run on all three packages — clean

Unblocks #391, which hit this flake on a docs-only branch.

testhttp.FreeAddr binds 127.0.0.1:0, records the address, then closes the listener — which
returns the port to the ephemeral pool. Between that close and the caller's bind the kernel is
free to hand the port to anything else asking for one, and in CI it did:
TestStartMetricsBindsTheEndpoint failed with `bind: address already in use` on 127.0.0.1:43573,
a port the miniredis in this package's own cache_selection_test.go had been given in the
interval. Nothing in the test was wrong. The address was stale by the time it was used.

FreeAddr's own doc comment admitted the window and called it something nothing could eliminate.
That is true of FreeAddr and not of the callers: internal/metrics.Collector already records the
address the listener actually got (collector.go boundAddr) and reports it through Addr, so a
caller can configure port 0 and read back where the kernel put it. The kernel picks at the
moment of the bind, so there is no interval to lose.

What that costs is knowing the port in advance, which one assertion needed: `Addr() == addr`,
the check for #211. So it is now testhttp.SameHost, comparing hosts. That is what #211 was
always about — `fmt.Sprintf(":%d", Port)` could only produce a wildcard *host*, publishing an
unauthenticated /metrics and /debug/operations on every interface the machine routes, and a
scrape of 127.0.0.1 passes against a wildcard bind because a wildcard answers on loopback too.
A wildcard reads back here as 0.0.0.0 or [::], never as the configured loopback address, so the
host comparison catches exactly the defect the port never participated in. internal/metrics's
own copy of this test already carried the comment "fixed port: the assertion is about the host
half".

Verified by mutation rather than assumed:

  - bind ":"+port instead of the configured addr → both wiring tests fail naming the wildcard
    ("bound host ::; the configuration said 127.0.0.1")
  - delete the Serve goroutine, keeping boundAddr → the scrape fails with connection refused

FreeAddr survives for the caller that genuinely needs an address before a server starts and
never binds it: TestStartMetricsHonorsDisabled asserts nothing is listening, and a competing
ephemeral bind cannot make that pass, because passing it would require answering 200 on
/metrics. Its doc comment now says that instead of describing the window as unavoidable.

go test -race -count=8 -parallel 16 ./internal/adapter/ ./internal/metrics/ — ok, with the
miniredis tests running alongside.
TestStartServesTheEndpointWhereConfigured reserved an ephemeral port, closed it, and handed the
address to Start — the pattern that failed a CI run on the metrics side. It has not fired here
yet; the reason is timing, not design. Fixing it the same way needed something that did not
exist: metrics.Collector records what its listener actually got and reports it through Addr,
and health.Checker recorded nothing, so a test could only know the address by choosing it in
advance.

So Checker.Addr joins it, set from ln.Addr() under the lock Start already holds. The test now
configures 127.0.0.1:0 and reads back, and asserts the host halves match via testhttp.SameHost
— which is what #211 turns on, since a wildcard bind reports 0.0.0.0 or [::] here while
answering a loopback scrape identically to a loopback bind.

Not only for the test: an operator who configures the health endpoint on port 0 previously had
one slog line at Info to find out where it landed. The accessor is the same shape metrics has
had since #211.

Mutation-verified both directions:

  - bind ":"+port instead of the configured address → fails naming the wildcard
    ("bound host ::; the configuration said 127.0.0.1"), and the routable-address scrape fails too
  - set boundAddr from c.config.HTTPAddr instead of the listener → fails dialing 127.0.0.1:0,
    so the accessor is confirmed to report the listener rather than the configuration

go test -race -count=6 -parallel 16 ./internal/health/ ./internal/adapter/ ./internal/metrics/
— ok. golangci-lint clean.
@scttfrdmn
scttfrdmn merged commit 6808041 into main Aug 8, 2026
35 checks passed
@scttfrdmn
scttfrdmn deleted the fix/freeaddr-ephemeral-race branch August 8, 2026 23:50
@scttfrdmn scttfrdmn mentioned this pull request Aug 9, 2026
scttfrdmn added a commit that referenced this pull request Aug 9, 2026
Bumps the version constant, promotes `## [Unreleased]` to `## [0.13.0] - 2026-08-09`, and moves the
supported-versions table on.

`cmd/objectfs/main.go`'s constant is the only authority for the version — `release.yml` seds it and
fails the release if the tag disagrees — so this is the one place it changes. The CHANGELOG's
Unreleased section was audited against all 18 commits in `v0.12.0..main` before promotion rather than
trusted: every user-facing change is present, and the two commits without an entry (#398, #392) are
test-infrastructure with nothing a user can observe.

The release lede states what the tag is actually about, which is that two subsystems that existed and
did nothing now do something. `cluster.enabled: true` started a gossip ring and stopped: no
ClusterManager, no coordinator on FileSystem, `AnnounceKey`/`QueryKeyOwnership` returning
ErrNotSupported from stubs. And `internal/awsrates` held live-API-verified prices with every path to a
user severed, percentiles were declared and never assigned over a histogram that was `ms % 100`, an
acceleration fallback was permanent and invisible, and compression ran zstd over BAM and then discarded
the result.

Two things the lede is careful about, because a release note that oversells is the failure mode this
project has a history of:

- **Peer data fetch is not in this release.** Metadata travels over gossip; the bytes come from S3. The
  reason is measured, not architectural taste: a sealed datagram carries 5802 bytes of payload, so a
  128 KiB read is 21x over and used to fail as a 30-second timeout on a different host (#399).
- **Cost accounting is not Cost Explorer.** The counts come from a middleware in the AWS SDK's
  Deserialize step because that layer and the wrapper layer disagree — a 5 GB PutObject is one wrapper
  call and 641 requests — and they are this mount's requests, per attempt, excluding 5xx.

SECURITY.md's 0.12.0 row says its `cluster.*` settings had no security posture rather than implying
they had a weak one: a 0.12.0 mount never started coordination at all, which is what #139 fixed, so
0.13.0 is the first release where those keys mean anything and it refuses to start a cluster without a
gossip secret.

Verified: go build, gofmt, go vet, golangci-lint (0 issues), `go test -race ./...` with no FAIL, panic
or data race, and the four CHANGELOG/version invariant tests in internal/config that police link
references, the Unreleased compare target, and consecutive-release compare links.
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