Skip to content

[SDK] Use shared_ptr internally for AttributesProcessor to prevent use-after-free #3457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 3, 2025

Conversation

lalitb
Copy link
Member

@lalitb lalitb commented Jun 2, 2025

Fixes #3456

Changes

This PR changes metric storage and View class to use std::shared_ptr<const AttributesProcessor> internally, while the public View API continues to accept a std::unique_ptr. The unique pointer is promoted to a shared pointer inside the View constructor. The change prevents use-after-free when metrics are recorded after MeterProvider shutdown or destruction.

Each metric recording operation now accesses the AttributesProcessor via a std::shared_ptr instead of a raw pointer as before. This adds a minimal overhead due to shared pointer reference counting in recording hot-path, but ensures memory safety, so should be acceptable.

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

@lalitb lalitb requested a review from a team as a code owner June 2, 2025 19:02
Copy link

netlify bot commented Jun 2, 2025

Deploy Preview for opentelemetry-cpp-api-docs canceled.

Name Link
🔨 Latest commit 446fdfe
🔍 Latest deploy log https://app.netlify.com/projects/opentelemetry-cpp-api-docs/deploys/683e32b64c69b6000877f8dc

@ThomsonTan
Copy link
Contributor

Thanks for raising the PR. Could you add a dedicated test to validate the seg fault?

Copy link

codecov bot commented Jun 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.89%. Comparing base (05ac106) to head (446fdfe).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3457      +/-   ##
==========================================
+ Coverage   89.88%   89.89%   +0.02%     
==========================================
  Files         212      212              
  Lines        6941     6942       +1     
==========================================
+ Hits         6238     6240       +2     
+ Misses        703      702       -1     
Files with missing lines Coverage Δ
...ntelemetry/sdk/metrics/state/sync_metric_storage.h 88.58% <100.00%> (+0.34%) ⬆️
sdk/include/opentelemetry/sdk/metrics/view/view.h 100.00% <100.00%> (ø)
sdk/src/metrics/meter.cc 85.89% <100.00%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lalitb
Copy link
Member Author

lalitb commented Jun 2, 2025

Thanks for raising the PR. Could you add a dedicated test to validate the seg fault?

done

Copy link
Member

@marcalff marcalff left a comment

Choose a reason for hiding this comment

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

LGTM

@marcalff marcalff changed the title Fix: Use shared_ptr internally for AttributesProcessor to prevent use-after-free [SDK] Use shared_ptr internally for AttributesProcessor to prevent use-after-free Jun 3, 2025
@lalitb lalitb merged commit d2e7289 into open-telemetry:main Jun 3, 2025
67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Potential use-after-free of AttributesProcessor in metric storage after MeterProvider shutdown
3 participants