Skip to content

Commit

Permalink
Prevent custom java.io.tmpdir from polluting JVM's temp file creation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wilkinsona committed Aug 16, 2024
1 parent 2dbee6d commit 7a898cb
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}
Expand Down

0 comments on commit 7a898cb

Please sign in to comment.