Skip to content

fix(workloadapi): stop newSource from storing defaults in the source options - #499

Merged
maxlambrecht merged 1 commit into
spiffe:mainfrom
NopeCharon:fix/source-options-client-reuse
Nov 5, 2026
Merged

maxlambrecht merged 1 commit into
spiffe:mainfrom
NopeCharon:fix/source-options-client-reuse

Conversation

@NopeCharon

Copy link
Copy Markdown
Contributor

What

DefaultX509Source.newSource, DefaultJwtSource.newSource and CachedJwtSource.newSource now keep the Workload API client they create, and the default init timeout, in local variables. Before, they stored them in the options object passed in. newSource no longer modifies its argument, and each call without a configured workloadApiClient creates its own client. A client set in the options is used, and closed with the source, as before.

Fixes #497

Why

When init failed, for example because initTimeout expired before the SPIRE agent was up, newSource closed the source and so closed the client it had created. The options still held that client. Every later newSource(options) call reused it and failed immediately with IllegalStateException: Cannot register watch on closed Workload API client. A startup loop retrying newSource with the same options never recovered, even after the agent became available.

After a successful call, a second source created from the same options shared the first one's client, so closing either source broke the other.

This is the same pattern #494 fixes for DefaultWorkloadApiClient.newClient and ClientOptions, one level up.

How tested

  • newSource_timeout_retryWithSameOptions_createsNewClient in DefaultX509SourceTest, DefaultJwtSourceTest and CachedJwtSourceTest calls newSource twice with the same options, pointing at a socket nobody listens on. It checks that both calls fail with the init timeout and that the options still have no client. The JWT variants also check that initTimeout is still unset.
  • Without the change, all three fail on the second call: expected: <Timeout waiting for ...> but was: <Cannot register watch on closed Workload API client>.
  • ./gradlew :java-spiffe-core:check :java-spiffe-core:japicmp :java-spiffe-core:javadoc on JDK 17.

@maxlambrecht maxlambrecht left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed against #497. The fix consistently avoids mutating caller options across all three source factories while preserving existing behavior for explicitly supplied clients.
LGTM

…options

DefaultX509Source.newSource, DefaultJwtSource.newSource and
CachedJwtSource.newSource stored the Workload API client they created,
and DEFAULT_TIMEOUT, in the options object passed by the caller.

When initialization failed, for example because initTimeout expired
before the SPIRE agent was available, newSource closed the source and so
closed that client, but the options kept a reference to it. Every later
newSource call with the same options reused the closed client and failed
immediately with "Cannot register watch on closed Workload API client",
so a retry loop never recovered even after the agent came up. After a
successful call, a second source created from the same options shared
the first one's client, and closing either source broke the other.

Keep the client and the timeout in local variables instead. newSource no
longer modifies its argument, and each call without a configured client
creates its own. A client set in the options is used, and closed with the
source, as before.

Fixes spiffe#497

Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
@maxlambrecht
maxlambrecht force-pushed the fix/source-options-client-reuse branch from 4d4b825 to 6ab9238 Compare November 5, 2026 20:59
@maxlambrecht
maxlambrecht merged commit e5731f6 into spiffe:main Nov 5, 2026
8 checks passed
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.

newSource stores its own Workload API client in the options, so retrying after a failed init always fails

2 participants