fix(atomic): initialize bucket atomics before publishing them in ETS - #202
Merged
Conversation
added 2 commits
September 2, 2026 14:25
TokenBucket and LeakyBucket created the atomic, inserted it into ETS, and only then wrote the initial packed value. Between insert_new and the put, a concurrent hit could look the atomic up and read zero, which unpacks as an empty bucket at timestamp 0. With refill_rate 0 that hit is denied and lost; this is the intermittent failure in the Atomic TokenBucket "race condition" test. Initialize both slots before insert_new so a published atomic is always valid. If insert_new loses, the unused atomic is simply dropped. Stress check (64 concurrent hits on 2000 fresh keys, refill_rate 0): master lost hits on 2 keys, this branch on 0. Co-Authored-By: Emmanuel Pinault <dev@pinault-family.us> Claude-Session: https://claude.ai/code/session_011dH6kRtZqUkVpoGo79CKYG
The 7.5.0 release commit is on master but not yet tagged, so this fix ships with it. Co-Authored-By: Emmanuel Pinault <dev@pinault-family.us> Claude-Session: https://claude.ai/code/session_011dH6kRtZqUkVpoGo79CKYG
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
The Atomic
TokenBucketandLeakyBucketcreate an:atomicsref, insert it into ETS withinsert_new, and only then write the initial packed value. Between those two steps a concurrenthit/5can look the atomic up and read0, which unpacks as an empty bucket at timestamp0.For the token bucket with
refill_rate: 0that hit is denied and lost. With a nonzero refill rate it works by accident, because(now - 0) * refill_rateis huge and gets capped atcapacity. This is the cause of the intermittent failure in the AtomicTokenBucket"race condition" test (seen on #201, CI run 33683962078, jobtest (1.17, 27)).The fix initializes both slots before
insert_new, so a published atomic is always valid. Ifinsert_newloses the race, the unused atomic is dropped and the retry reads the winner's.Ships as part of 7.5.0: the release commit (#200) is on master but not yet tagged, so the changelog entry is filed under
7.5.0rather thanUnreleased.Verification
refill_rate: 0, asserting the final level is0. Master lost hits on 2 keys; this branch on 0.mix test150 passed;test/hammer/atomicrun 30 times, 30/30 pass.mix compile --warnings-as-errors,mix credo --strict,mix format --check-formattedclean.Test plan
🤖 Generated with Claude Code by Emmanuel Pinault
https://claude.ai/code/session_011dH6kRtZqUkVpoGo79CKYG