Repository navigation
Class loading fails with 'Zip file closed' while ClearCachesApplicationListener closes the jars of an executable jar - #52635
Conversation
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
bde0371 to
47c5978
Compare
|
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 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! |
|
@philwebb Thanks! I tried the latest snapshot ( I built the same application twice from the same commit: a Grails app on Tomcat with actuator, 135 nested jars, run with
With the snapshot, every start answered 200 on The RC2 start that never answered shows how bad the failure can be. Tomcat's acceptor thread failed to initialize 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. |
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 toJarUrlClassLoader, whereClearCachesApplicationListenercould 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 hasfindClasslook through these same jar files directly, and it is only onmain: 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
findClassholds a jar file acrossgetJarEntryanddefineClass. Handling it in the lookups would mean a retry or a lock on each offindClass,findResource(s)anddefinePackage.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
JarUrlClassLoaderin the executable jar:clearCache()The
Handlercaches, theNestedLocationcaches and the per-URLclearCache(URL)calls still run. What this does not do is release the file handles of these jar files, which #52582 lists as somethingclearCache()should do. They stay open as they have in every release since 3.2, untilclose().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.