Skip to content

Commit 7a898cb

Browse files
committed
Prevent custom java.io.tmpdir from polluting JVM's temp file creation
If java.nio.file.Files.createTempFile or java.io.File.createTempFile(String, String) is called for the first time while the java.io.tmpdir system property is set to a custom value, the JVM's temporary file creation will then try to use that custom temporary directory for all subsequent file creation. This can result in failures if the custom temporary directory is deleted and the JVM then tries to use it. This commit avoids the problem by calls the two createTempFile methods while the default java.io.tmpdir value is in place. This ensures that the JVM will use this original temporary directory for all of its subsequent temporary file creation while allowing the tests to use a custom location without unwanted side-effects. Closes gh-41905
1 parent 2dbee6d commit 7a898cb

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.boot.logging;
1818

1919
import java.io.File;
20+
import java.io.IOException;
21+
import java.nio.file.Files;
2022
import java.nio.file.Path;
2123
import java.util.Arrays;
2224

@@ -43,8 +45,10 @@ public abstract class AbstractLoggingSystemTests {
4345
private String originalTempDirectory;
4446

4547
@BeforeEach
46-
void configureTempDir(@TempDir Path temp) {
48+
void configureTempDir(@TempDir Path temp) throws IOException {
4749
this.originalTempDirectory = System.getProperty(JAVA_IO_TMPDIR);
50+
Files.delete(Files.createTempFile("prevent", "pollution"));
51+
File.createTempFile("prevent", "pollution").delete();
4852
System.setProperty(JAVA_IO_TMPDIR, temp.toAbsolutePath().toString());
4953
MDC.clear();
5054
}

0 commit comments

Comments
 (0)