Skip to content

Commit 50e9674

Browse files
committed
Backport "Attempt to prevent JarFiles from being left open"
Update `JarFile` so that `super.close()` is called early so that the file is not left open. Since we re-implement `JarFile` methods to work directly on the underlying `RandomAccessDataFile`, it should be safe to close immediately. Closes gh-21177
1 parent e2705b2 commit 50e9674

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public class JarFile extends java.util.jar.JarFile {
8484

8585
private String comment;
8686

87+
private volatile boolean closed;
88+
8789
/**
8890
* Create a new {@link JarFile} backed by the specified file.
8991
* @param file the root jar file
@@ -109,6 +111,7 @@ public JarFile(File file) throws IOException {
109111
*/
110112
JarFile(JarFile parent) throws IOException {
111113
super(parent.rootFile.getFile());
114+
super.close();
112115
this.parent = parent;
113116
this.rootFile = parent.rootFile;
114117
this.pathFromRoot = parent.pathFromRoot;
@@ -140,6 +143,7 @@ private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, RandomAccess
140143
private JarFile(JarFile parent, RandomAccessDataFile rootFile, String pathFromRoot, RandomAccessData data,
141144
JarEntryFilter filter, JarFileType type, Supplier<Manifest> manifestSupplier) throws IOException {
142145
super(rootFile.getFile());
146+
super.close();
143147
this.parent = parent;
144148
this.rootFile = rootFile;
145149
this.pathFromRoot = pathFromRoot;
@@ -334,12 +338,19 @@ public int size() {
334338

335339
@Override
336340
public void close() throws IOException {
337-
super.close();
341+
if (this.closed) {
342+
return;
343+
}
344+
this.closed = true;
338345
if (this.type == JarFileType.DIRECT && this.parent == null) {
339346
this.rootFile.close();
340347
}
341348
}
342349

350+
boolean isClosed() {
351+
return this.closed;
352+
}
353+
343354
String getUrlString() throws MalformedURLException {
344355
if (this.urlString == null) {
345356
this.urlString = getUrl().toString();

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarURLConnectionTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.File;
2121
import java.io.FileNotFoundException;
2222
import java.net.URL;
23-
import java.util.zip.ZipFile;
2423

2524
import org.junit.Before;
2625
import org.junit.Rule;
@@ -29,7 +28,6 @@
2928

3029
import org.springframework.boot.loader.TestJarCreator;
3130
import org.springframework.boot.loader.jar.JarURLConnection.JarEntryName;
32-
import org.springframework.test.util.ReflectionTestUtils;
3331

3432
import static org.assertj.core.api.Assertions.assertThat;
3533
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -201,7 +199,7 @@ public void openConnectionCanBeClosedWithoutClosingSourceJar() throws Exception
201199
JarURLConnection connection = JarURLConnection.get(url, this.jarFile);
202200
JarFile connectionJarFile = connection.getJarFile();
203201
connectionJarFile.close();
204-
assertThat((Boolean) ReflectionTestUtils.getField(this.jarFile, ZipFile.class, "closeRequested")).isFalse();
202+
assertThat(this.jarFile.isClosed()).isFalse();
205203
}
206204

207205
private String getRelativePath() {

0 commit comments

Comments
 (0)