Skip to content

fix(mecak8s): make Prometheus listener test deterministic - #2620

Merged
jbeda merged 1 commit into
mainfrom
fix/mecak8s-metrics-listener-flake
Oct 30, 2026
Merged

jbeda merged 1 commit into
mainfrom
fix/mecak8s-metrics-listener-flake

Conversation

@jbeda

@jbeda jbeda commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Bind the metrics listener directly on 127.0.0.1:0 and report its actual bound address through an instance-local serve hook. The test no longer reserves/closes a port and hopes it is still free when startup reaches metrics.
  • Replace repeated unbounded http.Get calls with one context-bound scrape after the listener has bound; report early serve exits and HTTP/body errors directly.
  • Cancel and join the server on every post-startup exit, including failed assertions, and check its return error.
  • Add an occupied-address integration regression requiring the listen metrics diagnostic and proving startup is not announced after a failed bind.

The successful test still builds the real mock/miniredis service, drives a run, executes the full server startup/shutdown implementation, and requires a real /metrics response containing a mecatl_ series. Production supplies no observer; listener ownership and behavior are unchanged. The shared free-port helper remains for other tests outside this PR's scope.

Fixes #2074.

Evidence and limits

The exact cause of the historical CI failure remains unproven. This removes the documented port-handoff race and unbounded polling hazards rather than increasing the readiness or shutdown timeouts. The occupied-address test deterministically exercises the startup failure the old handoff could permit; it does not reproduce the historical CI schedule.

Verification

  • go test -race ./cmd/mecak8s -run '^TestTelemetryMetricsAddr(ServesPrometheus|BindFailure)$' -count=50 -cpu=1,2,4 — passed, 150 executions per test / 300 total.
  • Mutation check: reporting port zero instead of the bound port failed the real scrape; deferred shutdown still completed.
  • Mutation check: swallowing metrics bind errors failed the occupied-address diagnostic assertion at its bounded context exit.
  • Both temporary mutations were reverted before final gates.
  • task lint — passed, zero issues across all modules.
  • task test — passed (final integrated change, all modules and standalone proofs).
  • task test:race — passed (all modules and standalone proofs; mecak8s package 8.425s).
  • go run ./cmd/mecademo — passed; tool call, permission ask/approval, tool result, and final result observed.
  • git diff main...HEAD --check — passed.

Review

Independent Spec, Standards, Test adequacy, Architecture, Library reuse, and Duplication reviews. The initial missing occupied-address regression was added and re-reviewed; no unresolved findings. A suggestion to remove the shared port helper was withdrawn after checking its other callers.

API compatibility

No remote-observable contract changes: no API major/feature changes, protobuf changes, public Go API changes, or stored-data changes. The only production-code addition is an unexported bound-listener observation seam; production uses a nil observer. Existing clients and deployments require no upgrades or configuration changes.

Bind the metrics listener on an OS-selected port and observe its actual address through a per-invocation serve hook. Replace free-port handoff and unbounded polling with a context-bound scrape, early startup error reporting, and cancellation plus join on every assertion path.

Add an occupied-address regression that requires the bind diagnostic and forbids a startup callback on failure.

Co-authored-by: Mecatl <[email protected]>
@jbeda
jbeda merged commit 6dd10fa into main Oct 30, 2026
42 checks passed
@jbeda
jbeda deleted the fix/mecak8s-metrics-listener-flake branch October 30, 2026 00:52
@jbeda jbeda mentioned this pull request Oct 31, 2026
10 of 14 tasks
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Nov 18, 2026
2 tasks
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.

Flaky mecak8s Prometheus metrics listener test under race suite

1 participant