From bec385d310865331db34b1ef8f93f0b0c29eac2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Tue, 21 Nov 2023 12:00:33 +0100 Subject: [PATCH] Use JarURLConnection caching defaults In order to prevent leaks of large amounts of non-heap memory (and potential other efficiency and performance side effects), this commit updates ResourceUtils#useCachesIfNecessary to leave the caching flag to its JVM default value for instances of JarURLConnection. The previous behavior was originally introduced via gh-9316 and gh-13755 to avoid I/O failure during webapp hot reloading in Servlet containers. This is not a popular deployment mode anymore and we have not been able to reproduce the original issue with a Java 17 JVM and Tomcat 10. Closes gh-30955 --- .../io/support/PathMatchingResourcePatternResolver.java | 1 - .../java/org/springframework/util/ResourceUtils.java | 9 ++++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java index 84b085036dee..318855cadc03 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java @@ -680,7 +680,6 @@ protected Set doFindPathMatchingJarResources(Resource rootDirResource, if (con instanceof JarURLConnection jarCon) { // Should usually be the case for traditional JAR files. - ResourceUtils.useCachesIfNecessary(jarCon); jarFile = jarCon.getJarFile(); jarFileUrl = jarCon.getJarFileURL().toExternalForm(); JarEntry jarEntry = jarCon.getJarEntry(); diff --git a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java index db7f2e0e6f3f..b751fa5125e1 100644 --- a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java @@ -18,6 +18,7 @@ import java.io.File; import java.io.FileNotFoundException; +import java.net.JarURLConnection; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; @@ -422,12 +423,14 @@ public static URL toRelativeURL(URL root, String relativePath) throws MalformedU /** * Set the {@link URLConnection#setUseCaches "useCaches"} flag on the - * given connection, preferring {@code false} but leaving the - * flag at {@code true} for JNLP based resources. + * given connection, preferring {@code false} but leaving the flag at + * its JVM default value for jar resources (typically {@code true}). * @param con the URLConnection to set the flag on */ public static void useCachesIfNecessary(URLConnection con) { - con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP")); + if (!(con instanceof JarURLConnection)) { + con.setUseCaches(false); + } } }