fix(rate_limiter): re-check the backoff deadline after sleeping (close TOCTOU)#97
Open
gadievron wants to merge 1 commit into
Open
fix(rate_limiter): re-check the backoff deadline after sleeping (close TOCTOU)#97gadievron wants to merge 1 commit into
gadievron wants to merge 1 commit into
Conversation
…e TOCTOU) wait_if_needed() read _backoff_until under the lock, released it, then slept for the entry-time duration. If another worker extended _backoff_until (a fresh 429 via report_rate_limit) during that sleep, this worker woke before the new deadline and issued a request into the still-active backoff window -- re-triggering the 429 storm (thundering herd). Wrap the read+sleep in a loop that re-checks _backoff_until after each sleep and keeps waiting until the deadline has actually passed. Preserves the existing contract: returns 0.0 immediately when not in backoff, and increments _total_waits once per waiting call. The loop waits as long as the backoff is genuinely active (a persistently-overloaded API) -- the intended coordinated-backoff behavior, and strictly safer than the old early-wake. Tests: tests/test_rate_limiter_deadline_recheck.py (2 cases: a deadline extended mid-sleep is honored via re-check; not-in-backoff returns 0.0 without sleeping). RED 1 failed -> GREEN 2 passed; full suite 178 passed / 63 skipped (py3.11). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
wait_if_needed() read _backoff_until under the lock, released it, then slept for the entry-time
duration. If another worker extended _backoff_until (a fresh 429 via report_rate_limit) during that
sleep, this worker woke before the new deadline and issued a request into the still-active backoff
window -- re-triggering the 429 storm (thundering herd).
Wrap the read+sleep in a loop that re-checks _backoff_until after each sleep and keeps waiting until
the deadline has actually passed. Preserves the existing contract: returns 0.0 immediately when not
in backoff, and increments _total_waits once per waiting call. The loop waits as long as the backoff
is genuinely active (a persistently-overloaded API) -- the intended coordinated-backoff behavior, and
strictly safer than the old early-wake.
Tests: tests/test_rate_limiter_deadline_recheck.py (2 cases: a deadline extended mid-sleep is honored
via re-check; not-in-backoff returns 0.0 without sleeping). RED 1 failed -> GREEN 2 passed; full suite
178 passed / 63 skipped (py3.11).
Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com