Fix #768: TinyUfo weight underflow panic from evict race condition#823
Open
cobyfrombrooklyn-bot wants to merge 1 commit intocloudflare:mainfrom
Open
Fix #768: TinyUfo weight underflow panic from evict race condition#823cobyfrombrooklyn-bot wants to merge 1 commit intocloudflare:mainfrom
cobyfrombrooklyn-bot wants to merge 1 commit intocloudflare:mainfrom
Conversation
…anic A race condition exists where one thread evicts an item (subtracting its weight) before another thread finishes adding the weight after insertion. This causes fetch_sub to underflow the atomic usize, wrapping to usize::MAX, which then causes 'attempt to add with overflow' panic in evict_to_limit. Replace all fetch_sub on weight atomics with fetch_update using saturating_sub, and use saturating_add in evict_to_limit's weight comparison. This prevents the underflow without adding locks. Fixes cloudflare#768
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.
A race condition exists in TinyUfo where one thread evicts an item (subtracting its weight via
fetch_sub) before another thread finishes adding the weight after insertion. This causes the atomicusizeto underflow (wrap tousize::MAX), which then triggersattempt to add with overflowpanic inevict_to_limit.Fix
fetch_subcalls onsmall_weightandmain_weightwithfetch_updateusingsaturating_sub- prevents underflow from wrapping.saturating_addinevict_to_limitweight comparison - prevents overflow when summing weights.This is a minimal, lock-free fix that preserves the existing concurrent design.
Tests
Added two tests:
test_weight_saturating_sub_no_underflow: Directly verifies that weight atomics handle underflow gracefully by setting weight to 0 then subtracting, and thatevict_to_limithandles near-usize::MAXweights without panicking.test_concurrent_put_no_overflow_panic: Stress test with 20 threads doing concurrent put/get operations (reproduces the original race from the issue).All 16 TinyUfo tests pass on macOS ARM (Apple Silicon).
Fixes #768