fix(usage-limit): resolve race condition in usage tracker#273
Merged
dev-jodee merged 2 commits intosolana-foundation:mainfrom Jan 5, 2026
Merged
Conversation
Refactored the usage tracking logic from check-then-act to atomic increment-then-check. This prevents limit bypassing during concurrent requests. Also updated validator tests to use a non-standard port to avoid environment conflicts.
Contributor
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed everything up to 38c9fe7 in 46 seconds. Click for details.
- Reviewed
105lines of code in2files - Skipped
0files when reviewing. - Skipped posting
2draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. crates/lib/src/usage_limit/usage_tracker.rs:67
- Draft comment:
Good refactoring: using an atomic increment avoids the race condition and cuts down on network calls. Please ensure that castingself.max_transactionsto u32 is safe (i.e. it cannot exceed u32::MAX). - Reason this comment was not posted:
Comment did not seem useful. Confidence is useful =0%<= threshold50%The comment is asking the author to ensure that a casting operation is safe, which violates the rule against asking the author to ensure behavior is intended or safe. It does not provide a specific suggestion or ask for a specific test to be written.
2. crates/lib/src/validator/cache_validator.rs:172
- Draft comment:
Using port 54321 instead of the default 6379 in tests ensures connection failure if a local Redis is running, which makes the tests more reliable. - Reason this comment was not posted:
Comment did not seem useful. Confidence is useful =0%<= threshold50%This comment is purely informative, explaining why a certain port is used in tests. It doesn't provide a suggestion, ask for confirmation, or point out a potential issue. Therefore, it violates the rule against making purely informative comments.
Workflow ID: wflow_GTrDoZ1oIj5S26ng
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
Contributor
Author
|
Note: The atomic increment logic from this PR is also included in #277, which provides a more comprehensive update (SignerPool failover, Token-2022 security, etc.). I recommend reviewing #277 first. If you choose to merge that, this PR can be closed. However, if you'd like to merge this small fix independently, I am happy to resolve the resulting conflict in #277 afterward. |
dev-jodee
approved these changes
Jan 5, 2026
Contributor
dev-jodee
left a comment
There was a problem hiding this comment.
lgtm, will merge this one even if another pr includes it so we keep a good separation
amilz
pushed a commit
that referenced
this pull request
Jan 6, 2026
* fix(usage-limit): resolve race condition using atomic increment Refactored the usage tracking logic from check-then-act to atomic increment-then-check. This prevents limit bypassing during concurrent requests. Also updated validator tests to use a non-standard port to avoid environment conflicts. * style: fix formatting in usage_tracker
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
The current implementation of
UsageTrackeruses aget()thenincrement()pattern (check-then-act), which is susceptible to race conditions under high concurrency. This PR refactors the logic to use an atomicincrement()followed by a threshold check.Changes
check_usage_limit.cache_validatortests to use port54321to ensure consistent failure assertions regardless of local Redis instances running on the default port.Verification Results
386 passed; 0 failed.Important
Refactor
UsageTrackerto use atomic increment for usage limits, reducing Redis calls and improving reliability.check_usage_limitinusage_tracker.rsto use atomicincrement()before checking limits.usage_tracker.rs.cache_validator.rstests to use port54321for consistent failure assertions.test_usage_limit_store_get_error_fallback_enabledandtest_usage_limit_store_get_error_fallback_disabledfromusage_tracker.rs.This description was created by
for 38c9fe7. You can customize this summary. It will automatically update as commits are pushed.