fix(shared): serialize TokenBucket.waitAndConsume to prevent token over-issue (closes #209)#213
Merged
Merged
Conversation
waitAndConsume computed a single wait then unconditionally did `this.tokens -= count` after sleeping. N concurrent waiters that found the bucket empty all scheduled the same wait, woke together, and each subtracted from a bucket that refilled enough for only one — driving `tokens` negative and letting the limiter exceed its rate (the exact failure it exists to prevent). Used by the annotator / CourtListener client. Serialize waiters via a FIFO promise chain so they consume one at a time, each looping until enough tokens have actually refilled and only subtracting after a re-check (never negative). Add a capacity guard: waitAndConsume(count > capacity) now throws instead of waiting forever. New tests: concurrent waiters are served FIFO without over-issuing, and the capacity guard throws. shared 27 pass; annotator/fetcher and build unaffected. Closes #209 Co-Authored-By: Claude Opus 4.8 (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.
Completes #209 (the rate-limiter half; the
retry.tshalf is in #212).Bug
TokenBucket.waitAndConsumecomputed a singlewaitMs, slept, then didthis.tokens -= countunconditionally. N concurrent waiters that found the bucket empty all scheduled the same wait, woke together, and each subtracted from a bucket that refilled enough for only one →tokensgoes negative and the limiter silently exceeds its rate. It's used by the annotator and the CourtListener client; today's call sites are sequential so it's latent, but it's a real correctness bug in a shared primitive and the existing test only exercised a single waiter.Fix
refill(), and onlytokens -= countafter re-checkingtokens >= count. Concurrent waiters can never over-issue, and ordering is preserved.waitAndConsume(count > capacity)throwsRangeErrorinstead of waiting for a deficit that can never close.Tests
tryConsume/refill/waitAndConsumetests unchanged.shared27 pass;annotator76 / build 8/8 unaffected.🤖 Generated with Claude Code