-
Notifications
You must be signed in to change notification settings - Fork 41.6k
Description
Hi! I was having trouble with some JUnit test cases where the tests would fail to clean up their temporary directories after running. I tracked down the issue to three things:
- There doesn't appear to be a way to close a JarFileArchive
- org.springframework.boot.loader.LaunchedURLClassLoader doesn't close open files when close() is called
- Even if I could close a JarFileArchive, it contains an instance of org.springframework.boot.loader.jar.JarFile. All instances of org.springframework.boot.loader.jar.JarFile fail to close properly when close() is called on them.
This issue describes item (3). Please let me know if more information is needed.
Description
The JarFile class in org.springframework.boot.loader.jar doesn't close correctly. I verified this by pausing execution after closing the file and checking if the file handle was closed. It was not. With a regular java.util.jar.JarFile, calling close() immediately closes the OS-level file handle.
Analysis
I suspect this is because org.springframework.boot.loader.jar.JarFile doesn't call super.close(). The offending code:
@Override
public void close() throws IOException {
this.rootFile.close();
}
Failing test case
Here is a JUnit test case that fails on my system. I figure out if the JarFile is closed by trying to delete it's containing folder. On Windows, this will fail if the JarFile is still open.
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.loader.jar.JarFile;
import org.springframework.boot.loader.tools.JarWriter;
import java.io.File;
import java.io.IOException;
import static org.junit.Assert.assertFalse;
public class Example {
@Rule
public TemporaryFolder tmpDir = new TemporaryFolder();
@Test
public void testJarFileCloses() throws IOException {
File tmp2 = tmpDir.newFile();
JarWriter ok = new JarWriter(tmp2);
ok.close();
java.util.jar.JarFile jf2 = new java.util.jar.JarFile(tmp2);
jf2.close();
File tmp = tmpDir.newFile();
JarWriter jarWriter = new JarWriter(tmp);
jarWriter.close();
JarFile jf = new JarFile(tmp);
jf.close();
tmpDir.delete();
assertFalse("tmpDir.delete() should be able to delete the directory", tmpDir.getRoot().exists());
}
}
System information
java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
IDE information
IntelliJ IDEA 2017.1
Build #IU-171.3780.107, built on March 22, 2017
Licensed to Kevin Perkins
Subscription is active until August 25, 2017
JRE: 1.8.0_112-release-736-b13 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
OS Version Information
OS Name Microsoft Windows 10 Pro
Version 10.0.14393 Build 14393
System Type x64-based PC