Skip to content

Nested JAR loader race condition with concurrent jar file access #50925

Closed as not planned

Description

@janjanneck7777

We observe intermittent failures at application startup using this combination of dependencies:

  • Spring Boot 4.0.6
  • MongoDB Java driver 5.8.0
  • Elastic APM Java Agent 1.56.0

It manifests as a

NoClassDefFoundError: com/mongodb/internal/connection/InternalStreamConnection by all of the MongoDB monitor threads that initially try to establish a connection to our MongoDB Atlas cluster.

The same issue happens with Spring Boot 3.5.X and can be resolved by using the LoaderImplementation.CLASSIC. This workaround is no longer available starting with Boot 4. We also had success by

  • unpacking the affected driver JARs using Gradle (requiresUnpack("**/mongodb-driver-core-*.jar"))
  • disabling the APM agent entirely
  • disabling the mongodb instrumentation of the APM agent

An (AI generated) reproducer can be found here: https://github.com/janjanneck7777/springboot-loader-race-repro. Run it using ./capture-diagnostics.sh. On my machine the bug occurred in around 1 of 25 startups.

We are aware that the APM agent declares support for Boot 4 as experimental. That being said: The same (or at least very similar) bug occurred using Boot 3 with the new loader. We have no issues when using the classic loader, suggesting there might be something that could be handled better in the new loader.

See also: #43290

I attached the output of the reproducer to this ticket.

Main log output
[0.220s][info][jfr,startup] Started recording 1. No limit specified, using maxsize=250MB as default.
[0.220s][info][jfr,startup] 
[0.220s][info][jfr,startup] Use jcmd 10988 JFR.dump name=repro to copy recording data to file.
WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::arrayBaseOffset has been called by com.lmax.disruptor.RingBufferFields (file:/private/var/folders/9r/mrxrn12567v24xty0cwy8y000000gp/T/elastic-apm-agent-8c1369c2fd16ed4842b88ae2f442d336-4595ce678bfe3a3420ada95356bc85c2.jar)
WARNING: Please consider reporting this to the maintainers of class com.lmax.disruptor.RingBufferFields
WARNING: sun.misc.Unsafe::arrayBaseOffset will be removed in a future release
APM agent: attached via ElasticApmAttacher.attach()
Connecting MongoClient to mongodb://192.0.2.1:27017,192.0.2.2:27017,192.0.2.3:27017,192.0.2.4:27017,192.0.2.5:27017,192.0.2.6:27017,192.0.2.7:27017,192.0.2.8:27017/?replicaSet=rs0&serverSelectionTimeoutMS=1500&connectTimeoutMS=500&heartbeatFrequencyMS=100000 (hosts are intentionally unreachable)
59 [main] INFO org.mongodb.driver.cluster - Adding discovered server 192.0.2.1:27017 to client view of cluster
80 [main] INFO org.mongodb.driver.cluster - Adding discovered server 192.0.2.2:27017 to client view of cluster
80 [main] INFO org.mongodb.driver.cluster - Adding discovered server 192.0.2.3:27017 to client view of cluster
80 [main] INFO org.mongodb.driver.cluster - Adding discovered server 192.0.2.4:27017 to client view of cluster
80 [main] INFO org.mongodb.driver.cluster - Adding discovered server 192.0.2.5:27017 to client view of cluster
80 [main] INFO org.mongodb.driver.cluster - Adding discovered server 192.0.2.6:27017 to client view of cluster
81 [main] INFO org.mongodb.driver.cluster - Adding discovered server 192.0.2.7:27017 to client view of cluster
81 [main] INFO org.mongodb.driver.cluster - Adding discovered server 192.0.2.8:27017 to client view of cluster
101 [cluster-6a4c981ac59fd96c39728df3-192.0.2.1:27017] ERROR org.mongodb.driver.cluster - Thread[#56,cluster-6a4c981ac59fd96c39728df3-192.0.2.1:27017,5,main] for ServerId{clusterId=ClusterId{value='6a4c981ac59fd96c39728df3', description='null'}, address=192.0.2.1:27017} stopped working. You may want to recreate the MongoClient
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 java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:369)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:557)
	at org.springframework.boot.loader.net.protocol.jar.JarUrlClassLoader.loadClass(JarUrlClassLoader.java:107)
	at org.springframework.boot.loader.launch.LaunchedClassLoader.loadClass(LaunchedClassLoader.java:91)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:490)
	... 5 more
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(InflaterInputStream.java:308)
	at org.springframework.boot.loader.jar.ZipInflaterInputStream.fill(ZipInflaterInputStream.java:67)
	at java.base/java.util.zip.InflaterInputStream.read(InflaterInputStream.java:208)
	at org.springframework.boot.loader.jar.ZipInflaterInputStream.read(ZipInflaterInputStream.java:57)
	at org.springframework.boot.loader.net.protocol.jar.LazyDelegatingInputStream.read(LazyDelegatingInputStream.java:43)
	at java.base/jdk.internal.loader.Resource.getBytes(Resource.java:106)
	at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
	at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:367)
	... 9 more
