Skip to content

[OpenTelemetry] Release IMetricsListener refs on dispose - #7924

Merged
martincostello merged 2 commits into
open-telemetry:mainfrom
martincostello:gh-6447
Oct 13, 2026
Merged

martincostello merged 2 commits into
open-telemetry:mainfrom
martincostello:gh-6447

Conversation

@martincostello

Copy link
Copy Markdown
Member

Fixes #6447

Changes

Release the MeterProvider and IObservableInstrumentsSource references in OpenTelemetryMetricsListener.Dispose() instead of only unsubscribing from OnCollectObservableInstruments.

Versions of Microsoft.Extensions.Diagnostics prior to 9.0 never dispose MetricsSubscriptionManager (dotnet/runtime#94434), so the runtime keeps the listener rooted after the host is disposed. This kept the MeterProvider, and the IServiceProvider it references, alive.

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, successful build/tests, etc.)
  • Tests added/updated
  • CHANGELOG.md file(s) updated for non-trivial changes
  • Changes in public API reviewed (if applicable)
  • AI usage disclosure:
    • No AI used
    • AI-assisted
    • Bulk AI-generated
  • I have the experience and knowledge necessary to understand, review, and validate all content in this PR1

Footnotes

  1. Yes, I can answer maintainer questions about the content of this PR without using AI. ↩

Release the `MeterProvider` and `IObservableInstrumentsSource` references in `OpenTelemetryMetricsListener.Dispose()` instead of only unsubscribing from `OnCollectObservableInstruments`.

Versions of `Microsoft.Extensions.Diagnostics` prior to 9.0 never dispose `MetricsSubscriptionManager` (dotnet/runtime#94434), so the runtime keeps the listener rooted after the host is disposed. This kept the `MeterProvider`, and the `IServiceProvider` it references, alive.

Fixes open-telemetry#6447.
@martincostello martincostello added this to the v1.20.0 milestone Oct 13, 2026
@github-actions github-actions Bot added the pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package label Oct 13, 2026
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-10-13 14:39 UTC

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

@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.25%. Comparing base (62e03b7) to head (529155c).
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7924      +/-   ##
==========================================
- Coverage   92.26%   92.25%   -0.01%     
==========================================
  Files         363      363              
  Lines       20987    20989       +2     
==========================================
+ Hits        19363    19364       +1     
- Misses       1624     1625       +1     
Flag Coverage Δ
unittests-Project-Experimental 92.38% <100.00%> (-0.01%) ⬇️
unittests-Project-Stable 92.38% <100.00%> (+<0.01%) ⬆️

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

Files with missing lines Coverage Δ
...s/IMetricsListener/OpenTelemetryMetricsListener.cs 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The source-release behavior lacks direct verification, and the changelog misidentifies which runtime object remains undisposed.

2 open findings
What changed in this PR

Fixes retained MeterProvider references after host disposal.

Changes:

  • Clears listener-held provider and observable-source references.
  • Adds a garbage-collection regression test.
  • Documents the memory-leak fix.

[!NOTE]
This review was generated with GitHub Copilot.

File Description
OpenTelemetryMetricsListener.cs Releases retained references during disposal.
MeterProviderSdkTests.cs Tests provider collection and post-disposal behavior.
CHANGELOG.md Records the fix.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/OpenTelemetry/CHANGELOG.md Resolved
Address review feedback from GitHub Copilot.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused lifetime fix is consistent with the listener lifecycle and has appropriate regression coverage.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

@martincostello martincostello added the review-priority Should be prioritized for review label Oct 13, 2026
@martincostello
martincostello marked this pull request as ready for review October 13, 2026 13:05
@martincostello
martincostello requested a review from a team as a code owner October 13, 2026 13:05
This was referenced Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Memory leak in unittest

3 participants