Skip to content

Bug fix: disposing meter with same name does not dispose the shared metric#7172

Open
sseelam2 wants to merge 2 commits into
open-telemetry:mainfrom
sseelam2:fix-dispose-meter
Open

Bug fix: disposing meter with same name does not dispose the shared metric#7172
sseelam2 wants to merge 2 commits into
open-telemetry:mainfrom
sseelam2:fix-dispose-meter

Conversation

@sseelam2
Copy link
Copy Markdown
Contributor

Fixes #6412

Changes

Added reference counting to Metric. AddInstrumentReference() is called when an instrument is attached; RemoveInstrumentReference() is called on completion and only triggers DeactivateMetric(Metric) when the count reaches zero. Both BuildForSingleMetric(Metric) and BuildForMetricList(List<Metric>) in MetricState are updated accordingly.

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

@sseelam2 sseelam2 requested a review from a team as a code owner April 26, 2026 01:56
@github-actions github-actions Bot added the pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package label Apr 26, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.62%. Comparing base (9b45df9) to head (9d00a05).
⚠️ Report is 81 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7172      +/-   ##
==========================================
+ Coverage   89.08%   89.62%   +0.54%     
==========================================
  Files         271      272       +1     
  Lines       13073    14130    +1057     
==========================================
+ Hits        11646    12664    +1018     
- Misses       1427     1466      +39     
Flag Coverage Δ
unittests-Project-Experimental 89.30% <100.00%> (+0.26%) ⬆️
unittests-Project-Stable 89.45% <100.00%> (+0.46%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/OpenTelemetry/Metrics/Metric.cs 100.00% <100.00%> (ø)
src/OpenTelemetry/Metrics/MetricState.cs 92.10% <100.00%> (+0.67%) ⬆️

... and 34 files with indirect coverage changes

Comment thread src/OpenTelemetry/Metrics/Metric.cs Outdated
Comment thread src/OpenTelemetry/Metrics/Metric.cs Outdated
Comment thread src/OpenTelemetry/Metrics/Metric.cs Outdated
Comment thread src/OpenTelemetry/Metrics/Metric.cs Outdated
Comment thread src/OpenTelemetry/Metrics/MetricState.cs Outdated
@cijothomas
Copy link
Copy Markdown
Member

Thanks for the PR! I want to understand whether this is actually a bug or expected behavior.

The OTel SDK identifies meters by (name, version, schema_url, attributes) — not by object instance. So when two new Meter("MyApp") calls produce the same identity, from the SDK's perspective there's only one Meter/Scope. When either instance is disposed, isn't it reasonable that the SDK treats it as "the Meter named MyApp is being torn down" and deactivates the associated Metric?

Should we consider the current behavior a bug?

@sseelam2
Copy link
Copy Markdown
Contributor Author

My understanding is that OpenTelemetry defines Meter identity using (name, version, schema_url, attributes) mainly for attribution. Does that also imply that all Meter instances with the same identity are expected to share the same lifetime?

I’m still getting familiar with the SDK, but in .NET Meter is IDisposable, and disposing one instance should ideally only affect the instruments created from that instance and not tear down the Metric if there are other Meter instances with the same identity still alive.

If disposing one Meter instance is meant to deactivate metrics for all meters with the same identity, that behavior should probably be explicit. Otherwise, we could consider preventing multiple meters from being created with the same identity.

@martincostello martincostello requested a review from cijothomas May 5, 2026 11:38
@github-actions
Copy link
Copy Markdown
Contributor

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions Bot added the Stale Issues and pull requests which have been flagged for closing due to inactivity label May 13, 2026
@martincostello martincostello added keep-open Prevents issues and pull requests being closed as stale and removed Stale Issues and pull requests which have been flagged for closing due to inactivity labels May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

keep-open Prevents issues and pull requests being closed as stale pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Disposing a Meter causes all Meters with the same name to stop working

3 participants