101 [cluster-6a4c981ac59fd96c39728df3-192.0.2.3:27017] ERROR org.mongodb.driver.cluster - Thread[#58,cluster-6a4c981ac59fd96c39728df3-192.0.2.3:27017,5,main] for ServerId{clusterId=ClusterId{value='6a4c981ac59fd96c39728df3', description='null'}, address=192.0.2.3:27017} stopped working. You may want to recreate the MongoClient
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
	... 5 more
110 [main] INFO org.mongodb.driver.client - MongoClient with metadata {"driver": {"name": "mongo-java-driver|sync", "version": "5.8.0"}, "os": {"type": "Darwin", "name": "Mac OS X", "architecture": "aarch64", "version": "26.5.2"}, "platform": "Java/Amazon.com Inc./25.0.2+10-LTS"} created with settings MongoClientSettings{readPreference=primary, writeConcern=WriteConcern{w=null, wTimeout=null ms, journal=null}, retryWrites=true, retryReads=true, readConcern=ReadConcern{level=null}, credential=null, transportSettings=null, commandListeners=[], codecRegistry=ProvidersCodecRegistry{codecProviders=[ValueCodecProvider{}, BsonValueCodecProvider{}, DBRefCodecProvider{}, DBObjectCodecProvider{}, DocumentCodecProvider{}, CollectionCodecProvider{}, IterableCodecProvider{}, MapCodecProvider{}, GeoJsonCodecProvider{}, GridFSFileCodecProvider{}, Jsr310CodecProvider{}, JsonObjectCodecProvider{}, BsonCodecProvider{}, com.mongodb.client.model.mql.ExpressionCodecProvider@3234f74e, com.mongodb.Jep395RecordCodecProvider@7bc10d84, com.mongodb.KotlinCodecProvider@275fe372, EnumCodecProvider{}]}, loggerSettings=LoggerSettings{maxDocumentLength=1000}, clusterSettings={hosts=[192.0.2.1:27017, 192.0.2.2:27017, 192.0.2.3:27017, 192.0.2.4:27017, 192.0.2.5:27017, 192.0.2.6:27017, 192.0.2.7:27017, 192.0.2.8:27017], srvServiceName=mongodb, mode=MULTIPLE, requiredClusterType=REPLICA_SET, requiredReplicaSetName='rs0', serverSelector='null', clusterListeners='[]', serverSelectionTimeout='1500 ms', localThreshold='15 ms'}, socketSettings=SocketSettings{connectTimeoutMS=500, readTimeoutMS=0, receiveBufferSize=0, proxySettings=ProxySettings{host=null, port=null, username=null, password=null}}, heartbeatSocketSettings=SocketSettings{connectTimeoutMS=500, readTimeoutMS=500, receiveBufferSize=0, proxySettings=ProxySettings{host=null, port=null, username=null, password=null}}, connectionPoolSettings=ConnectionPoolSettings{maxSize=100, minSize=0, maxWaitTimeMS=120000, maxConnectionLifeTimeMS=0, maxConnectionIdleTimeMS=0, maintenanceInitialDelayMS=0, maintenanceFrequencyMS=60000, connectionPoolListeners=[], maxConnecting=2}, serverSettings=ServerSettings{heartbeatFrequencyMS=100000, minHeartbeatFrequencyMS=500, serverMonitoringMode=AUTO, serverListeners='[]', serverMonitorListeners='[]'}, sslSettings=SslSettings{enabled=false, invalidHostNameAllowed=false, context=null}, applicationName='null', compressorList=[], uuidRepresentation=UNSPECIFIED, serverApi=null, autoEncryptionSettings=null, dnsClient=null, inetAddressResolver=null, contextProvider=null, timeoutMS=null}
Exception in thread "cluster-6a4c981ac59fd96c39728df3-192.0.2.1:27017" 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 java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:369)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:557)
	at org.springframework.boot.loader.net.protocol.jar.JarUrlClassLoader.loadClass(JarUrlClassLoader.java:107)
	at org.springframework.boot.loader.launch.LaunchedClassLoader.loadClass(LaunchedClassLoader.java:91)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:490)
	... 5 more
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(InflaterInputStream.java:308)
	at org.springframework.boot.loader.jar.ZipInflaterInputStream.fill(ZipInflaterInputStream.java:67)
	at java.base/java.util.zip.InflaterInputStream.read(InflaterInputStream.java:208)
	at org.springframework.boot.loader.jar.ZipInflaterInputStream.read(ZipInflaterInputStream.java:57)
	at org.springframework.boot.loader.net.protocol.jar.LazyDelegatingInputStream.read(LazyDelegatingInputStream.java:43)
	at java.base/jdk.internal.loader.Resource.getBytes(Resource.java:106)
	at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
	at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:367)
	... 9 more
