Repository navigation
fix(workloadapi): honor sub-second source init timeouts and interrupts - #530
Open
NopeCharon wants to merge 1 commit into
Open
NopeCharon wants to merge 1 commit into
NopeCharon wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes how
DefaultX509Source,DefaultJwtSourceandCachedJwtSourcewait for the first Workload API update innewSource:timeout.getSeconds(), which drops the fractional part of theinitTimeoutoption and of thespiffe.newX509Source.timeout/spiffe.newJwtSource.timeoutsystem properties.Duration.ofMillis(500)(orPT0.5S) became a zero-second wait, sonewSourcefailed withTimeout waiting for ...right away.Duration.ofMillis(1500)waited one second.RetryHandlerhad the same problem with retry delays, fixed in 0.9.0.newSourcethen returned a source that had not received an update yet.DefaultX509Source.getX509Svid()threwIllegalStateException("X.509 SVID source not initialized"), and the JWT sources'getBundleForTrustDomainthrewNullPointerException. With a timeout, an interrupt was reported as a timeout.Changes:
ThreadUtilsawait helpers are replaced byawait(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, andInterruptedExceptionpropagates.newSourcecloses the source, restores the thread's interrupt status and throwsX509SourceException/JwtSourceException(Error creating ... source) with theInterruptedExceptionas the cause. ThenewSourceJavadoc documents this.ThreadUtilsis in an internal package that japicmp excludes, and its only callers are these three sources.Why
A sub-second
initTimeoutis a reasonable choice for a workload that should start without SPIFFE material when no agent is reachable, or for tests. It madenewSourcefail every time, before the Workload API could answer. A source returned after an interrupt violates thenewSourcecontract ("with the SVID and bundles initialized"). The failures then show up later, far from the cause, and in the JWT sources as aNullPointerException.How tested
DefaultX509SourceTest,DefaultJwtSourceTestandCachedJwtSourceTest:initTimeoutand no Workload API,newSourcewaits at least 500 ms before failing with the timeoutnewSourcethrows the source exception with anInterruptedExceptioncause, closes the client and keeps the interrupt statusmain: the sub-second ones fail immediately, and the interrupted ones return a source or report a timeout../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).