From 7a898cbbec884c9be34b047df1e8abba44f3e76c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 16 Aug 2024 19:14:58 +0100 Subject: [PATCH] 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 --- .../boot/logging/AbstractLoggingSystemTests.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java index 29a240b6b9..fd65041786 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/AbstractLoggingSystemTests.java @@ -17,6 +17,8 @@ package org.springframework.boot.logging; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; @@ -43,8 +45,10 @@ public abstract class AbstractLoggingSystemTests { private String originalTempDirectory; @BeforeEach - void configureTempDir(@TempDir Path temp) { + void configureTempDir(@TempDir Path temp) throws IOException { this.originalTempDirectory = System.getProperty(JAVA_IO_TMPDIR); + Files.delete(Files.createTempFile("prevent", "pollution")); + File.createTempFile("prevent", "pollution").delete(); System.setProperty(JAVA_IO_TMPDIR, temp.toAbsolutePath().toString()); MDC.clear(); }