Skip to content

opentelemetry-api: prevent recursive attribute value crashes - #5976

Closed
csw7777 wants to merge 3 commits into
open-telemetry:mainfrom
csw7777:fix/attribute-nesting-depth
Closed

csw7777 wants to merge 3 commits into
open-telemetry:mainfrom
csw7777:fix/attribute-nesting-depth

Conversation

@csw7777

@csw7777 csw7777 commented Nov 27, 2026 •

Copy link
Copy Markdown

Summary

Fixes #5564.

  • Track active ancestor container IDs to reject cycles immediately, avoiding exponential traversal for branching cycles while allowing non-cyclic shared references.
  • Bound recursive attribute cleaning to 20 levels, replacing excessively nested values with None instead of leaking RecursionError into application code.
  • Clean top-level attribute values independently so an invalid value does not discard valid siblings.
  • Apply the same isolation to metric Measurement attributes.
  • Add regression coverage for cyclic sequences and mappings, the exact depth boundary, spans, resources, BoundedAttributes, and metric measurements.

This follows the 20-level approach previously reviewed in #5565 and #5757. In particular, it adds the metrics regression coverage requested in #5757 and reapplies the fix onto current main.

Compatibility

Only values that previously caused RecursionError are affected. Valid values at the supported depth remain intact, and valid sibling attributes are preserved. A warning is emitted when a value is replaced with None.

Validation

  • Focused API/SDK suites: 97 passed, 16 subtests passed.
  • The 12 new regression tests all pass.
  • ruff format and ruff check on every changed Python file: passed.
  • pylint on every changed Python file: passed, 10.00/10.

The focused suites were:

python -m pytest opentelemetry-api/tests/attributes \
  opentelemetry-sdk/tests/test_attribute_nesting_depth.py \
  opentelemetry-sdk/tests/metrics/test_metrics.py -q -o addopts=""

🤖 Generated with Kimi

CLA

csw7777 has completed EasyCLA authorization; re-evaluation requested.

Bound attribute nesting at 20 levels and replace cyclic or deeper values
with None. Clean top-level attributes independently so invalid values do not
discard valid siblings in spans, resources, and metric measurements.

Add API, SDK, resource, and measurement regression tests.

Fixes open-telemetry#5564

Assisted-by: Kimi
Co-Authored-By: Kimi <noreply@anthropic.com>
@csw7777
csw7777 requested a review from a team as a code owner November 27, 2026 17:06
Assisted-by: Kimi
Co-Authored-By: Kimi <noreply@anthropic.com>
@linux-foundation-easycla

linux-foundation-easycla Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

CLA Not Signed

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Closed · refreshed 2026-11-29 05:33 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@givemehat givemehat left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch on isolating top-level attributes via _clean_attributes so sibling attributes are preserved!

One edge case to consider in _clean_attribute_value_impl: relying solely on depth > _MAX_NESTING_DEPTH (20) to stop cycles means a cyclic structure with a branching factor of 2+ (e.g., d = {}; d['a'] = d; d['b'] = d) will still traverse $O(2^{20})$ recursive calls before hitting the depth limit.

Tracking active ancestor container id(value)s in a seen: set[int] (adding before recursing into a Sequence/Mapping and removing in a finally block so non-cyclic shared references/DAGs remain allowed) would catch cycles immediately in $O(1)$ while keeping _MAX_NESTING_DEPTH for non-cyclic deep nesting.

Track active ancestor container IDs to avoid exponential traversal for
branching cycles while preserving non-cyclic shared references. Add
regression coverage for both cases.

Assisted-by: Kimi
Co-Authored-By: Kimi <noreply@anthropic.com>
@csw7777

csw7777 commented Nov 28, 2026

Copy link
Copy Markdown
Author

My account csw7777 is now authorized by EasyCLA. The remaining failure is for the AI co-author trailer Co-Authored-By: Kimi <noreply@anthropic.com>. Could a maintainer advise whether this co-author can be exempted, or how AI-assisted commits should be attributed while satisfying EasyCLA?

@csw7777

csw7777 commented Nov 28, 2026

Copy link
Copy Markdown
Author

Nice catch on isolating top-level attributes via _clean_attributes so sibling attributes are preserved!

One edge case to consider in _clean_attribute_value_impl: relying solely on depth > _MAX_NESTING_DEPTH (20) to stop cycles means a cyclic structure with a branching factor of 2+ (e.g., d = {}; d['a'] = d; d['b'] = d) will still traverse O ( 2 20 ) recursive calls before hitting the depth limit.

Tracking active ancestor container id(value)s in a seen: set[int] (adding before recursing into a Sequence/Mapping and removing in a finally block so non-cyclic shared references/DAGs remain allowed) would catch cycles immediately in O ( 1 ) while keeping _MAX_NESTING_DEPTH for non-cyclic deep nesting.

Thank you for pointing out the issue. It has been fixed in commit 71b7741: before traversing a Sequence/Mapping, we record the ID of each active ancestor and remove it in a finally block after the recursive return, and if a duplicated ancestor is found, we reject it immediately so that branch cycles will not expand exponentially. Non-cyclic shared references are still allowed, and we have added two sets of regression tests, one for branch cycles and one for shared DAGs. Please take another look when you have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Self-referential attribute value crashes the instrumented application with RecursionError

2 participants