Fix race in SimpleFixedSizeExemplarReservoir offer/collect (#5431)#5445
Closed
feiiiiii5 wants to merge 1 commit into
Closed
Fix race in SimpleFixedSizeExemplarReservoir offer/collect (#5431)#5445feiiiiii5 wants to merge 1 commit into
feiiiiii5 wants to merge 1 commit into
Conversation
`_find_bucket_index` does `self._measurements_seen += 1` then `randrange(0, self._measurements_seen)`. Between those two lines, `collect()` can run `_reset()` and zero `_measurements_seen`, producing `ValueError: empty range for randrange() (0, 0, 0)`. The `defaultdict`-backed `_reservoir_storage` has the matching race: collect() iterates `self._reservoir_storage.items()` while offer() can insert a new bucket, raising `RuntimeError: dictionary changed size during iteration`. Add a `threading.Lock` on `FixedSizeExemplarReservoirABC` and guard `offer()` / `collect()` with it. `AlignedHistogramBucketExemplarReservoir` overrides `offer()`, so its override takes the lock too — otherwise the shared `_reservoir_storage` would still be unsafe. Regression test `test_concurrent_offer_and_collect_does_not_raise` runs 2 offer threads + 1 collect thread for 1.0s. Without the lock it reproduces the exact `ValueError` from the issue; with the lock it passes.
Contributor
|
Closing this given that there is already an open PR #5437 |
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.
Fixes #5431.
Problem
SimpleFixedSizeExemplarReservoir._find_bucket_indexdoes:Between (a) and (b), a concurrent
collect()can run_reset()and zero_measurements_seen, producing:FixedSizeExemplarReservoirABC.collect()has a matching race: it iteratesself._reservoir_storage.items()(adefaultdict) whileoffer()caninsert a new bucket, raising
RuntimeError: dictionary changed size during iteration.Fix
Add a
threading.LockonFixedSizeExemplarReservoirABC.__init__andguard
offer()andcollect()with it.AlignedHistogramBucketExemplarReservoiroverridesoffer()(becauseit computes its bucket index from the value, not the reservoir state),
so its override must also acquire the lock — otherwise the shared
_reservoir_storagewould still be unsafe.Verification
test_concurrent_offer_and_collect_does_not_raiseruns 2
offerthreads + 1collectthread for 1.0s on asize=1reservoir (maximizes the probability of hitting the
randrangebranch on every offer after the first).
`ValueError: empty range for randrange() (0, 0, 0)` from the issue.
With the fix, it passes.
Notes
FixedSizeExemplarReservoirABC, not on the baseExemplarReservoir, because the base has no shared mutable state toprotect — the race is specific to the
_reservoir_storagedefaultdictand the subclass-specific reservoir state (
_measurements_seen).offer()is now O(1) under the lock (no I/O, noallocations beyond the existing
defaultdictlookup), so contentionis bounded. For high-throughput metrics pipelines, the next step would
be a per-bucket lock or a lock-free reservoir, but neither is in scope
for this bug fix.