Skip to content

fix(agent): re-subscribe SPIRE streams with backoff instead of failing - #133

Merged
bpalermo merged 1 commit into
mainfrom
fix/spire-stream-reconnect
Jun 11, 2026
Merged

bpalermo merged 1 commit into
mainfrom
fix/spire-stream-reconnect

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

Problem

A SPIRE agent restart closes the delegated-identity subscription streams, with two failure modes in the bridge:

  • Bundle stream (bridge.go): the closed channel made Bridge.Start return an error, failing the controller-runtime runnable and restarting the entire agent. Observed once in the 2026-06-11 e2e run (bundle subscription stream closed unexpectedly).
  • Per-pod SVID streams: a closed stream exited its goroutine silently and permanently, leaving the stale subscriptions entry behind (blocking re-subscribe for the same netns) and freezing that pod's SVID until expiry — a delayed, hard-to-diagnose mTLS outage.

Fix

  • Both stream kinds are now maintained with a jittered exponential-backoff reconnect loop (1s → 30s, ×2, 20% jitter — same policy as the registrar watch stream). Backoff resets only once a response is received on the new stream, so a connect-then-instant-close flap cannot hot-loop.
  • Cached secrets keep serving while disconnected; SPIRE sends the full current state as the first response on every new stream, so a plain re-subscribe fully resynchronizes — no reconciliation needed.
  • The SVID re-subscribe happens inside the existing per-pod goroutine, so the subscription entry and its cancel func stay valid for UnsubscribePod.
  • New counters by stream kind (bundle/svid) so a flapping SPIRE agent shows up in dashboards rather than as mystery restarts:
    • aether.agent.spire.stream_failures — unexpected stream ends + failed re-subscribe attempts
    • aether.agent.spire.stream_reconnects — streams re-established after a failure

Testing

New bridge_reconnect_test.go with a fake DelegatedIdentityServer over a real UDS whose first stream of each kind sends one response then ends (simulated SPIRE agent restart):

  • TestBundleStreamReconnects — Start does not return, re-subscribes, and still exits nil on context cancellation
  • TestSVIDStreamResubscribes — the pod stream re-subscribes and stays tracked in subscriptions

Backoff bounds are bridge fields (set from the constants in NewBridge) so tests shorten them to 10ms.

Full suite: bazel test //... 38/38 pass; --config=lint clean.

🤖 Generated with Claude Code

A SPIRE agent restart closes the delegated-identity subscription streams,
which previously had two failure modes:

- The bundle stream closing made Bridge.Start return an error, failing the
  controller-runtime runnable and restarting the entire agent (observed in
  the 2026-06-11 e2e run).
- A per-pod SVID stream closing exited its goroutine silently while leaving
  the stale subscription entry behind, freezing that pod's SVID until expiry
  with no re-subscribe possible for the same netns.

The bridge now maintains both stream kinds with a jittered exponential
backoff reconnect loop (1s..30s, matching the registrar watch-stream
policy). Backoff resets only once a response is received on the new stream,
so a connect-then-instant-close flap cannot hot-loop. Cached secrets keep
serving while disconnected, and SPIRE sends the full current state as the
first response on every new stream, so a plain re-subscribe fully
resynchronizes.

Stream churn is now visible via two new counters by stream kind
(bundle/svid):

- aether.agent.spire.stream_failures
- aether.agent.spire.stream_reconnects

Tested with a fake DelegatedIdentity server over a UDS whose first stream
of each kind ends after one response: the bridge re-subscribes both kinds,
keeps the pod subscription tracked, and still shuts down cleanly.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@bpalermo
bpalermo merged commit c62c78a into main Jun 11, 2026
10 checks passed
@bpalermo
bpalermo deleted the fix/spire-stream-reconnect branch June 11, 2026 00: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