fix: eliminate TOCTOU cache stampede in cachingClientCreator - #593
Open
adityakrmishra wants to merge 2 commits into
Open
fix: eliminate TOCTOU cache stampede in cachingClientCreator#593adityakrmishra wants to merge 2 commits into
adityakrmishra wants to merge 2 commits into
Conversation
Under concurrent load, multiple goroutines calling NewInstallationClient or NewInstallationV4Client for the same installation ID could all miss the LRU cache simultaneously (TOCTOU race between Get and Add) and each proceed to call the delegate — resulting in N redundant token-fetch HTTP requests to the GitHub API. Fix by wrapping the slow delegate call in a singleflight.Group so only one in-flight request per installation key is made; all other concurrent callers share the result. A double-checked Get inside the singleflight closure ensures the cache is used if a prior call already populated it. Adds TestCacheStampede_Vulnerable to prove the fix: 100 goroutines result in exactly 1 delegate invocation after the patch. Fixes: TOCTOU race in cachingClientCreator.NewInstallationClient
✅ Successfully generated changelog entry!Entry generated via PR titleTo modify this entry, edit PR title using proper format. 📋Changelog Preview🐛 Fixes
|
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.
Description
Resolves a TOCTOU (Time-of-Check to Time-of-Use) race condition in
cachingClientCreatorthat causes a cache stampede against the GitHub API.Problem:
Under concurrent load (e.g., webhook bursts), multiple goroutines requesting a token for the same
installationIDsimultaneously miss the LRU cache. This results in N redundant HTTP requests to GitHub before the cache is populated, risking rate limit exhaustion.Solution:
Implemented
golang.org/x/sync/singleflightaround the delegate call.Getinside the closure to safely catch races where the cache was populated just before the lock.Proof of Work (Test Results)
Added
TestCacheStampede_Vulnerablewhich fires 100 concurrent requests for the sameinstallationID.Before Patch:
After Patch: