Skip to content

fix(usage-limit): resolve race condition in usage tracker#273

Merged
dev-jodee merged 2 commits intosolana-foundation:mainfrom
raushan728:fix/usage-limit-race-condition
Jan 5, 2026
Merged

fix(usage-limit): resolve race condition in usage tracker#273
dev-jodee merged 2 commits intosolana-foundation:mainfrom
raushan728:fix/usage-limit-race-condition

Conversation

@raushan728
Copy link
Contributor

@raushan728 raushan728 commented Dec 22, 2025

Description

The current implementation of UsageTracker uses a get() then increment() pattern (check-then-act), which is susceptible to race conditions under high concurrency. This PR refactors the logic to use an atomic increment() followed by a threshold check.

Changes

  • Core Logic: Switched to atomic increment-then-check in check_usage_limit.
  • Performance: Reduced Redis network calls from 2 to 1 per request.
  • Reliability: Updated cache_validator tests to use port 54321 to ensure consistent failure assertions regardless of local Redis instances running on the default port.

Verification Results

  • All unit tests passed: 386 passed; 0 failed.
  • Manually verified that usage limits are strictly enforced.

Important

Refactor UsageTracker to use atomic increment for usage limits, reducing Redis calls and improving reliability.

  • Core Logic:
    • Refactor check_usage_limit in usage_tracker.rs to use atomic increment() before checking limits.
  • Performance:
    • Reduce Redis calls from 2 to 1 per request in usage_tracker.rs.
  • Reliability:
    • Update cache_validator.rs tests to use port 54321 for consistent failure assertions.
  • Tests:
    • Remove test_usage_limit_store_get_error_fallback_enabled and test_usage_limit_store_get_error_fallback_disabled from usage_tracker.rs.

This description was created by Ellipsis for 38c9fe7. You can customize this summary. It will automatically update as commits are pushed.

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.
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

Looks good to me! 👍

Reviewed everything up to 38c9fe7 in 46 seconds. Click for details.
  • Reviewed 105 lines of code in 2 files
  • Skipped 0 files when reviewing.
  • Skipped posting 2 draft 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 casting self.max_transactions to 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% <= threshold 50% 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% <= threshold 50% 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 Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@raushan728
Copy link
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 dev-jodee self-assigned this Jan 5, 2026
@dev-jodee dev-jodee self-requested a review January 5, 2026 16:33
Copy link
Contributor

@dev-jodee dev-jodee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, will merge this one even if another pr includes it so we keep a good separation

@dev-jodee dev-jodee merged commit c22ee83 into solana-foundation:main Jan 5, 2026
9 checks passed
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
@raushan728 raushan728 deleted the fix/usage-limit-race-condition branch March 4, 2026 03:22
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.

2 participants