Repository navigation
fix(limits): Fixes an attempt to subtract with overflow panic in Rate::maybe_reset() during concurrent access when overflow checks are enabled - #1107
Closed
deepak-pandey2005 wants to merge 1 commit into
Conversation
When multiple threads concurrently call maybe_reset, last_reset_time can be updated by another thread after now is sampled, causing now < last_reset. In debug builds or with overflow checks enabled, now - last_reset panics with 'attempt to subtract with overflow'. Similarly, now - 1000 can underflow when now < 1000ms. Use saturating_sub to safely handle timestamp differences without panicking, and add a concurrent regression test.
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
Rate::maybe_reset()during concurrent access when overflow checks are enabled (e.g. in debug builds and test profiles).saturating_subfor elapsed time calculations inmaybe_resetto safely handle timestamp differences across racing threads.test_concurrent_reset_does_not_panic) inpingora-limits.Motivation
In
Rate::maybe_reset():When multiple threads concurrently call
observe()orrate():now.compare_exchangeand advancesself.last_reset_timeto its newer timestamp.last_reset, which is now strictly greater than its samplednow(now < last_reset).now - last_resetpanics with:Additionally:
if now - last_reset >= self.reset_interval_ms * 2can underflow for the same reason.assert!(new >= now - 1000)panics with underflow ifnow < 1000ms (within the first second of process startup).Design
saturating_subfor elapsed time: Replacingnow - last_resetwithnow.saturating_sub(last_reset). When another thread has already won CAS and advancedlast_reset_timepastnow,past_mssafely evaluates to0, which is< reset_interval_ms. The calling thread correctly treats the reset as already performed and continues without panicking.assert!(new >= now.saturating_sub(1000))avoids underflowing during early process uptime.Cost
Tests
test_concurrent_reset_does_not_panic: spawns 8 threads continuously hammeringobserve()andrate()concurrently for 500ms over 1ms reset intervals.pingora-limitspass cleanly:cargo test -p pingora-limits cargo clippy -p pingora-limits --tests