Skip to content

fix(workloadapi): reconnect watches when the server completes the stream - #541

Open
amartinezfayo wants to merge 1 commit into
spiffe:mainfrom
amartinezfayo:reconnect-on-stream-completed
Open

amartinezfayo wants to merge 1 commit into
spiffe:mainfrom
amartinezfayo:reconnect-on-stream-completed

Conversation

@amartinezfayo

Copy link
Copy Markdown
Member

What

Makes the X.509 context, X.509 bundles and JWT bundles watches reconnect when the Workload API server completes the stream without an error.

  • onCompleted in the three stream observers now goes through the same retry path as a retryable error, so the watch reconnects with the configured backoff. It no longer closes the watch context, because the reconnection runs inside it.
  • When the watch cannot be retried (the retry limit is reached or the retry cannot be scheduled), the watcher gets the usual typed terminal error (X509ContextException, X509BundleException or JwtBundleException) with an EOFException as its cause.
  • The DefaultWorkloadApiClient class Javadoc describes the new behavior, and there is a changelog entry under Unreleased.
  • Unchanged are the handling of error statuses, the permission denied reporting rules and the log line Workload API stream is completed.

Why

A stream that ended with an OK status used to stop the watch. onCompleted closed the context and logged at INFO, and the watcher was never told. DefaultX509Source and the JWT sources kept serving their last SVID and bundles and never received another rotation, so the workload looked healthy until that SVID expired.

The SPIFFE Workload API specification covers this in section 4.2. The connection may be terminated by either the server or the client at any time, and the client should then establish a new connection. go-spiffe already behaves this way. It treats the end of the stream like any other error that is not a cancellation and retries with backoff.

A SPIRE agent does not trigger this today, because it drops the connection on shutdown and clients see UNAVAILABLE. Any Workload API server that drains its streams gracefully on shutdown ends them with OK, and every agent restart then stopped the watches of the Java workloads connected to it.

One consequence to be aware of is that a successful update resets the backoff, as it does in go-spiffe. A server that sends an update and then completes every stream is reconnected to at the initial delay for as long as it keeps doing that.

How tested

  • New cases in StreamObserversTest for each watch type. A completed stream schedules a retry without notifying the watcher and without cancelling the watch context. When the retry cannot continue, the watcher receives the typed error with the EOFException cause.
  • New DefaultWorkloadApiClientStreamCompletedTest with an in-process server that completes the first stream of each watch, before or after an update. The client reconnects and receives the next update, with no error reported.
  • All 11 new cases fail on main.
  • FakeWorkloadApi completes each stream after one response, so watches against it now reconnect. Its new withoutCompletion helper keeps a stream open, and the fake in DefaultWorkloadApiClientPermissionDeniedRetryTest uses it so that its request counts do not depend on timing.
  • ./gradlew build passes on JDK 17 (703 core, 115 provider and 74 helper tests).

Signed-off-by: Agustín Martínez Fayó <amartinezfayo@gmail.com>
@amartinezfayo
amartinezfayo marked this pull request as ready for review December 4, 2026 12:09
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