Skip to content

Commit 360eb02

Browse files
committed
Don't close nested jars or wrapper when parent is closed
Update `JarFile` so that the `close()` method no longer closes nested jars or the wrapper. Prior to this commit it was possible for a parent jar file to be garbage collected and closed even though references still existed to the nested jars. When this happened the nested jars would get closed and any access to entries would result in `JarFile.ensureOpen()` throwing an `IllegalStateException`. The user would often not see this exception directly, but rather find `ClassNotFoundException` being thrown. Fixes gh-31853
1 parent 48443c9 commit 360eb02

File tree

1 file changed

+1
-14
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar

1 file changed

+1
-14
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@
2626
import java.net.URLStreamHandler;
2727
import java.net.URLStreamHandlerFactory;
2828
import java.security.Permission;
29-
import java.util.ArrayList;
30-
import java.util.Collections;
3129
import java.util.Enumeration;
3230
import java.util.Iterator;
33-
import java.util.List;
3431
import java.util.Spliterator;
3532
import java.util.Spliterators;
3633
import java.util.function.Supplier;
@@ -96,8 +93,6 @@ public class JarFile extends AbstractJarFile implements Iterable<java.util.jar.J
9693

9794
private volatile JarFileWrapper wrapper;
9895

99-
private final List<JarFile> nestedJars = Collections.synchronizedList(new ArrayList<>());
100-
10196
/**
10297
* Create a new {@link JarFile} backed by the specified file.
10398
* @param file the root jar file
@@ -340,10 +335,8 @@ private JarFile createJarFileFromFileEntry(JarEntry entry) throws IOException {
340335
+ "mechanism used to create your executable jar file");
341336
}
342337
RandomAccessData entryData = this.entries.getEntryData(entry.getName());
343-
JarFile nestedJar = new JarFile(this.rootFile, this.pathFromRoot + "!/" + entry.getName(), entryData,
338+
return new JarFile(this.rootFile, this.pathFromRoot + "!/" + entry.getName(), entryData,
344339
JarFileType.NESTED_JAR);
345-
this.nestedJars.add(nestedJar);
346-
return nestedJar;
347340
}
348341

349342
@Override
@@ -368,12 +361,6 @@ public void close() throws IOException {
368361
if (this.type == JarFileType.DIRECT) {
369362
this.rootFile.close();
370363
}
371-
if (this.wrapper != null) {
372-
this.wrapper.close();
373-
}
374-
for (JarFile nestedJar : this.nestedJars) {
375-
nestedJar.close();
376-
}
377364
this.closed = true;
378365
}
379366
}

0 commit comments

Comments
 (0)