Skip to content

test(registrar): subscribe before Start in TestSyncer_Start_NoEventsAfterInitialSync (#1187) - #1188

Merged
bpalermo merged 2 commits into
mainfrom
fix/1187-syncer-noop-event
Oct 4, 2026
Merged

bpalermo merged 2 commits into
mainfrom
fix/1187-syncer-noop-event

Conversation

@bpalermo

@bpalermo bpalermo commented Oct 4, 2026

Copy link
Copy Markdown
Owner

Diagnosis: (B), a missing barrier in the test. The syncer is not publishing no-op versions.

I added temporary logging of each stray event's content and version, then reproduced on main @ 6394d25 with bazel test --config=race --runs_per_test=40 --nocache_test_results --test_filter=TestSyncer_Start_NoEventsAfterInitialSync //registrar/internal/server:server_test. It failed 5/40. Every failing run logged the same four events:

ENDPOINT_ADDED svc PROTOCOL_HTTP 10.0.5.1:8080 version:"1"
ENDPOINT_ADDED svc PROTOCOL_TCP  10.0.5.1:8080 version:"1"
ENDPOINT_ADDED svc PROTOCOL_UDP  10.0.5.1:8080 version:"1"
SERVICE_ADDED  svc               version:"1"
(final snapshot version 3)

These are the initial sync's events, and they arrived late. None of them came from a later cycle. The cause is in Syncer.sync: close(s.synced) runs before that same cycle's s.broadcaster.Broadcast(events) (this ordering dates from #142). So the test's requireSynced then Subscribe sequence can register the watcher inside that window, and the watcher then receives the first cycle's fan-out. #1142 and #1152 are not involved, because this test uses a plain mockRegistry: there is no ChangeNotifier and no write-behind queue. The window got easier to hit when #788 replaced the 50 ms sleep with the Synced() gate. The gate opens slightly before the broadcast.

Why the production path is fine

WatchEndpoints waits on awaitSynced, sends FullSnapshotEvents at the current version, and then calls Subscribe. If the first cycle's broadcast lands after that, the agent sees ENDPOINT_ADDED / SERVICE_ADDED for state it already received in the snapshot. RegistrarRegistry applies ENDPOINT_ADDED as upsertLocked, which is idempotent, so the result is a redundant change signal and no wrong state. The same snapshot-then-subscribe overlap exists for every later cycle too, because Broadcast runs outside the snapshot lock, and it is tolerated the same way. Unchanged syncs never broadcast: diffLocked produces no events, and sync only calls Broadcast when len(events) > 0.

Fix (test-only)

  • The watcher now subscribes before Start, so the initial sync's events are observed deterministically instead of being raced against.
  • The test requires exactly the initial set: SERVICE_ADDED plus one ENDPOINT_ADDED per synced protocol. It then asserts that every received event carries that first version, which means no later cycle emitted anything.
  • The barrier for the later cycles is Start returning (<-done). Broadcast is a synchronous enqueue on the sync goroutine, so once Start returns every event is already in the channel. Unsubscribe then closes the channel, and the test drains it with range. There are no sleeps.

I checked that the new test still catches the real bug it guards. I temporarily injected a mutation that makes a no-op sync emit ENDPOINT_UPDATED for unchanged endpoints, and the test failed 5/5, reporting the version-2 and version-3 events. The mutation was reverted.

Gates

  • bazel test --config=race --runs_per_test=50 --nocache_test_results --test_filter=TestSyncer_Start_NoEventsAfterInitialSync //registrar/internal/server:server_test: 50/50 pass (it was 5/40 failing before the fix).
  • bazel test --config=race on all 11 go_test targets under //registrar/... //registry/...: all pass. A bare //registrar/... pattern fails at analysis on //registrar/cmd/registrar:image_index under race because cgo is off, so I ran the test targets explicitly.
  • bazel build --config=lint --@aspect_rules_lint//lint:fail_on_violation //registrar/... //registry/...: clean.
  • make format-check: clean. make gazelle: no changes.

Fixes #1187

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

…fterInitialSync (#1187)

The flake was a missing barrier, not a no-op publish. Synced() closes
inside the first sync cycle BEFORE that cycle's Broadcast runs, so a
watcher that subscribes right after Synced() can still receive the
initial sync's version-1 events late. Every stray event in the failing
runs carried version "1" (the first cycle) and the same content the
snapshot already held; no later cycle ever emitted an event.

The test now subscribes before Start, requires exactly the initial
sync's events (SERVICE_ADDED + one ENDPOINT_ADDED per synced protocol),
and asserts every event carries that first version. Start returning is
the barrier for the later cycles: Broadcast is a synchronous enqueue on
the sync goroutine.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
@bpalermo
bpalermo merged commit 4a5727a into main Oct 4, 2026
24 checks passed
@bpalermo
bpalermo deleted the fix/1187-syncer-noop-event branch October 4, 2026 03:07
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.

registrar: TestSyncer_Start_NoEventsAfterInitialSync flakes under --config=race (2/20 on main) — a sync with unchanged state still emits an event

1 participant