Skip to content

Class loading fails with 'Zip file closed' while ClearCachesApplicationListener closes the jars of an executable jar - #52635

Merged
philwebb merged 3 commits into
spring-projects:mainfrom
codeconsole:fix/clear-cache-keeps-jars-open
Nov 13, 2026
Merged

philwebb merged 3 commits into
spring-projects:mainfrom
codeconsole:fix/clear-cache-keeps-jars-open

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

Fixes the failure in #52634 by leaving the class loader's jar files open in clearCache().

clearCache() has closed them since the nested jar rewrite in 3.2, but the same rewrite moved the method to JarUrlClassLoader, where ClearCachesApplicationListener could not find it, so the closing did not run until #52582. Before the rewrite, clearCache() did not close them. The failure also needs #52246, which has findClass look through these same jar files directly, and it is only on main: the reproduction in #52634 did not fail in 20 runs on each of 4.1.3-SNAPSHOT and 4.0.10-SNAPSHOT.

Swapping the map before closing would not be enough, since findClass holds a jar file across getJarEntry and defineClass. Handling it in the lookups would mean a retry or a lock on each of findClass, findResource(s) and definePackage.

Closing them also gains little, since the next lookup opens them all again. On a class path of 151 nested jars, looking up a missing class on 4.2.0-RC2, with the same change applied to JarUrlClassLoader in the executable jar:

clearCache() First lookup afterwards Any other lookup Heap after GC Reproduction in #52634
Closes them, as now 1,317 µs 52 µs 3 MB Thread dead in 10 of 10 runs
Leaves them open 79 µs 50 µs 3 MB Thread dead in 0 of 20 runs

The Handler caches, the NestedLocation caches and the per-URL clearCache(URL) calls still run. What this does not do is release the file handles of these jar files, which #52582 lists as something clearCache() should do. They stay open as they have in every release since 3.2, until close().

The test fails in 8 of 8 runs without the change and passes in 20 of 20 with it. It uses 100 nested jars, since the window is the time spent closing them while the map still holds them. It opens them all first, because @AssertFileChannelDataBlocksClosed's tracker uses unsynchronized collections and jars opened on the second thread made it fail intermittently.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. label Nov 6, 2026
@philwebb philwebb changed the title Keep jar files open when clearing JarUrlClassLoader's caches Class loading fails with 'Zip file closed' while ClearCachesApplicationListener closes the jars of an executable jar Nov 13, 2026
@philwebb philwebb added type: regression A regression from a previous release for: merge-with-amendments Needs some changes when we merge labels Nov 13, 2026
@philwebb philwebb self-assigned this Nov 13, 2026
@philwebb philwebb removed the status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. label Nov 13, 2026
@philwebb philwebb added this to the 4.2.0 milestone Nov 13, 2026
codeconsole and others added 3 commits November 12, 2026 18:20
Add a test to `JarUrlClassLoader` to check that calling `clearCache`
at the same time that classes are being loaded in a different thread
does not result in a "Zip file closed" exception.

See spring-projectsgh-52635

Signed-off-by: Scott Murphy Heiberg <scott@alwaysvip.com>
Replace `jarFiles` cache with `connections` and use reference counting
to ensure that calls to `clearCache()` only close jar files that are
not in use.

Prior to this commit, calls to `clearCache()` would close jar files
before clearing the Map. This would sometimes result in a "zip closed"
exception being thrown if another thread is calling a classloader
operation that is using the `JarFile`.

Earlier versions of Spring Boot do not seem as susceptible to the
problem since they do not include the `NestedOnlyOptimizedLoader`
which was added in commit 8b5c1e7. The means that `definePackage`
is the only method directly using the `JarFile`.

Spring Boot 4.2.0-RC1 was also not susceptible since commit 107ba4b
wasn't fixed which meant no calls to `clearCache()` were happening.

See spring-projectsgh-52635
@philwebb
philwebb force-pushed the fix/clear-cache-keeps-jars-open branch from bde0371 to 47c5978 Compare November 13, 2026 02:40
@philwebb
philwebb merged commit 1fc56dc into spring-projects:main Nov 13, 2026
1 check failed
@philwebb

Copy link
Copy Markdown
Member

Thanks for the pull-request @codeconsole, and for the detailed write-up in #52634. The test from this pull-request is super useful, but I think I want to try a different approach for the fix (at least in 4.2.x.). I think we can use reference counting to track when the jars can actually be closed. I've merged this PR into 4.2, but only taking the test. I've then tried the reference counting approach in commit 47c5978.

Ideally I'd like to also apply this fix to earlier versions of Spring Boot, but since threading issues are notoriously hard to get right I've started by just removing the clearJarFiles() calls as you've suggested here. That's been done in #52797. Once 4.2 has been battle tested a bit more we can consider backporting the fix.

We're starting the release process for Spring Boot 4.2.0 early next week. If you get a chance to try the latest SNAPSHOT on your real application I'd appreciate it.

Thanks!

@codeconsole

Copy link
Copy Markdown
Contributor Author

@philwebb Thanks! I tried the latest snapshot (4.2.0-20261113.041558-360, from 7af7929) on our application, and it fixes it.

I built the same application twice from the same commit: a Grails app on Tomcat with actuator, 135 nested jars, run with java -jar. One build used 4.2.0-RC2 and the other the snapshot's BOM, Gradle plugin and loader. I started each 30 times with a client sending requests to the port throughout startup:

Spring Boot Starts Logged Zip file closed Never answered
4.2.0-RC2 30 22 1
4.2.0-SNAPSHOT (041558-360) 30 0 0

With the snapshot, every start answered 200 on / and /actuator/health, with no errors or exceptions in the log.

The RC2 start that never answered shows how bad the failure can be. Tomcat's acceptor thread failed to initialize SocketWrapperBase during the window:

Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.BootstrapMethodError: java.lang.IllegalStateException: Zip file closed [in thread "http-nio-18091-Acceptor"]
	at org.apache.tomcat.util.net.SocketWrapperBase.<clinit>(SocketWrapperBase.java:1138)

The JVM doesn't retry a failed class initialization, so every later connection failed the same way, 3,378 times, until the process was stopped.

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

Labels

for: merge-with-amendments Needs some changes when we merge type: regression A regression from a previous release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants