Skip to content

Clear context class loader from started threads #7488

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

laurit
Copy link
Contributor

@laurit laurit commented Jul 14, 2025

Thread inherits context class loader from the thread that started it. This could be problematic when a thread is started from an application thread. For example with the StrictContextStorage (which is not used by default) some agent tests run on tomcat produce the following warning

Jul 14, 2025 4:08:02 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [app] appears to have started a thread named [weak-ref-cleaner-strictcontextstorage] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.base/jdk.internal.misc.Unsafe.park(Native Method)
 java.base/java.util.concurrent.locks.LockSupport.park(LockSupport.java:371)
 java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionNode.block(AbstractQueuedSynchronizer.java:519)
 java.base/java.util.concurrent.ForkJoinPool.unmanagedBlock(ForkJoinPool.java:3780)
 java.base/java.util.concurrent.ForkJoinPool.managedBlock(ForkJoinPool.java:3725)
 java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:1707)
 java.base/java.lang.ref.ReferenceQueue.await(ReferenceQueue.java:67)
 java.base/java.lang.ref.ReferenceQueue.remove0(ReferenceQueue.java:158)
 java.base/java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:234)
 io.opentelemetry.context.StrictContextStorage$PendingScopes.run(StrictContextStorage.java:270)
 java.base/java.lang.Thread.run(Thread.java:1583)

Basically this tells that even when you undeploy the application the resources can't be freed because there is thread that has a reference to the application class loader.

@laurit laurit requested a review from a team as a code owner July 14, 2025 13:25
Copy link

codecov bot commented Jul 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.00%. Comparing base (8009415) to head (41415e5).

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #7488   +/-   ##
=========================================
  Coverage     90.00%   90.00%           
  Complexity     7078     7078           
=========================================
  Files           803      803           
  Lines         21405    21408    +3     
  Branches       2086     2086           
=========================================
+ Hits          19265    19268    +3     
  Misses         1477     1477           
  Partials        663      663           

☔ View full report in Codecov by Sentry.
📢 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.

@jkwatson
Copy link
Contributor

this seems fine. are there any downsides to removing the context class loader, though? What is it generally used for?

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.

2 participants