Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change in Jar usecache behavior with Spring 6.1.x causing java.lang.IllegalStateException: zip file closed #34678

Closed
rahulsh1 opened this issue Mar 29, 2025 · 4 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: regression A bug that is also a regression
Milestone

Comments

@rahulsh1
Copy link

We use the PathMatchingResourcePatternResolver outside of Spring to discover resources on the classpath, as it provides significant performance benefits as compared to anything else we came up with.

One such class looks for resources using a ForkJoinPool to scan for multiple patterns in the classpath using PathMatchingResourcePatternResolver. This worked fine with Spring 5.3.x.
Post migration to Spring 6.1.x, we started seeing a random java.lang.IllegalStateException: zip file closed on some of the invocations

After looking at the implementation differences between the 5.3.x and 6.1.x, it appears a change was done to remove the call to ResourceUtils.useCachesIfNecessary(jarCon);

The 5.3.x implementation sets the useCache on the JarConnection as false (since there is no JNLP) while there is no such thing happening with 6.1.x.

// 5.3.x
public static void useCachesIfNecessary(URLConnection con) {
  con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP"));
}

This appears to be related to JDK-6947916 where inconsistent values of the useCache flag may result in this behavior for a multi-threaded invocation.

With Spring 5.3.x, this was explicitly set to be false while now with Spring 6.1.x, that guarantee does not exist resulting in the race condition and random "zip file closed" exceptions.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 29, 2025
@jhoeller jhoeller self-assigned this Mar 29, 2025
@jhoeller jhoeller added type: regression A bug that is also a regression in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 29, 2025
@jhoeller jhoeller added this to the 6.2.6 milestone Mar 29, 2025
@jhoeller
Copy link
Contributor

This is actually a 6.1.x regression. That said, please note that PathMatchingResourcePatternResolver got heavily revised in 6.2 for internal caching. It'd be great if you could give 6.2.5 a try and see whether the problem still remains against that caching approach.

Generally wondering, is anything holding you back from upgrading to 6.2.x directly? At this point, it's the recommended target for 5.3.x migrations.

@jhoeller
Copy link
Contributor

We can make jar caching configurable through a setUseCaches method on PathMatchingResourcePatternResolver. On 6.2, this affects JarURLConnection caching as well as our local caching in the resolver, whereas on 6.1.19 it only affects JarURLConnection. The default stays the same but you may set it to false if that turns out to be necessary in your scenario.

@jhoeller jhoeller added the for: backport-to-6.1.x Marks an issue as a candidate for backport to 6.1.x label Mar 31, 2025
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-6.1.x Marks an issue as a candidate for backport to 6.1.x labels Mar 31, 2025
@jhoeller
Copy link
Contributor

@rahulsh1 this setUseCaches(false) option on PathMatchingResourcePatternResolver is available in the latest 6.2.6 and 6.1.19 snapshots now. Please give it a try at your earliest convenience!

@rahulsh1
Copy link
Author

thanks @jhoeller. Will try it out this week and report back but I think the fix should get back the same behavior as 5.3.

Will start with Spring 6.2 in a couple of weeks.
Good to know that PathMatchingResourcePatternResolver got heavily revised in 6.2 with caching support.

In 2018, I implemented a custom cached version of PathMatchingResourcePatternResolver and submitted it as PR-1981, which provided performance improvements through caching. It might be time to consider retiring the home-grown version in favor of the enhanced version in 6.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

3 participants