Skip to content

fix(agent): the initial snapshot waits for endpoints, not for a latch (#740) - #746

Merged
bpalermo merged 1 commit into
mainfrom
fix/740-pr5-initial-snapshot-waits-registry
Sep 7, 2026
Merged

bpalermo merged 1 commit into
mainfrom
fix/740-pr5-initial-snapshot-waits-registry

Conversation

@bpalermo

@bpalermo bpalermo commented Sep 7, 2026

Copy link
Copy Markdown
Owner

PR 5 of #740. A race found on the rev211 deploy roll: with SPIRE healthy and both registrar replicas Ready since 20:46:45Z, main-worker-02's agent produced a 316-request http_error burst on that node's prober (~30s at 10 rq/s).

The timeline (all 2026-09-07, within 3 seconds)

20:47:26.342 INFO  registrar client using SPIRE mTLS / initialized registrar registry
20:47:26.413 INFO  restored 9 observed upstreams from local storage
20:47:27.124..27.423 DEBUG setting snapshot (×~12)            ← before identity
20:47:27.338 WARN  cluster refresh deferred: the registrar has no identity yet …   ← MISCLASSIFIED: it was the AGENT's own identity that was pending
20:47:27.339 INFO  watch stream deferred until this agent has an SVID              ← correct
20:47:27.827 INFO  identity acquired; generating the initial snapshot
20:47:27.828 DEBUG generating initial snapshot / found pods in local storage
20:47:27.911 INFO  identity acquired; reconnecting the registrar client immediately …
20:47:27.912 INFO  registrar has no identity yet; retrying                         ← MISCLASSIFIED again (the registrar had identity; our conn was still in its handshake-failed state)
20:47:28.212 DEBUG setting snapshot                                                ← the initial snapshot: local pods, NO registry endpoints
20:47:28.311 DEBUG generating clusters and endpoints from registry
20:47:28.312 WARN  cluster refresh deferred … (failed to list endpoints)
20:47:29.017 DEBUG watch stream connected / re-asserting this node's registrations

Once identity is acquired, PreListen generated and published the initial snapshot immediately, but the registrar client's connection was still recovering from its pre-identity handshake failures (ResetConnectBackoff at 27.911; the watch connected at 29.017). The registry list failed, the first snapshot Envoy received had no cross-node endpoints, and the next refresh (~30s) repaired it.

What registryReady meant

PreListen type-asserted registry.ReadyWaiter (registry/registry.go:52-59) and called WaitReady (registry/internal/registrar/registrar.go:207-214), which blocks on a one-shot latch closed by readyOnce at registrar.go:799 on the first SNAPSHOT_COMPLETE.

So it meant "a complete snapshot arrived at some point" — not "the registry will serve this read now":

  • it is a latch, not a live state: once closed it never re-opens, so mid-life it is a no-op — including immediately after AssertWatchFilter (server.go:150) has purged cache entries leaving the filter and forced a re-filtered resnapshot;
  • the read that follows takes a different path — ListAllEndpoints falls through to the RPC fallback (registrar.go:377-397) whenever the cache is not serving — and that path fails on a ClientConn still in its handshake-failed state;
  • on expiry the branch only logs at INFO and proceeds, and nothing downstream checks that the resulting snapshot actually contains registry endpoints. The failure was handled by the local-only fallback, not by more waiting.

The fix

  1. The initial snapshot waits for endpoints, not for a latch. loadInitialRegistryConfig keeps WaitReady and then retries LoadClustersFromRegistry inside the same 15s budget the ready-wait already had, ending only on a successful load. New INFO registry connected; generating the initial snapshot with waited=. A genuinely unreachable registrar costs exactly what it cost before — the budget, then the unchanged registry unavailable for initial snapshot; starting with local-only config and the background retry. Failed attempts publish nothing (LoadClustersFromRegistry returns before touching the snapshot), so Envoy never sees an endpoint-less generation on the way to the good one. Registries with no watch (synchronous backends) keep the single attempt exactly. Nothing else in PreListen moves — agent: first registrar watch-stream attempt logs ERROR 'context canceled' on a clean startup, then retries fine #700/fix(agent): stop logging the startup watch-stream handoff as an ERROR (#700) #712's AssertWatchFilter ordering stands.

  2. Classification order. The two x509svid: could not get X509 bundle cases are textually identical, and the reason is structural: that string is raised by go-spiffe's own verifier against the local bundle source (svid/x509svid/verify.go:59-61), so it is produced whichever party was short — and after a pre-identity attempt the gRPC ClientConn hands it back from cache, so it is not even current. (A peer that cannot present a certificate at all arrives as a remote error: tls: … alert instead.) Text therefore cannot name the party; local state can. New spire.ClassifyHandshake(err, identityReady, sinceIdentity) asks in the order the answers are trustworthy:

    1. our own IdentityReady() false → deferred until this agent has an SVID;
    2. a transport-level Unavailable within ReconnectWindow (5s) of identity arriving → INFO registrar connection not yet re-established after identity; retrying;
    3. only then → registrar has no identity yet.

    Used by both the cluster-refresh path (reportReloadFailure, fix(registrar,controller,edge): NotReady until identity, not after a dwell (#740) #744) and the watch loop (failStream). The window's anchor is NotifyIdentityReady, stamped once; a client never told about identity (SPIRE off) gets no window and keeps its pre-PR-5 classification byte for byte. agent: registrar watch stream logs ERROR 'watch stream disconnected … received prior goaway' on every registrar roll; a server-initiated GOAWAY is a graceful handoff, not a failure #718's drain classification is untouched — an established stream ending on a NO_ERROR GOAWAY returns no failure and never reaches failStream.

  3. Tests (hermetic, no cluster, no sleep-based flake): a watch-backed fake whose readiness latch is already satisfied while reads keep failing for 400ms — PreListen must not return before the registry can serve, and the published snapshot must carry the fake registry's cluster and its endpoint IP; a control run proving the failed attempts publish nothing (same snapshot generation as a registry that was serving all along); the never-comes-up registry still falling back local-only inside its bound (existing behaviour, kept); the synchronous-backend negative control; and the three-way classification pinned in common/spire, in the refresher, and in the registrar client — with the agent: first registrar watch-stream attempt logs ERROR 'context canceled' on a clean startup, then retries fine #700 ERROR path as the negative control in each.

  4. Runbook: a paragraph in The agent is stuck waiting for SPIRE on registry connected; generating the initial snapshot and on reading the reconnect INFO as ours rather than the registrar's.

No chart change.

Validation

bazel test --test_arg=-test.short //agent/... //common/... //registry/... — 56/56 pass. make gazelle, bazel build --config=lint on the touched packages, make format-check all clean.

On talos: a plain kubectl -n aether rollout restart ds/aether-agent must produce 0 mesh_dns http_error on every node — the rev211 roll produced 316 on main-worker-02. Grade per node against a pre-roll baseline of the prober counters, and expect registry connected; generating the initial snapshot once per agent with waited in the tens-of-ms to ~1s range, registrar has no identity yet only when a registrar replica really is starting, and zero registry unavailable for initial snapshot.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

…#740)

On the rev211 deploy roll (2026-09-07 20:47Z) main-worker-02's agent published
its initial xDS snapshot 0.4s after acquiring its SVID, while its registrar
client's ClientConn was still recovering from the handshakes it had failed
before identity existed. The registry read failed, the snapshot went out with
no cross-node endpoints, and that node's prober logged 316 http_error over the
~30s until the next refresh repaired it — with SPIRE healthy and both registrar
replicas Ready since 20:46:45Z.

What registryReady meant: PreListen type-asserted registry.ReadyWaiter
(registry/registry.go:52-59) and called WaitReady
(registry/internal/registrar/registrar.go:207-214), which blocks on a ONE-SHOT
latch closed by readyOnce at registrar.go:799 on the first SNAPSHOT_COMPLETE.
So it meant "a complete snapshot arrived at some point" — not "the registry
will serve this read now". The read that follows takes a different path (the
RPC fallback at registrar.go:377-397 when the cache is not serving), and its
failure was handled by the local-only fallback, not by more waiting.

So the wait now ends on a SUCCESSFUL load: WaitReady, then
LoadClustersFromRegistry retried inside the same 15s budget the ready-wait
already had, logging `registry connected; generating the initial snapshot`
with the elapsed wait. A genuinely unreachable registrar costs exactly what it
cost before — the budget, then the unchanged local-only fallback and background
retry. Synchronous backends (no watch) keep the single attempt exactly.
Nothing else in PreListen moves: #700/#712's AssertWatchFilter ordering stands.

Classification, second half. `x509svid: could not get X509 bundle` is raised by
go-spiffe's own verifier against the LOCAL bundle source
(svid/x509svid/verify.go:59-61), so the identical text is produced whichever
party was short of an identity — and after a pre-identity attempt the
ClientConn hands it back from cache, so it is not even current. Text cannot
classify it; local state can. spire.ClassifyHandshake asks in the order the
answers are trustworthy: our own IdentityReady() first, then a transport
failure inside a 5s window after identity arrived (the reconnect), and only
then the peer's. Both the cluster-refresh path (reportReloadFailure, PR 4) and
the watch loop use it. New INFO
`registrar connection not yet re-established after identity; retrying`;
`registrar has no identity yet` now means what it says. #718's drain
classification is untouched (it never reaches failStream).

Tests, all hermetic: a watch-backed registry whose readiness latch is already
satisfied while reads still fail for 400ms — PreListen must not return before
it can serve, and the published snapshot must carry its cluster and endpoint;
a control proving the failed attempts publish nothing; the never-comes-up
registry still falling back local-only inside its bound; the synchronous-backend
control; and the three-way classification in common/spire, the refresher and
the registrar client.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
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