Fix TokenBucketRateLimiter AttemptAcquire(0) with fractional tokens#124498
Open
apoorvdarshan wants to merge 1 commit intodotnet:mainfrom
Open
Fix TokenBucketRateLimiter AttemptAcquire(0) with fractional tokens#124498apoorvdarshan wants to merge 1 commit intodotnet:mainfrom
apoorvdarshan wants to merge 1 commit intodotnet:mainfrom
Conversation
…al _tokenCount Change availability checks from > 0 / != 0 to >= 1 so fractional tokens below 1.0 are not treated as available permits, making AttemptAcquire(0) consistent with GetStatistics().CurrentAvailablePermits. Fixes dotnet#118192
Contributor
|
Tagging subscribers to this area: @agocke, @VSadov |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in TokenBucketRateLimiter where AttemptAcquire(0) incorrectly succeeded when fractional tokens were present (e.g., _tokenCount = 0.5). The issue arose because _tokenCount is stored as a double for accurate replenishment calculations, but availability checks used > 0 or != 0 instead of >= 1, causing inconsistency with GetStatistics().CurrentAvailablePermits which truncates to long.
Changes:
- Changed three availability checks from
> 0/!= 0to>= 1to ensure fractional tokens are not treated as available permits - Added regression test
AttemptAcquireZeroWithFractionalTokensReportsUnavailablethat verifies the probe behavior with fractional tokens
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Threading.RateLimiting/src/System/Threading/RateLimiting/TokenBucketRateLimiter.cs | Fixed three conditions in AttemptAcquireCore, AcquireAsyncCore, and TryLeaseUnsynchronized to check _tokenCount >= 1 instead of > 0 or != 0 |
| src/libraries/System.Threading.RateLimiting/tests/TokenBucketRateLimiterTests.cs | Added regression test that drains tokens, replenishes to create fractional tokens (~0.5), and verifies AttemptAcquire(0) correctly returns unavailable |
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
AttemptAcquire(0)incorrectly succeeding when_tokenCountholds a fractional value (e.g.0.5) by changing three availability checks from> 0/!= 0to>= 1AttemptAcquire(0)consistent withGetStatistics().CurrentAvailablePermits, which truncates to(long)and reports0Fixes #118192
Test plan
AttemptAcquireZeroWithFractionalTokensReportsUnavailablepassesTokenBucketRateLimiterTestscontinue to passdotnet test src/libraries/System.Threading.RateLimiting/tests/System.Threading.RateLimiting.Tests.csproj🤖 Generated with Claude Code