Skip to content

test(registrar): make the watch-filter re-assert test ordering-immune (#772) - #793

Merged
bpalermo merged 2 commits into
mainfrom
fix/772-watch-filter-reassert-flake
Sep 16, 2026
Merged

bpalermo merged 2 commits into
mainfrom
fix/772-watch-filter-reassert-flake

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

Follow-up to #781: the fake server's request log is appended by concurrent stream handlers, so the assertion raced. Production loop verified correct.

fix(registrar): make the filter re-assert test deterministic (#772)

VERDICT: test artefact, not a production bug. The watch loop is correct;
the in-process fake's request log is not ordered the way the assertion
assumed.

TestWatchLoop_FilterReassertIsNeverLost asserted that the LAST request
recorded by fakeWatchServer carried the newest filter. But the fake
appends to s.requests from inside WatchEndpoints, and grpc-go
dispatches every server stream on its own goroutine
(watch_startup_test.go:51). When the loop cancels stream N and
immediately opens stream N+1 — which is exactly what each of the test's
4-deep bursts makes it do — the two handler goroutines race to append,
and N's request can land after N+1's. Once the burst settles, the fake
holds the live stream open forever and no further request arrives, so
lastRequest() reports the superseded stream's filter permanently and
the 10s require.Eventually can never converge.

Interleaving trace, captured by instrumenting the fake to dump its whole
request log on failure (7/7 observed failures, identical shape):

req[39] filter=[default/svc-33-3]
req[40] filter=[default/svc-34-3]   <- the wanted filter DID reach the server
req[41] filter=[default/svc-34-2]   <- an OLDER stream's handler appending late
active streams: 1

i.e. the loop opened a stream asserting svc-34-3 and was sitting on it,
doing precisely the right thing, while the test reported it "stuck on a
stream with a stale filter". In all 7 failures the wanted filter was
present exactly once in the request log and the final entry was an
earlier round-member (svc-N-2 or svc-N-1).

Production is correct and no window remains:

  • assertFilter (registrar.go:284-290) publishes this stream's
    canceller and reads the filter under ONE filterMu hold, so a
    concurrent SetServiceFilter either completes first (the loop reads
    the new filter) or completes after (it captures THIS stream's
    canceller at registrar.go:238 and cancels it at registrar.go:257).
    "Neither" is unrepresentable — that is fix(registrar,node): copy cached endpoints out of the lock, never lose a watch-filter re-assert, atomic Diff+Replace, optimistic-lock taint patches (#772) #781's fix, and it holds.
  • SetServiceFilter cancelling late (after the cache purge at
    registrar.go:245-255) is harmless: it cancels the exact stream it
    captured, and if the loop has already moved on, its next
    assertFilter necessarily read a filter at least as new, because
    the critical sections are totally ordered on filterMu.
  • Two rapid updates both capturing the same canceller (the test's
    burst) converge: whichever cancel lands first reconnects the loop,
    and the reconnect's assertFilter reads the newest filter; the
    second cancel is a no-op on a dead stream.
  • build(deps): bump google.golang.org/grpc to v1.83.0 (security) #612's reconnect semantics are untouched: a locally-cancelled stream
    is classified out of the failure path (registrar.go:843-847,
    registrar.go:585-590), so no backoff accumulates across the 40
    rounds and the cap-1 wake / CompareAndSwap paths are not involved.

Test changes only:

  • watch_startup_test.go: add sawFilter, the ordering-immune
    predicate — "some stream asserted exactly this filter". It loses no
    strength, because a genuinely lost re-assert leaves its filter in no
    request at all. Document the ordering hazard on lastRequest; its
    other caller asserts requestCount() == 1 so it is unaffected.
  • watch_filter_test.go: assert on sawFilter, and pass logs
    (a Stringer) instead of logs.String() — testify evaluates message
    args eagerly, so the pre-rendered string captured the log from
    BEFORE the 10s wait and never showed what actually happened. That
    alone cost most of the diagnosis time on the original CI failure.

Failure rates (race detector, taskset -c 0,1 to mimic a 2-core runner):

before: 2/30 on the shipped test; 7/40 on an instrumented copy
after: 0/60 on the test, 0/30 on the whole package

Also 30/30 green via
bazel test //registry/internal/registrar:registrar_test --config=race --runs_per_test=30 --nocache_test_results (whole package, unfiltered).

Verification: bazel build //... //test/e2e:e2e_test, bazel test //...
(101/101), bazel run //:format.check, bazel build --config=lint --@aspect_rules_lint//lint:fail_on_violation //..., bazel run //:gazelle (no churn), bazel mod tidy (idempotent),
scripts/go-deps-audit.sh (exit 0).

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

bpalermo and others added 2 commits September 15, 2026 22:43
VERDICT: test artefact, not a production bug. The watch loop is correct;
the in-process fake's request log is not ordered the way the assertion
assumed.

`TestWatchLoop_FilterReassertIsNeverLost` asserted that the LAST request
recorded by `fakeWatchServer` carried the newest filter. But the fake
appends to `s.requests` from inside `WatchEndpoints`, and grpc-go
dispatches every server stream on its own goroutine
(`watch_startup_test.go:51`). When the loop cancels stream N and
immediately opens stream N+1 — which is exactly what each of the test's
4-deep bursts makes it do — the two handler goroutines race to append,
and N's request can land after N+1's. Once the burst settles, the fake
holds the live stream open forever and no further request arrives, so
`lastRequest()` reports the superseded stream's filter permanently and
the 10s `require.Eventually` can never converge.

Interleaving trace, captured by instrumenting the fake to dump its whole
request log on failure (7/7 observed failures, identical shape):

    req[39] filter=[default/svc-33-3]
    req[40] filter=[default/svc-34-3]   <- the wanted filter DID reach the server
    req[41] filter=[default/svc-34-2]   <- an OLDER stream's handler appending late
    active streams: 1

i.e. the loop opened a stream asserting svc-34-3 and was sitting on it,
doing precisely the right thing, while the test reported it "stuck on a
stream with a stale filter". In all 7 failures the wanted filter was
present exactly once in the request log and the final entry was an
earlier round-member (svc-N-2 or svc-N-1).

Production is correct and no window remains:

  - `assertFilter` (registrar.go:284-290) publishes this stream's
    canceller and reads the filter under ONE `filterMu` hold, so a
    concurrent `SetServiceFilter` either completes first (the loop reads
    the new filter) or completes after (it captures THIS stream's
    canceller at registrar.go:238 and cancels it at registrar.go:257).
    "Neither" is unrepresentable — that is #781's fix, and it holds.
  - `SetServiceFilter` cancelling late (after the cache purge at
    registrar.go:245-255) is harmless: it cancels the exact stream it
    captured, and if the loop has already moved on, its next
    `assertFilter` necessarily read a filter at least as new, because
    the critical sections are totally ordered on `filterMu`.
  - Two rapid updates both capturing the same canceller (the test's
    burst) converge: whichever cancel lands first reconnects the loop,
    and the reconnect's `assertFilter` reads the newest filter; the
    second cancel is a no-op on a dead stream.
  - #612's reconnect semantics are untouched: a locally-cancelled stream
    is classified out of the failure path (registrar.go:843-847,
    registrar.go:585-590), so no backoff accumulates across the 40
    rounds and the cap-1 wake / CompareAndSwap paths are not involved.

Test changes only:

  - `watch_startup_test.go`: add `sawFilter`, the ordering-immune
    predicate — "some stream asserted exactly this filter". It loses no
    strength, because a genuinely lost re-assert leaves its filter in no
    request at all. Document the ordering hazard on `lastRequest`; its
    other caller asserts `requestCount() == 1` so it is unaffected.
  - `watch_filter_test.go`: assert on `sawFilter`, and pass `logs`
    (a Stringer) instead of `logs.String()` — testify evaluates message
    args eagerly, so the pre-rendered string captured the log from
    BEFORE the 10s wait and never showed what actually happened. That
    alone cost most of the diagnosis time on the original CI failure.

Failure rates (race detector, `taskset -c 0,1` to mimic a 2-core runner):

  before: 2/30 on the shipped test; 7/40 on an instrumented copy
  after:  0/60 on the test, 0/30 on the whole package

Also 30/30 green via
`bazel test //registry/internal/registrar:registrar_test --config=race
--runs_per_test=30 --nocache_test_results` (whole package, unfiltered).

Verification: `bazel build //... //test/e2e:e2e_test`, `bazel test //...`
(101/101), `bazel run //:format.check`, `bazel build --config=lint
--@aspect_rules_lint//lint:fail_on_violation //...`, `bazel run
//:gazelle` (no churn), `bazel mod tidy` (idempotent),
`scripts/go-deps-audit.sh` (exit 0).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
@bpalermo
bpalermo merged commit a607a04 into main Sep 16, 2026
20 checks passed
@bpalermo
bpalermo deleted the fix/772-watch-filter-reassert-flake branch September 16, 2026 01:52
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