Skip to content

Remove static ThreadLocal caching in XmlUtils to prevent ClassLoader leaks#2516

Merged
jbonofre merged 1 commit intoapache:mainfrom
jbonofre:fix/xmlutils-threadlocal-classloader-leak
Apr 15, 2026
Merged

Remove static ThreadLocal caching in XmlUtils to prevent ClassLoader leaks#2516
jbonofre merged 1 commit intoapache:mainfrom
jbonofre:fix/xmlutils-threadlocal-classloader-leak

Conversation

@jbonofre
Copy link
Copy Markdown
Member

@jbonofre jbonofre commented Apr 5, 2026

The static ThreadLocal fields holding DocumentBuilderFactory, TransformerFactory and SAXParserFactory pin the ClassLoader of the XML provider bundle on long-lived Karaf worker threads. When that bundle is updated or refreshed, the old ClassLoader can never be GC'd, causing zombie bundles and Metaspace growth.

This patch removes the ThreadLocal caching and creates fresh factory instances per call. The public API is unchanged.

Fixes #2278

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 5, 2026

Test Results

233 files  ±0  233 suites  ±0   21m 54s ⏱️ +2s
917 tests ±0  869 ✅ ±0  48 💤 ±0  0 ❌ ±0 
921 runs  ±0  873 ✅ ±0  48 💤 ±0  0 ❌ ±0 

Results for commit 9160869. ± Comparison against base commit 031d9a1.

…leaks

The static ThreadLocal fields holding DocumentBuilderFactory, TransformerFactory
and SAXParserFactory pin the ClassLoader of the XML provider bundle on long-lived
Karaf worker threads. When that bundle is updated or refreshed, the old
ClassLoader can never be GC'd, leading to zombie bundles and Metaspace growth.

Create fresh factory instances per call instead of caching them. The public API
is unchanged.

Fixes apache#2278
Copy link
Copy Markdown
Contributor

@holgerfriedrich holgerfriedrich left a comment

Choose a reason for hiding this comment

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

LGTM

@jbonofre jbonofre merged commit 7a3f8f8 into apache:main Apr 15, 2026
44 of 54 checks passed
@jbonofre jbonofre deleted the fix/xmlutils-threadlocal-classloader-leak branch April 15, 2026 16:57
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.

ClassLoader leak and "Zombie Bundles" caused by static ThreadLocals in XmlUtils

2 participants