Repository navigation
Stop the rate-limit counter from wrapping below zero - #586
Open
shri30yans wants to merge 1 commit into
Open
shri30yans wants to merge 1 commit into
shri30yans wants to merge 1 commit into
Conversation
OAUTH_RATELIMIT_REMAINING is decremented speculatively with fetch_sub(1) before every upstream request. On a u16 this wraps at zero: a burst of concurrent requests crossing zero flips the estimate to ~65535, and the current_rate_limit < 10 check in json() then skips the proactive OAuth token rollover exactly when it is needed. Move the decrement into burn_rate_limit_unit(), which saturates at 0 via a fetch_update CAS loop, and add four regression tests. The three wrap-behavior tests fail against the previous code (observed 65535) and pass with the fix.
shri30yans
force-pushed
the
fix/rate-limit-counter-wrap
branch
from
October 11, 2026 18:53
f9709fb to
bcb0feb
Compare
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.
Fixes #585
How redlib keeps track
OAUTH_RATELIMIT_REMAINING, and each request to Reddit lowers it by 1 before the request is even sent, so concurrent requests are each accounted for.What happens today
Why it breaks
u16, and the code usesfetch_sub(1). Subtracting 1 from 0 wraps to 65535.The change
burn_rate_limit_unit(), does the decrement withfetch_updateandsaturating_sub: subtract, but never below 0. It is still safe when many threads decrement at once.json()calls the helper instead offetch_sub. That is the entire production change.Tests
Four new tests, isolated so they do not clash with the network tests. On the old code, three fail with the counter reading 65535. On the new code, all four pass.