[OpenTelemetry] Metrics - Raise min CircularBufferBuckets capacity to 2#7078
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7078 +/- ##
==========================================
+ Coverage 88.72% 88.74% +0.02%
==========================================
Files 270 270
Lines 12928 12928
==========================================
+ Hits 11470 11473 +3
+ Misses 1458 1455 -3
Flags with carried forward coverage won't be shown. Click here to find out more.
|
@ysolomchenko details? |
reyang
left a comment
There was a problem hiding this comment.
It is unclear what was the problem, and how the change proposed in this PR will solve the problem.
The problem is that If the range spans negative to non-negative values (e.g., -1 and 0), the loop converges to The PR fixes this by ensuring that this non-converging case cannot occur (e.g., by disallowing For end users, nothing changes because existing usage already enforces This PR simply adds an extra safeguard to handle the edge case ( |
Thanks @ysolomchenko! I have two suggestions:
|
I think there's some intentional vagueness being applied to pull requests being raised at the moment ("Found by Codex/security scans."). |
I disagree wearing my SIG security maintainer hat. |
|
I can't speak to that specifically - the first I heard about this particular PR was when it was opened. |
No worries, I'm here to help. Here is my recommendation:
|
|
@reyang, it is not a security issue. It was detected during the Codex/security scans. IMO tests after changes are good enough, as it cover -1,0,1 with exception expectations.. It should prevent us with any unihtentional changes in the future. @ysolomchenko, could you please extend description with such information? With this, we could proceed and merge changes IMO. |
Got it, thank you very much! |
|
Thank you for your contribution @ysolomchenko! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey. |
Defense-in-depth: CalculateScaleReduction can infinite-loop when capacity is 1 and the index range spans negative-to-non-negative values (e.g. begin=-1, end=0), because right-shifting converges to a fixed point where diff remains >= capacity forever. This is not reachable in current usage because the only call site (Base2ExponentialHistogramAggregator) already enforces maxBuckets >= 2, and there is a Debug.Assert(capacity >= 2) in CalculateScaleReduction. This change hardens CircularBufferBuckets itself so future callers cannot accidentally trigger the infinite loop. Inspired by open-telemetry/opentelemetry-dotnet#7078. Co-authored-by: Copilot <[email protected]>
## Summary Defense-in-depth change to `CircularBufferBuckets`: raise the minimum accepted `capacity` from 1 to 2. ## Motivation `CalculateScaleReduction` (inside `TryIncrement`) can infinite-loop when `capacity == 1` and the index range spans negative-to-non-negative values (e.g. `begin = -1`, `end = 0`). In that scenario, right-shifting converges to a fixed point where `diff` remains `>= capacity` forever. **This is not reachable in current usage** the only caller (`Base2ExponentialHistogramAggregator`) already enforces `maxBuckets >= 2` via `MinBuckets`, and there is an existing `Debug.Assert(capacity >= 2)` inside `CalculateScaleReduction`. This change simply hardens `CircularBufferBuckets` itself so future callers cannot accidentally trigger the issue. Inspired by [open-telemetry/opentelemetry-dotnet#7078](open-telemetry/opentelemetry-dotnet#7078). ## Changes - **`CircularBufferBuckets.cs`**: Changed constructor guard from `capacity < 1` to `capacity < 2`, updated error message. - **`Base2ExponentialHistogramAggregatorTests.cs`**: Added `CircularBufferBucketsRejectsInvalidCapacity` (Theory with 0, 1, -1) and `CircularBufferBucketsAcceptsCapacityOfTwo` tests. Co-authored-by: Copilot <[email protected]>
Fixes # N/A
Design discussion issue # N/A
Found by Codex/security scans.
Changes
Base2ExponentialBucketHistogrambucket initialization now rejects invalidmaxBucketsvalues smaller than 2, preventing a possible hang during metrics recording.Merge requirement checklist
AppropriateCHANGELOG.mdfiles updated for non-trivial changesChanges in public API reviewed (if applicable)