< prev index next >

test/hotspot/jtreg/runtime/8176717/TestInheritFD.java

changes after first review

8202740: runtime/8176717/TestInheritFD.java fails with java.lang.RuntimeException: could not match: VM RESULT => RETAINS FD

*** 1,14 **** --- 1,22 ---- import static java.io.File.createTempFile; import static java.lang.Long.parseLong; import static java.lang.System.getProperty; import static java.lang.management.ManagementFactory.getOperatingSystemMXBean; import static java.nio.file.Files.readAllBytes; + import static java.nio.file.Files.readSymbolicLink; + import static java.util.Arrays.stream; + import static java.util.Optional.empty; + import static java.util.Optional.of; + import static java.util.stream.Collectors.joining; import static jdk.test.lib.process.ProcessTools.createJavaProcessBuilder; import java.io.File; + import java.io.FileNotFoundException; + import java.io.FileOutputStream; import java.io.IOException; + import java.util.Optional; import com.sun.management.UnixOperatingSystemMXBean; /* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. ***************
*** 35,44 **** --- 43,53 ---- /* * @test TestInheritFD * @bug 8176717 8176809 * @summary a new process should not inherit open file descriptors + * @requires (os.family != "mac") * @library /test/lib * @modules java.base/jdk.internal.misc * java.management */ ***************
*** 64,77 **** public class TestInheritFD { public static final String LEAKS_FD = "VM RESULT => LEAKS FD"; public static final String RETAINS_FD = "VM RESULT => RETAINS FD"; public static final String EXIT = "VM RESULT => VM EXIT"; // first VM public static void main(String[] args) throws Exception { ! String logPath = createTempFile("logging", ".log").getName(); File commFile = createTempFile("communication", ".txt"); ProcessBuilder pb = createJavaProcessBuilder( "-Xlog:gc:\"" + logPath + "\"", "-Dtest.jdk=" + getProperty("test.jdk"), --- 73,87 ---- public class TestInheritFD { public static final String LEAKS_FD = "VM RESULT => LEAKS FD"; public static final String RETAINS_FD = "VM RESULT => RETAINS FD"; public static final String EXIT = "VM RESULT => VM EXIT"; + public static final String LOG_SUFFIX = ".strangelogsuffixthatcanbecheckedfor"; // first VM public static void main(String[] args) throws Exception { ! String logPath = createTempFile("logging", LOG_SUFFIX).getName(); File commFile = createTempFile("communication", ".txt"); ProcessBuilder pb = createJavaProcessBuilder( "-Xlog:gc:\"" + logPath + "\"", "-Dtest.jdk=" + getProperty("test.jdk"), ***************
*** 105,128 **** args[0], "" + ProcessHandle.current().pid(), "" + (supportsUnixMXBean()?+unixNrFD():-1)); pb.inheritIO(); // in future, redirect information from third VM to first VM pb.start(); } } static class VMShouldNotInheritFileDescriptors { // third VM public static void main(String[] args) throws InterruptedException { File logFile = new File(args[0]); long parentPid = parseLong(args[1]); long parentFDCount = parseLong(args[2]); if(supportsUnixMXBean()){ long thisFDCount = unixNrFD(); ! System.out.println("This VM FD-count (" + thisFDCount + ") should be strictly less than parent VM FD-count (" + parentFDCount + ") as log file should have been closed"); ! System.out.println(thisFDCount<parentFDCount?RETAINS_FD:LEAKS_FD); } else if (getProperty("os.name").toLowerCase().contains("win")) { windows(logFile, parentPid); } else { System.out.println(LEAKS_FD); // default fail on unknown configuration } --- 115,147 ---- args[0], "" + ProcessHandle.current().pid(), "" + (supportsUnixMXBean()?+unixNrFD():-1)); pb.inheritIO(); // in future, redirect information from third VM to first VM pb.start(); + + findOpenLogFile(); } } static class VMShouldNotInheritFileDescriptors { // third VM public static void main(String[] args) throws InterruptedException { File logFile = new File(args[0]); long parentPid = parseLong(args[1]); long parentFDCount = parseLong(args[2]); + fakeLeakyJVM(false); + if(supportsUnixMXBean()){ long thisFDCount = unixNrFD(); ! System.out.println("This VM FD-count (" ! + thisFDCount ! + ") should be strictly less than parent VM FD-count (" ! + parentFDCount ! + ") as log file should have been closed, HOWEVER, THIS CAN NOT BE RELIED" ! + " ON as files in /proc and /sys are opened by the JVM"); ! System.out.println(findOpenLogFile() ? LEAKS_FD : RETAINS_FD); } else if (getProperty("os.name").toLowerCase().contains("win")) { windows(logFile, parentPid); } else { System.out.println(LEAKS_FD); // default fail on unknown configuration } ***************
*** 137,147 **** static long unixNrFD() { UnixOperatingSystemMXBean osBean = (UnixOperatingSystemMXBean) getOperatingSystemMXBean(); return osBean.getOpenFileDescriptorCount(); } static void windows(File f, long parentPid) throws InterruptedException { System.out.println("waiting for pid: " + parentPid); ProcessHandle.of(parentPid).ifPresent(handle -> handle.onExit().join()); System.out.println("trying to rename file to the same name: " + f); ! System.out.println(f.renameTo(f)?RETAINS_FD:LEAKS_FD); // this parts communicates a closed file descriptor by printing "CLOSED FD" } --- 156,199 ---- static long unixNrFD() { UnixOperatingSystemMXBean osBean = (UnixOperatingSystemMXBean) getOperatingSystemMXBean(); return osBean.getOpenFileDescriptorCount(); } + static Optional<String> linkTargetName(File f) { + try { + return of(readSymbolicLink(f.toPath()).toFile().getName()); + } catch (IOException e) { + return empty(); + } + } + + @SuppressWarnings("resource") + static void fakeLeakyJVM(boolean fake) { + if (fake) { + try { + new FileOutputStream("fakeLeakyJVM" + LOG_SUFFIX, false); + } catch (FileNotFoundException e) { + } + } + } + + static boolean findOpenLogFile() { + File dir = new File("/proc/self/fd"); + + System.out.println("Open file descriptors:\n" + stream(dir.listFiles()) + .map(f -> f.getAbsolutePath() + " maps to: " + linkTargetName(f).orElse("?")) + .collect(joining("\n"))); + + return stream(dir.listFiles()) + .map(TestInheritFD::linkTargetName) + .flatMap(Optional::stream) + .filter(fileName -> fileName.endsWith(LOG_SUFFIX)) + .findAny() + .isPresent(); + } + static void windows(File f, long parentPid) throws InterruptedException { System.out.println("waiting for pid: " + parentPid); ProcessHandle.of(parentPid).ifPresent(handle -> handle.onExit().join()); System.out.println("trying to rename file to the same name: " + f); ! System.out.println(f.renameTo(f) ? RETAINS_FD : LEAKS_FD); // this parts communicates a closed file descriptor by printing "VM RESULT => RETAINS FD" }
< prev index next >