Preserve token invalidation across concurrent refreshes - #1841
Open
sylvesterkaczmarek wants to merge 1 commit into
Open
Preserve token invalidation across concurrent refreshes#1841sylvesterkaczmarek wants to merge 1 commit into
sylvesterkaczmarek wants to merge 1 commit into
Conversation
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.
Summary
Make
TokenCache.invalidate()safe when it races with an in-flight credential refresh.TokenCachedeliberately releases its lock while calling the credential provider. Before this change, an invalidation that happened during that provider call could be lost:invalidate(), clearing the cache and requestingforce_refresh=True;The cache can therefore return and retain a token from a refresh that began before the invalidation which was supposed to revoke it.
The same race also affected advisory refresh failures: an advisory refresh that failed after a concurrent invalidation could fall back to the cached token that had just been invalidated.
Fix
Introduce an invalidation generation guarded by the existing cache lock.
Each refresh snapshots the generation and force flag before leaving the lock. When the provider returns:
force_refreshflag is consumed only by a successful refresh belonging to the current generation;Repeated invalidations are coalesced while the cache is already invalidated and a forced refresh is pending. This matters for concurrent requests that all receive 401 responses for the same revoked token: those duplicate invalidations must not continually invalidate the replacement refresh.
The provider call still runs outside the lock, preserving the existing non-blocking/single-flight design.
Regression coverage
Adds deterministic threaded tests for:
force_refresh=Truefor the next provider attempt after that failure.The concurrency scenarios were also exercised independently against the synchronization logic before preparing the PR.