Repository navigation
Description
Describe the bug
co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers#readImplementationVersion reads a library's version by opening the jar URL from the class's ProtectionDomain and, when the connection is a JarURLConnection, calling getJarFile():
URLConnection urlConnection = jarUrl.openConnection();
if (urlConnection instanceof JarURLConnection) {
jarFile = ((JarURLConnection) urlConnection).getJarFile();
}
...
} finally {
if (jarFile != null) {
jarFile.close();
}
}The connection's caching is never disabled, so per the JarURLConnection contract the returned JarFile is a shared cached instance that the caller must not close. The agent closes it unconditionally in the finally block. Present in at least 1.55.1 and 1.56.0.
On a Spring Boot executable (packed) jar this is severe: Boot's nested: protocol returns its shared cached NestedJarFile for the dependency jar, and that single instance also backs classloading. Closing it fails every in-flight read with java.util.zip.ZipException: ZipFile closed and every in-flight classload with NoClassDefFoundError.
Real-world impact we observed across 10 production Spring Boot 4.1.0 services: the version check runs when the first MongoDB driver class is transformed (version-gated Mongo instrumentation). If one of the driver's DefaultServerMonitor threads is concurrently loading com.mongodb.internal.connection.InternalStreamConnection from the same jar, the monitor threads die, the client never leaves CONNECTING, context initialisation fails with MongoTimeoutException, and the container exits 1. It is timing-dependent, so it presents as an intermittent startup crash-loop that heals on restart.
This appears to be the root cause of the symptom reported in spring-projects/spring-boot#50925 (same exception, same driver class, same agent).
Steps to reproduce
- Build a Spring Boot 4.1.0 executable jar containing
org.mongodb:mongodb-driver-sync:5.8.0, with a main that creates aMongoClientagainst unreachable replica-set hosts (monitor threads spawn regardless). - Run it with the APM agent attached (
-javaagent:elastic-apm-agent.jar, any config; we useddisable_send=true,central_config=false). - Observe the close. We instrumented
org.springframework.boot.loader.jar.NestedJarFile#closewith a watch agent: in 10 out of 10 boots, the shared cachedNestedJarFileformongodb-driver-core-5.8.0.jar(opened bymainduring client creation) was closed by the APM matcher. Stack below. Without the APM agent attached: zero closes of shared instances across all runs. - The visible crash (
NoClassDefFoundErroronInternalStreamConnection, thenMongoTimeoutException) occurs only when a monitor-thread classload overlaps the close, so it reproduces intermittently; the erroneous close itself reproduces deterministically every boot.
Environment: JDK 25.0.3, Spring Boot 4.1.0 (loader sources identical back to 3.5.15, so all Boot versions with the current loader are exposed), mongodb-driver 5.8.0, agent 1.55.1 and 1.56.0.
Expected behavior
The agent must not close a JarFile obtained from a caching JarURLConnection. Either:
JarURLConnection jarURLConnection = (JarURLConnection) urlConnection;
jarFile = jarURLConnection.getJarFile();
closeJarFile = !jarURLConnection.getUseCaches();and close only when closeJarFile is true, or call urlConnection.setUseCaches(false) before connecting so the connection owns a private instance that is safe to close. The new JarFile(file) branch is unaffected and should still be closed.
Happy to open a PR with this change.
Debug logs
Agent debug logs do not surface the close (it succeeds silently), so instead: the closing stack captured by instrumentation, and the production victim stack.
Click to expand
Closing stack (captured via instrumentation of NestedJarFile#close, 10/10 boots):
org.springframework.boot.loader.jar.NestedJarFile.close(NestedJarFile.java:392)
org.springframework.boot.loader.net.protocol.jar.UrlNestedJarFile.close(UrlNestedJarFile.java:62)
co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers.readImplementationVersion(CustomElementMatchers.java:296)
co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers.access$300(CustomElementMatchers.java:50)
co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers$5.matches(CustomElementMatchers.java:217)
co.elastic.apm.agent.sdk.bytebuddy.CustomElementMatchers$5.matches(CustomElementMatchers.java:204)
co.elastic.apm.agent.bci.ElasticApmAgent$5.matches(ElasticApmAgent.java:409)
net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.doTransform(AgentBuilder.java:12801)
...
Victim (production, Mongo driver monitor thread, same boot):
java.lang.NoClassDefFoundError: com/mongodb/internal/connection/InternalStreamConnection
at com.mongodb.internal.connection.InternalStreamConnectionFactory.create(InternalStreamConnectionFactory.java:77)
at com.mongodb.internal.connection.InternalConnectionFactory.create(InternalConnectionFactory.java:27)
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitor.setupNewConnectionAndGetInitialDescription(DefaultServerMonitor.java:278)
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitor.lookupServerDescription(DefaultServerMonitor.java:253)
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitor.run(DefaultServerMonitor.java:203)
Caused by: java.lang.ClassNotFoundException: com.mongodb.internal.connection.InternalStreamConnection
at org.springframework.boot.loader.net.protocol.jar.JarUrlClassLoader.loadClass(JarUrlClassLoader.java:107)
at org.springframework.boot.loader.launch.LaunchedClassLoader.loadClass(LaunchedClassLoader.java:91)
Caused by: java.util.zip.ZipException: ZipFile closed
at org.springframework.boot.loader.jar.NestedJarFile$JarEntryInputStream.ensureOpen(NestedJarFile.java:761)
at org.springframework.boot.loader.jar.NestedJarFile$JarEntryInputStream.read(NestedJarFile.java:722)
at java.base/java.util.zip.InflaterInputStream.fill(Unknown Source)
at org.springframework.boot.loader.jar.ZipInflaterInputStream.fill(ZipInflaterInputStream.java:67)
at java.base/jdk.internal.loader.Resource.getBytes(Unknown Source)
at java.base/java.net.URLClassLoader.defineClass(Unknown Source)
Activity
SylvainJuge commentedon Aug 14, 2026
I think it would be simpler to just disable cache when opening the jar in this particular case, I've opened #4543 to fix it.
aayushsrivastava commentedon Aug 18, 2026
Thanks for the fix @SylvainJuge!
When should we expect the next release for apm-agent-java?
Need this so that we can plan our own mitigation.
jackshirazi commentedon Aug 24, 2026
targeting this week
SylvainJuge commentedon Sep 8, 2026
The release 1.57.0 is now available.