Skip to content

fix(workloadapi): honor sub-second source init timeouts and interrupts - #530

Open
NopeCharon wants to merge 1 commit into
spiffe:mainfrom
NopeCharon:fix/source-init-timeout
Open

NopeCharon wants to merge 1 commit into
spiffe:mainfrom
NopeCharon:fix/source-init-timeout

Conversation

@NopeCharon

Copy link
Copy Markdown
Contributor

What

Fixes how DefaultX509Source, DefaultJwtSource and CachedJwtSource wait for the first Workload API update in newSource:

  • Sub-second timeouts were truncated. The wait used timeout.getSeconds(), which drops the fractional part of the initTimeout option and of the spiffe.newX509Source.timeout / spiffe.newJwtSource.timeout system properties. Duration.ofMillis(500) (or PT0.5S) became a zero-second wait, so newSource failed with Timeout waiting for ... right away. Duration.ofMillis(1500) waited one second. RetryHandler had the same problem with retry delays, fixed in 0.9.0.
  • Interrupts were swallowed. Without a timeout, an interrupt ended the wait but initialization still counted as successful. newSource then returned a source that had not received an update yet. DefaultX509Source.getX509Svid() threw IllegalStateException("X.509 SVID source not initialized"), and the JWT sources' getBundleForTrustDomain threw NullPointerException. With a timeout, an interrupt was reported as a timeout.

Changes:

  • The ThreadUtils await helpers are replaced by await(CountDownLatch, Duration). It waits for the whole duration in nanoseconds and still treats a zero duration as no deadline. Durations too long for a nanosecond count are capped instead of overflowing, and InterruptedException propagates.
  • On an interrupt, newSource closes the source, restores the thread's interrupt status and throws X509SourceException / JwtSourceException (Error creating ... source) with the InterruptedException as the cause. The newSource Javadoc documents this.
  • Unchanged: zero still means no deadline, negative timeouts still fail immediately with a timeout, and timeout and watcher errors keep their messages. ThreadUtils is in an internal package that japicmp excludes, and its only callers are these three sources.
  • Changelog entries under Unreleased.

Why

A sub-second initTimeout is a reasonable choice for a workload that should start without SPIFFE material when no agent is reachable, or for tests. It made newSource fail every time, before the Workload API could answer. A source returned after an interrupt violates the newSource contract ("with the SVID and bundles initialized"). The failures then show up later, far from the cause, and in the JWT sources as a NullPointerException.

How tested

  • New tests in DefaultX509SourceTest, DefaultJwtSourceTest and CachedJwtSourceTest:
    • with a 500 ms initTimeout and no Workload API, newSource waits at least 500 ms before failing with the timeout
    • when the calling thread is interrupted while waiting, with no timeout and with a 5 s timeout, newSource throws the source exception with an InterruptedException cause, closes the client and keeps the interrupt status
  • All 9 new tests fail on main: the sub-second ones fail immediately, and the interrupted ones return a source or report a timeout.
  • Commands, on JDK 17: ./gradlew :java-spiffe-core:test :java-spiffe-core:javadoc :java-spiffe-core:japicmp :java-spiffe-provider:test :java-spiffe-helper:test (618 core, 108 provider and 74 helper tests pass).

DefaultX509Source, DefaultJwtSource and CachedJwtSource wait for the
first Workload API update with timeout.getSeconds(), which drops the
fractional part of the initTimeout option and of the
spiffe.newX509Source.timeout and spiffe.newJwtSource.timeout system
properties. A timeout of 500 ms became a zero-second wait, so newSource
failed with "Timeout waiting for ..." right away, and 1.5 s became one
second. RetryHandler had the same problem with retry delays, fixed in
0.9.0.

The wait also swallowed interrupts. Without a timeout, an interrupt
ended the wait but init still reported success, so newSource returned
a source that had not received an update: DefaultX509Source then threw
IllegalStateException("... not initialized") and the JWT sources threw
NullPointerException from getBundleForTrustDomain. With a timeout, an
interrupt was reported as a timeout.

Replace the ThreadUtils await helpers with await(CountDownLatch,
Duration), which waits for the whole duration in nanoseconds, treats a
zero duration as no deadline as before, caps durations too long for a
nanosecond count instead of overflowing, and lets InterruptedException
propagate. The sources' init methods use it, and newSource closes the
source, restores the thread's interrupt status and throws
X509SourceException or JwtSourceException with the InterruptedException
as the cause. Negative timeouts still fail immediately with a timeout.
ThreadUtils is in an internal package that japicmp excludes, and its
only callers are these three sources.

Add tests to each source's test class: a 500 ms initTimeout waits at
least 500 ms before failing, and an interrupted wait, with and without
a timeout, fails newSource, closes the client and keeps the interrupt
status. All of them fail without this change. Document the interrupt
behavior in the newSource Javadoc and add changelog entries.

Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
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