Skip to content

Don't retain the context class loader in metric callback registrations - #9045

Open
trask wants to merge 4 commits into
open-telemetry:mainfrom
trask:trask-sdk-metric-callback-fix
Open

trask wants to merge 4 commits into
open-telemetry:mainfrom
trask:trask-sdk-metric-callback-fix

Conversation

@trask

@trask trask commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

A metric callback registration no longer keeps the registering thread's context class loader alive. Before this change, an undeployed web app could stay in memory for the life of the JVM.

For example, under the Java agent:

  1. shop.war registers a gauge during startup and never closes it.
  2. The agent's SDK holds the callback weakly. A map owned by shop.war's class loader keeps the callback alive, so it should go away with the app.
  3. The registration also strongly holds the context class loader, which is shop.war's loader during startup.
SDK → callback registration → shop.war class loader → map → callback

Neither the loader nor the callback is ever collected, so every redeploy leaks another copy of the app.

If the loader has been collected, the callback now runs with a null context class loader, as it did on the default periodic reader before #8091. The SDK still holds live callbacks strongly until they're closed.

@opentelemetry-pr-dashboard

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

Copy link
Copy Markdown

Pull request dashboard status

Waiting on maintainers · refreshed 2026-11-23 13:22 UTC

Merge when ready.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

Copilot AI 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.

🟢 Approval recommended

No active review issues remain for the callback and class-loader retention changes.

No open issues 🎉

What changed in this PR

Reduces callback and class-loader retention in the metrics SDK. Live registrations still retain callbacks, and collections that already captured a registration may invoke it after closure.

Changes:

  • Clears registration references when observable instrument handles close.
  • Holds the registering thread’s context class loader weakly, using null if it is unavailable.
  • Adds garbage-collection regression tests for callback release and class-loader fallback and restoration.
File Description
sdk/​metrics/​src/​test/​java/​io/​opentelemetry/​sdk/​metrics/​SdkObservableInstrumentTest.java Verifies callback release after closure and updates repeated-close log assertions.
sdk/​metrics/​src/​test/​java/​io/​opentelemetry/​sdk/​metrics/​internal/​state/​CallbackRegistrationTest.java Tests absent and collected registration loaders, including collection-thread loader restoration.
sdk/​metrics/​src/​main/​java/​io/​opentelemetry/​sdk/​metrics/​SdkObservableInstrument.java Atomically clears registration references on close and updates closed-state diagnostics.
sdk/​metrics/​src/​main/​java/​io/​opentelemetry/​sdk/​metrics/​internal/​state/​CallbackRegistration.java Replaces strong context-class-loader retention with a weak reference.

Review effort: Balanced

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.45%. Comparing base (f67bf22) to head (b8ef8ab).

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #9045   +/-   ##
=========================================
  Coverage     90.45%   90.45%           
  Complexity    11181    11181           
=========================================
  Files          1032     1032           
  Lines         30507    30507           
  Branches       3992     3992           
=========================================
  Hits          27595    27595           
  Misses         1935     1935           
  Partials        977      977           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

@trask trask changed the title Fix SDK metric callback and class loader retention Don't retain the context class loader in metric callback registrations Nov 20, 2026

This branch has not been deployed

No deployments
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.

3 participants