Exception in thread "cluster-6a4c981ac59fd96c39728df3-192.0.2.3:27017" 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
	... 5 more
118 [cluster-6a4c981ac59fd96c39728df3-192.0.2.2:27017] ERROR org.mongodb.driver.cluster - Thread[#57,cluster-6a4c981ac59fd96c39728df3-192.0.2.2:27017,5,main] for ServerId{clusterId=ClusterId{value='6a4c981ac59fd96c39728df3', description='null'}, address=192.0.2.2:27017} stopped working. You may want to recreate the MongoClient
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
	... 5 more
Exception in thread "cluster-6a4c981ac59fd96c39728df3-192.0.2.2:27017" 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
	... 5 more
128 [cluster-6a4c981ac59fd96c39728df3-192.0.2.4:27017] ERROR org.mongodb.driver.cluster - Thread[#59,cluster-6a4c981ac59fd96c39728df3-192.0.2.4:27017,5,main] for ServerId{clusterId=ClusterId{value='6a4c981ac59fd96c39728df3', description='null'}, address=192.0.2.4:27017} stopped working. You may want to recreate the MongoClient
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
	... 5 more
Exception in thread "cluster-6a4c981ac59fd96c39728df3-192.0.2.4:27017" 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
	... 5 more
RACE_REPRODUCED: 'NoClassDefFoundError' observed in a cluster-monitor thread
DIAGNOSTIC_REPORT: /var/folders/9r/mrxrn12567v24xty0cwy8y000000gp/T/tmp.6shAETnkLX/diagnostics/hit-20260707-080930-316-pid10988
135 [cluster-6a4c981ac59fd96c39728df3-192.0.2.5:27017] ERROR org.mongodb.driver.cluster - Thread[#60,cluster-6a4c981ac59fd96c39728df3-192.0.2.5:27017,5,main] for ServerId{clusterId=ClusterId{value='6a4c981ac59fd96c39728df3', description='null'}, address=192.0.2.5:27017} stopped working. You may want to recreate the MongoClient
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
	... 5 more
Exception in thread "cluster-6a4c981ac59fd96c39728df3-192.0.2.5:27017" 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
	... 5 more
142 [cluster-6a4c981ac59fd96c39728df3-192.0.2.6:27017] ERROR org.mongodb.driver.cluster - Thread[#61,cluster-6a4c981ac59fd96c39728df3-192.0.2.6:27017,5,main] for ServerId{clusterId=ClusterId{value='6a4c981ac59fd96c39728df3', description='null'}, address=192.0.2.6:27017} stopped working. You may want to recreate the MongoClient
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
	... 5 more
Exception in thread "cluster-6a4c981ac59fd96c39728df3-192.0.2.6:27017" 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
	... 5 more
148 [cluster-6a4c981ac59fd96c39728df3-192.0.2.7:27017] ERROR org.mongodb.driver.cluster - Thread[#62,cluster-6a4c981ac59fd96c39728df3-192.0.2.7:27017,5,main] for ServerId{clusterId=ClusterId{value='6a4c981ac59fd96c39728df3', description='null'}, address=192.0.2.7:27017} stopped working. You may want to recreate the MongoClient
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
	... 5 more
Exception in thread "cluster-6a4c981ac59fd96c39728df3-192.0.2.7:27017" 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
	... 5 more
155 [cluster-6a4c981ac59fd96c39728df3-192.0.2.8:27017] ERROR org.mongodb.driver.cluster - Thread[#63,cluster-6a4c981ac59fd96c39728df3-192.0.2.8:27017,5,main] for ServerId{clusterId=ClusterId{value='6a4c981ac59fd96c39728df3', description='null'}, address=192.0.2.8:27017} stopped working. You may want to recreate the MongoClient
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
	... 5 more
Exception in thread "cluster-6a4c981ac59fd96c39728df3-192.0.2.8:27017" 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
	... 5 more

jvm.log
stderr.log
stdout-stderr.log
threaddump-at-failure.txt

Activity

  1. added
    status: waiting-for-triage
    on Jul 7, 2026
  2. arnabnandy7 commented on Jul 18, 2026

    @arnabnandy7

    I tried the supplied reproducer from https://github.com/janjanneck7777/springboot-loader-race-repro and was unable to reproduce the failure in the environments available to me.

    Result

    I ran 800 fresh JVM launches in total with the Elastic APM agent enabled and hosts=16 to increase contention:

    OS/runtime Architecture / hardware JDK Attempts Hits
    Windows 11 Pro 64-bit, build 10.0.26200 x86_64, Intel Core i5-1335U Oracle JDK 26.0.1+8-34, HotSpot 64-bit 400 0
    Ubuntu 24.04.4 LTS under WSL2, Linux 6.18.33.2-microsoft-standard-WSL2 x86_64, same Intel host OpenJDK 17.0.19+10, 64-bit Server VM 400 0

    Neither arm produced RACE_REPRODUCED, NoClassDefFoundError, nor ZipFile closed.

    Both runs were x86_64. I did not have an ARM/aarch64 environment available, so this does not cover the reporter's macOS/aarch64/JDK 25 combination.

    Versions from the reproducer

    • Spring Boot 4.0.6
    • MongoDB Java driver 5.8.0
    • Elastic APM Java Agent 1.56.0
    • Buggy fat JAR (no requiresUnpack)
    • Elastic APM attachment enabled
    • 16 intentionally unreachable MongoDB hosts per launch

    Build

    On Windows:

    git clone --depth 1 https://github.com/janjanneck7777/springboot-loader-race-repro.git
    cd springboot-loader-race-repro
    .\gradlew.bat --quiet clean bootJar

    This produced:

    build/libs/loader-race-repro-buggy.jar
    

    Windows stress run

    The test stopped on the first matching failure marker; otherwise it completed 400 attempts:

    $java = Join-Path $env:JAVA_HOME 'bin\java.exe'
    $jar = (Resolve-Path 'build\libs\loader-race-repro-buggy.jar').Path
    
    for ($i = 1; $i -le 400; $i++) {
        $output = & $java `
            -XX:+EnableDynamicAgentLoading `
            -Dhosts=16 `
            -Dreport.dir=diagnostics `
            -jar $jar 2>&1 | Out-String
    
        if ($output -match 'RACE_REPRODUCED|ZipFile closed|NoClassDefFoundError') {
            "REPRODUCED_ITERATION=$i"
            $output
            break
        }
    }

    Result: 0 / 400.

    WSL2/Linux stress run

    I reused the same built fat JAR and launched it using the Linux JVM, once per iteration:

    jar=build/libs/loader-race-repro-buggy.jar
    for i in $(seq 1 400); do
      out=$(java \
        -XX:+EnableDynamicAgentLoading \
        -Dhosts=16 \
        -jar "$jar" 2>&1)
    
      if echo "$out" | grep -Eq \
          "RACE_REPRODUCED|ZipFile closed|NoClassDefFoundError"; then
        echo "REPRODUCED_ITERATION=$i"
        echo "$out"
        exit 0
      fi
    done
    
    echo "NO_REPRODUCTION_IN_400"

    Result: 0 / 400.

    This outcome is consistent with the issue being timing-, scheduler-, JVM-, or platform-sensitive; it does not invalidate the macOS/aarch64 reproduction reported above. A useful next comparison would be native macOS/aarch64 with JDK 25, followed by a cold/constrained native Linux environment rather than WSL2.

  3. wilkinsona commented on Jul 19, 2026

    @wilkinsona
    Member

    Thank you, @janjanneck7777. I've reproduced the problem on macOS:

    java.version        : 25.0.2
    java.vendor         : BellSoft
    java.vm.name        : OpenJDK 64-Bit Server VM
    os.name/arch        : Mac OS X / aarch64
    available processors: 10
    apm agent attached  : true
    

    We've got a lot of other, unfortunately higher priority, work on our plates at the moment, but I wanted to at least acknowledge that we can now reproduce the problem. Thank you.

    The question that I think we really need to answer if what's closing the NestedJarFile or JarEntryInputStream on one thread such that another thread ends up trying to use a closed instance. I haven't been able to find that answer in the diagnostics, but I also haven't had a huge amount of time to look. Regardless, being able to reproduce the problem is a significant step forwards.

  4. amarcalfaria commented on Aug 6, 2026

    @amarcalfaria

    Possibly a sibling of this in the same subsystem, with a different symptom: instead of ZipException: ZipFile closed, the application silently hangs forever.

    Environment: Spring Boot 4.1.0, OpenJDK 25.0.2+10-LTS, fat jar, embedded Tomcat with virtual threads, 4 vCPU.

    Symptom: process stays alive and keeps completing TCP handshakes, but never completes another HTTP response. Nothing thrown, nothing logged, never recovers. Hits ~1.5-3% of otherwise identical fresh JVMs, always in the first seconds of traffic while classes are still loading for the first time.

    Chain

    Identical on five hosts:

    request threads (virtual) + MessageBroker-N
          | blocked on the UrlNestedJarFile monitor
          v
    clientInboundChannel-N / clientOutboundChannel-N   <- platform thread, HOLDS it
          | blocked on the FileDataBlock$FileAccess lock
          v
          ??? no owner
    

    The jar monitor holder is always a message-broker channel thread:

    FileDataBlock$FileAccess.ensureOpen(FileDataBlock.java:282)
    - waiting to lock <0x...> (a java.lang.Object)
    FileDataBlock.ensureOpen(FileDataBlock.java:117)
    FileDataBlock.read(FileDataBlock.java:73)
    DataBlock.readFully(DataBlock.java:64)
    ZipCentralDirectoryFileHeaderRecord.load(ZipCentralDirectoryFileHeaderRecord.java:198)
    ZipContent.loadZipCentralDirectoryFileHeaderRecord(ZipContent.java:259)
    ZipContent.hasEntry(ZipContent.java:235)
    NestedJarFile.hasEntry(NestedJarFile.java:253)
    - locked <0x...> (a UrlNestedJarFile)
    JarUrlConnection.hasEntry / open, Handler.openConnection, URL.openConnection,
    URLClassPath.getResource, URLClassLoader.findClass, ClassLoader.loadClass,
    JarUrlClassLoader.loadClass(:107), LaunchedClassLoader.loadClass(:91)
    DefaultSubscriptionRegistry.addSubscriptionInternal(DefaultSubscriptionRegistry.java:161)
    ... broker frames ...
    

    The request threads sit one level up at NestedJarFile.hasEntry(NestedJarFile.java:251), waiting for the monitor that thread holds.

    The FileAccess lock has no owner

    kill -3 cannot see unmounted virtual threads, so we assumed the owner was merely invisible. It is not. A jcmd Thread.dump_to_file -format=json dump shows 93 threads, 52 virtual, and the lock object appears exactly once in the whole file, as the blockedOn of the waiting platform thread. It is in no monitorsOwned array on any thread. Confirmed on two hosts, three dumps each, five seconds apart. No "Found one Java-level deadlock" is printed.

    Ruled out:

    • Not a refcount close. Two descriptors still open on the jar, so close() never took the count to zero.
    • Not the interrupt path. No fillBufferUsingRandomAccessFile / repairFileChannel frames, no ClosedByInterruptException.
    • Not carrier starvation, though it is present: parallelism = 4, size = 4, active = 4, running = 0, 43 queued. The head of the chain is a platform thread, so freeing carriers cannot release it. Raising jdk.virtualThreadScheduler.parallelism changes nothing.

    Repro condition

    Only reproduces where the jar is cold in page cache: fincore shows ~42% resident on affected machines versus 100% where the jar stays warm, and it has never reproduced warm. FileAccess.read holds its lock across fillBuffer and therefore across a real FileChannel.read, so a cold cache turns that critical section into disk I/O and widens the window. Cold cache alone is not the trigger though: affected and unaffected machines from the same image show the same residency (42.1 / 42.3 versus 43.2).

    Question

    Separate issue, or same defect as the ZipException here? Same subsystem, same JDK build family, both concurrency-dependent, but a different outcome. Happy to attach the full JSON thread dumps.

  5. wilkinsona commented on Aug 17, 2026

    @wilkinsona
    Member

    @amarcalfaria unless you're using the Elastic APM agent, that's a different problem. Even if you are using it, the symptoms are so different, it's almost certainly a different problem. In terms of symptoms, #51379 sounds similar but the circumstances are still quite different. Please create a minimal reproducer and then open a new issue and we'll take things from there.

  6. wilkinsona commented on Aug 17, 2026

    @wilkinsona
    Member

    elastic/apm-agent-java#4541 identifies the root cause of the problem. Thank you, @aayushsrivastava.

    The Elastic APM agent was using a JarURLConnection with caching enabled, retrieving the cached and shared jar file and then closing it unconditionally. This has been fixed in elastic/apm-agent-java#4543. Anyone affected by this issue should update to a new release of the Elastic APM agent once available. In the meantime, disabling the agent is the only known workaround.

  7. added
    status: invalid
    for: external-project
    and removed
    status: waiting-for-triage
    on Aug 17, 2026
  8. SylvainJuge commented on Sep 8, 2026

    @SylvainJuge

    The elastic/apm-agent-java release 1.57.0 is now available with a fix to prevent this issue from happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: external-projectstatus: invalid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions