Skip to content

Commit 0baf81c

Browse files
committed
do not clean up old temp subprocess argfiles created by another process
1 parent 74a74c8 commit 0baf81c

File tree

2 files changed

+4
-83
lines changed

2 files changed

+4
-83
lines changed

ci/common.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ local common_json = import "../common.json";
146146
"Dumping debug output to '(?P<filename>[^']+)'",
147147
# Keep in sync with com.oracle.svm.hosted.NativeImageOptions#DEFAULT_ERROR_FILE_NAME
148148
" (?P<filename>.+/svm_err_b_\\d+T\\d+\\.\\d+_pid\\d+\\.md)",
149-
# Keep in sync with jdk.graal.compiler.test.SubprocessUtil#SUBPROCESS_ARGFILES
149+
# Keep in sync with jdk.graal.compiler.test.SubprocessUtil#makeArgfile
150150
" @(?P<filename>.*SubprocessUtil.*\\.argfile)",
151151
],
152152
},

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/test/SubprocessUtil.java

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,9 @@
2828
import java.io.File;
2929
import java.io.IOException;
3030
import java.io.InputStreamReader;
31-
import java.nio.file.FileSystems;
3231
import java.nio.file.Files;
3332
import java.nio.file.Path;
34-
import java.nio.file.attribute.FileTime;
35-
import java.nio.file.attribute.UserPrincipal;
3633
import java.time.Duration;
37-
import java.time.Instant;
38-
import java.time.temporal.ChronoUnit;
3934
import java.util.ArrayList;
4035
import java.util.Arrays;
4136
import java.util.Formatter;
@@ -478,9 +473,6 @@ private static Subprocess javaHelper(List<String> vmArgs, Map<String, String> en
478473

479474
private static final Set<String> EXECUTABLES_USING_ARGFILES = Set.of("java", "java.exe", "javac", "javac.exe");
480475

481-
// Keep in sync with the {@code catch_files} array in {@code ci/common.jsonnet}.
482-
private static final TemporaryFiles SUBPROCESS_ARGFILES = new TemporaryFiles(SubprocessUtil.class.getName() + ".", ".argfile");
483-
484476
/**
485477
* Creates an argfile for {@code command} if {@code command.get(0)} denotes an
486478
* {@linkplain #EXECUTABLES_USING_ARGFILES executable supporting argfiles}.
@@ -491,15 +483,16 @@ private static Subprocess javaHelper(List<String> vmArgs, Map<String, String> en
491483
public static Path makeArgfile(List<String> command) {
492484
File exe = new File(command.get(0));
493485
if (EXECUTABLES_USING_ARGFILES.contains(exe.getName())) {
494-
Path argfile = SUBPROCESS_ARGFILES.newFile();
495486
expandArgFileArgs(command);
496487
try {
488+
// Keep in sync with the {@code catch_files} array in {@code ci/common.jsonnet}.
489+
Path argfile = Files.createTempFile(SubprocessUtil.class.getName() + ".", ".argfile");
497490
String content = expandArgFileArgs(command).stream().map(SubprocessUtil::quoteArgfileArgument).collect(Collectors.joining("\n"));
498491
Files.writeString(argfile, "# " + content);
492+
return argfile;
499493
} catch (IOException e) {
500494
throw new AssertionError(e);
501495
}
502-
return argfile;
503496
}
504497
return null;
505498
}
@@ -598,76 +591,4 @@ private static int findMainClassIndex(List<String> commandLine) {
598591
}
599592
throw new InternalError();
600593
}
601-
602-
/**
603-
* Manages temporary files with a given prefix and suffix. When new files are created, temporary
604-
* files older than 2 hours are deleted.
605-
*/
606-
public static class TemporaryFiles {
607-
private final Path dir;
608-
private final FileTime maxAge;
609-
private final String prefix;
610-
private final String suffix;
611-
private final UserPrincipal user;
612-
613-
public TemporaryFiles(String prefix, String suffix) {
614-
this.dir = Path.of(System.getProperty("java.io.tmpdir"));
615-
this.maxAge = DEBUG ? //
616-
FileTime.from(Instant.now().minus(5, ChronoUnit.SECONDS)) : //
617-
FileTime.from(Instant.now().minus(2, ChronoUnit.HOURS));
618-
this.prefix = prefix;
619-
this.suffix = suffix;
620-
try {
621-
this.user = FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByName(System.getProperty("user.name"));
622-
} catch (IOException e) {
623-
throw new AssertionError(e);
624-
}
625-
}
626-
627-
/**
628-
* Creates a new temporary file after {@linkplain #removeOldFiles() deleting} old temporary
629-
* files.
630-
*/
631-
Path newFile() {
632-
removeOldFiles();
633-
try {
634-
Path path = Files.createTempFile(dir, prefix, suffix).toAbsolutePath();
635-
if (DEBUG) {
636-
System.out.println("created " + path);
637-
}
638-
return path;
639-
} catch (IOException e) {
640-
throw new AssertionError(e);
641-
}
642-
}
643-
644-
/**
645-
* Deletes argfiles in {@link #dir} that start with {@link #prefix} and end with
646-
* {@link #suffix} whose last modification time is before {@link #maxAge}.
647-
*/
648-
private void removeOldFiles() {
649-
try {
650-
Files.list(dir).filter(this::match).forEach(p -> {
651-
try {
652-
if (Files.getOwner(p).equals(user) && Files.getLastModifiedTime(p).compareTo(maxAge) < 0) {
653-
Files.delete(p);
654-
if (DEBUG) {
655-
System.out.println("deleted " + p);
656-
}
657-
return;
658-
}
659-
} catch (IOException e) {
660-
System.err.printf("Error deleting %s: %e%n", p, e);
661-
}
662-
});
663-
} catch (IOException e) {
664-
System.err.printf("Error listing files in %s: %e%n", dir, e);
665-
}
666-
}
667-
668-
private boolean match(Path p) {
669-
String fileName = p.getFileName().toString();
670-
return fileName.startsWith(prefix) && fileName.endsWith(suffix);
671-
}
672-
}
673594
}

0 commit comments

Comments
 (0)