changeset 51361:081b132c4dc0

8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong" Reviewed-by: bpb, mchung
author rriggs
date Fri, 22 Jun 2018 16:06:43 -0400
parents d7cf64ce3950
children 9375184cec98
files test/jdk/ProblemList.txt test/jdk/java/io/FileInputStream/UnreferencedFISClosesFd.java test/jdk/java/io/FileOutputStream/UnreferencedFOSClosesFd.java test/jdk/java/io/RandomAccessFile/UnreferencedRAFClosesFd.java test/lib/jdk/test/lib/util/FileUtils.java
diffstat 5 files changed, 131 insertions(+), 101 deletions(-) [+]
line wrap: on
line diff
--- a/test/jdk/ProblemList.txt	Fri Jun 22 12:51:49 2018 -0700
+++ b/test/jdk/ProblemList.txt	Fri Jun 22 16:06:43 2018 -0400
@@ -535,10 +535,6 @@
 
 java/io/pathNames/GeneralWin32.java                             8180264 windows-all
 
-java/io/FileInputStream/UnreferencedFISClosesFd.java            8202292 linux-all
-java/io/FileOutputStream/UnreferencedFOSClosesFd.java           8202292 linux-all
-java/io/RandomAccessFile/UnreferencedRAFClosesFd.java           8202292 linux-all
-
 ############################################################################
 
 # jdk_management
@@ -620,60 +616,60 @@
 sun/security/pkcs11/Cipher/ReinitCipher.java                    8204203 windows-all
 sun/security/pkcs11/Cipher/TestPKCS5PaddingError.java           8204203 windows-all
 sun/security/pkcs11/Cipher/TestRSACipher.java                   8204203 windows-all
-sun/security/pkcs11/Cipher/TestRSACipherWrap.java               8204203 windows-all 
-sun/security/pkcs11/Cipher/TestRawRSACipher.java                8204203 windows-all 
-sun/security/pkcs11/Cipher/TestSymmCiphers.java                 8204203 windows-all 
-sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java            8204203 windows-all 
-sun/security/pkcs11/KeyAgreement/SupportedDHKeys.java           8204203 windows-all 
-sun/security/pkcs11/KeyAgreement/TestDH.java                    8204203 windows-all 
-sun/security/pkcs11/KeyAgreement/TestInterop.java               8204203 windows-all 
-sun/security/pkcs11/KeyAgreement/TestShort.java                 8204203 windows-all 
-sun/security/pkcs11/KeyAgreement/UnsupportedDHKeys.java         8204203 windows-all 
-sun/security/pkcs11/KeyGenerator/DESParity.java                 8204203 windows-all 
-sun/security/pkcs11/KeyGenerator/TestKeyGenerator.java          8204203 windows-all 
-sun/security/pkcs11/KeyPairGenerator/TestDH2048.java            8204203 windows-all 
-sun/security/pkcs11/KeyStore/SecretKeysBasic.sh                 8204203 windows-all 
-sun/security/pkcs11/Mac/MacKAT.java                             8204203 windows-all 
-sun/security/pkcs11/Mac/MacSameTest.java                        8204203 windows-all 
-sun/security/pkcs11/Mac/ReinitMac.java                          8204203 windows-all 
-sun/security/pkcs11/MessageDigest/ByteBuffers.java              8204203 windows-all 
-sun/security/pkcs11/MessageDigest/DigestKAT.java                8204203 windows-all 
-sun/security/pkcs11/MessageDigest/ReinitDigest.java             8204203 windows-all 
-sun/security/pkcs11/MessageDigest/TestCloning.java              8204203 windows-all 
-sun/security/pkcs11/Provider/ConfigQuotedString.sh              8204203 windows-all 
-sun/security/pkcs11/Provider/Login.sh                           8204203 windows-all 
-sun/security/pkcs11/SampleTest.java                             8204203 windows-all 
-sun/security/pkcs11/Secmod/AddPrivateKey.java                   8204203 windows-all 
-sun/security/pkcs11/Secmod/Crypto.java                          8204203 windows-all 
-sun/security/pkcs11/Secmod/GetPrivateKey.java                   8204203 windows-all 
-sun/security/pkcs11/Secmod/JksSetPrivateKey.java                8204203 windows-all 
-sun/security/pkcs11/Secmod/LoadKeystore.java                    8204203 windows-all 
-sun/security/pkcs11/Secmod/TestNssDbSqlite.java                 8204203 windows-all 
-sun/security/pkcs11/SecureRandom/Basic.java                     8204203 windows-all 
-sun/security/pkcs11/SecureRandom/TestDeserialization.java       8204203 windows-all 
-sun/security/pkcs11/Serialize/SerializeProvider.java            8204203 windows-all 
-sun/security/pkcs11/Signature/ByteBuffers.java                  8204203 windows-all 
-sun/security/pkcs11/Signature/ReinitSignature.java              8204203 windows-all 
-sun/security/pkcs11/Signature/TestDSA.java                      8204203 windows-all 
-sun/security/pkcs11/Signature/TestDSAKeyLength.java             8204203 windows-all 
-sun/security/pkcs11/Signature/TestRSAKeyLength.java             8204203 windows-all 
-sun/security/pkcs11/ec/ReadCertificates.java                    8204203 windows-all 
-sun/security/pkcs11/ec/ReadPKCS12.java                          8204203 windows-all 
-sun/security/pkcs11/ec/TestCurves.java                          8204203 windows-all 
-sun/security/pkcs11/ec/TestECDH.java                            8204203 windows-all 
-sun/security/pkcs11/ec/TestECDH2.java                           8204203 windows-all 
-sun/security/pkcs11/ec/TestECDSA.java                           8204203 windows-all 
-sun/security/pkcs11/ec/TestECDSA2.java                          8204203 windows-all 
-sun/security/pkcs11/ec/TestECGenSpec.java                       8204203 windows-all 
-sun/security/pkcs11/rsa/KeyWrap.java                            8204203 windows-all 
-sun/security/pkcs11/rsa/TestCACerts.java                        8204203 windows-all 
-sun/security/pkcs11/rsa/TestKeyFactory.java                     8204203 windows-all 
-sun/security/pkcs11/rsa/TestKeyPairGenerator.java               8204203 windows-all 
-sun/security/pkcs11/rsa/TestSignatures.java                     8204203 windows-all 
-sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java            8204203 windows-all 
-sun/security/pkcs11/tls/TestLeadingZeroesP11.java               8204203 windows-all 
-sun/security/pkcs11/tls/TestMasterSecret.java                   8204203 windows-all 
-sun/security/pkcs11/tls/TestPRF.java                            8204203 windows-all 
+sun/security/pkcs11/Cipher/TestRSACipherWrap.java               8204203 windows-all
+sun/security/pkcs11/Cipher/TestRawRSACipher.java                8204203 windows-all
+sun/security/pkcs11/Cipher/TestSymmCiphers.java                 8204203 windows-all
+sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java            8204203 windows-all
+sun/security/pkcs11/KeyAgreement/SupportedDHKeys.java           8204203 windows-all
+sun/security/pkcs11/KeyAgreement/TestDH.java                    8204203 windows-all
+sun/security/pkcs11/KeyAgreement/TestInterop.java               8204203 windows-all
+sun/security/pkcs11/KeyAgreement/TestShort.java                 8204203 windows-all
+sun/security/pkcs11/KeyAgreement/UnsupportedDHKeys.java         8204203 windows-all
+sun/security/pkcs11/KeyGenerator/DESParity.java                 8204203 windows-all
+sun/security/pkcs11/KeyGenerator/TestKeyGenerator.java          8204203 windows-all
+sun/security/pkcs11/KeyPairGenerator/TestDH2048.java            8204203 windows-all
+sun/security/pkcs11/KeyStore/SecretKeysBasic.sh                 8204203 windows-all
+sun/security/pkcs11/Mac/MacKAT.java                             8204203 windows-all
+sun/security/pkcs11/Mac/MacSameTest.java                        8204203 windows-all
+sun/security/pkcs11/Mac/ReinitMac.java                          8204203 windows-all
+sun/security/pkcs11/MessageDigest/ByteBuffers.java              8204203 windows-all
+sun/security/pkcs11/MessageDigest/DigestKAT.java                8204203 windows-all
+sun/security/pkcs11/MessageDigest/ReinitDigest.java             8204203 windows-all
+sun/security/pkcs11/MessageDigest/TestCloning.java              8204203 windows-all
+sun/security/pkcs11/Provider/ConfigQuotedString.sh              8204203 windows-all
+sun/security/pkcs11/Provider/Login.sh                           8204203 windows-all
+sun/security/pkcs11/SampleTest.java                             8204203 windows-all
+sun/security/pkcs11/Secmod/AddPrivateKey.java                   8204203 windows-all
+sun/security/pkcs11/Secmod/Crypto.java                          8204203 windows-all
+sun/security/pkcs11/Secmod/GetPrivateKey.java                   8204203 windows-all
+sun/security/pkcs11/Secmod/JksSetPrivateKey.java                8204203 windows-all
+sun/security/pkcs11/Secmod/LoadKeystore.java                    8204203 windows-all
+sun/security/pkcs11/Secmod/TestNssDbSqlite.java                 8204203 windows-all
+sun/security/pkcs11/SecureRandom/Basic.java                     8204203 windows-all
+sun/security/pkcs11/SecureRandom/TestDeserialization.java       8204203 windows-all
+sun/security/pkcs11/Serialize/SerializeProvider.java            8204203 windows-all
+sun/security/pkcs11/Signature/ByteBuffers.java                  8204203 windows-all
+sun/security/pkcs11/Signature/ReinitSignature.java              8204203 windows-all
+sun/security/pkcs11/Signature/TestDSA.java                      8204203 windows-all
+sun/security/pkcs11/Signature/TestDSAKeyLength.java             8204203 windows-all
+sun/security/pkcs11/Signature/TestRSAKeyLength.java             8204203 windows-all
+sun/security/pkcs11/ec/ReadCertificates.java                    8204203 windows-all
+sun/security/pkcs11/ec/ReadPKCS12.java                          8204203 windows-all
+sun/security/pkcs11/ec/TestCurves.java                          8204203 windows-all
+sun/security/pkcs11/ec/TestECDH.java                            8204203 windows-all
+sun/security/pkcs11/ec/TestECDH2.java                           8204203 windows-all
+sun/security/pkcs11/ec/TestECDSA.java                           8204203 windows-all
+sun/security/pkcs11/ec/TestECDSA2.java                          8204203 windows-all
+sun/security/pkcs11/ec/TestECGenSpec.java                       8204203 windows-all
+sun/security/pkcs11/rsa/KeyWrap.java                            8204203 windows-all
+sun/security/pkcs11/rsa/TestCACerts.java                        8204203 windows-all
+sun/security/pkcs11/rsa/TestKeyFactory.java                     8204203 windows-all
+sun/security/pkcs11/rsa/TestKeyPairGenerator.java               8204203 windows-all
+sun/security/pkcs11/rsa/TestSignatures.java                     8204203 windows-all
+sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java            8204203 windows-all
+sun/security/pkcs11/tls/TestLeadingZeroesP11.java               8204203 windows-all
+sun/security/pkcs11/tls/TestMasterSecret.java                   8204203 windows-all
+sun/security/pkcs11/tls/TestPRF.java                            8204203 windows-all
 sun/security/pkcs11/tls/TestPremaster.java                      8204203 windows-all
 
 ############################################################################
--- a/test/jdk/java/io/FileInputStream/UnreferencedFISClosesFd.java	Fri Jun 22 12:51:49 2018 -0700
+++ b/test/jdk/java/io/FileInputStream/UnreferencedFISClosesFd.java	Fri Jun 22 16:06:43 2018 -0400
@@ -25,6 +25,8 @@
  *
  * @test
  * @modules java.base/java.io:open
+ * @library /test/lib
+ * @build jdk.test.lib.util.FileUtils UnreferencedFISClosesFd
  * @bug 6524062
  * @summary Test to ensure that FIS.finalize() invokes the close() method as per
  * the specification.
@@ -41,18 +43,15 @@
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
 import java.lang.reflect.Field;
-import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.util.ArrayDeque;
 import java.util.HashSet;
-import java.util.List;
-import java.util.Optional;
-import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import com.sun.management.UnixOperatingSystemMXBean;
 
+import jdk.test.lib.util.FileUtils;
+
 /**
  * Tests for FIS unreferenced.
  *  - Not subclassed - cleaner cleanup
@@ -146,8 +145,8 @@
 
         String name = inFile.getPath();
 
+        FileUtils.listFileDescriptors(System.out);
         long fdCount0 = getFdCount();
-        System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
 
         int failCount = 0;
         failCount += test(new FileInputStream(name), CleanupType.CLEANER);
@@ -166,11 +165,10 @@
 
         // Check the final count of open file descriptors
         long fdCount = getFdCount();
-        System.out.printf("final count of open file descriptors: %d%n", fdCount);
         if (fdCount != fdCount0) {
-            listProcFD();
-            throw new AssertionError("raw fd count wrong: expected: " + fdCount0
-                    + ", actual: " + fdCount);
+            System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
+            System.out.printf("final count of open file descriptors: %d%n", fdCount);
+            FileUtils.listFileDescriptors(System.out);
         }
     }
 
@@ -274,27 +272,4 @@
         }
         return 0;
     }
-
-    /**
-     * Method to list the open file descriptors (if supported by the 'lsof' command).
-     */
-    static void listProcFD() {
-        List<String> lsofDirs = List.of("/usr/bin", "/usr/sbin");
-        Optional<Path> lsof = lsofDirs.stream()
-                .map(s -> Paths.get(s, "lsof"))
-                .filter(f -> Files.isExecutable(f))
-                .findFirst();
-        lsof.ifPresent(exe -> {
-            try {
-                System.out.printf("Open File Descriptors:%n");
-                long pid = ProcessHandle.current().pid();
-                ProcessBuilder pb = new ProcessBuilder(exe.toString(), "-p", Integer.toString((int) pid));
-                pb.inheritIO();
-                Process p = pb.start();
-                p.waitFor(10, TimeUnit.SECONDS);
-            } catch (IOException | InterruptedException ie) {
-                ie.printStackTrace();
-            }
-        });
-    }
 }
--- a/test/jdk/java/io/FileOutputStream/UnreferencedFOSClosesFd.java	Fri Jun 22 12:51:49 2018 -0700
+++ b/test/jdk/java/io/FileOutputStream/UnreferencedFOSClosesFd.java	Fri Jun 22 16:06:43 2018 -0400
@@ -25,6 +25,8 @@
  *
  * @test
  * @modules java.base/java.io:open
+ * @library /test/lib
+ * @build jdk.test.lib.util.FileUtils UnreferencedFOSClosesFd
  * @bug 6524062
  * @summary Test to ensure that FOS.finalize() invokes the close() method as per
  * the specification.
@@ -41,11 +43,15 @@
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
 import java.lang.reflect.Field;
+import java.nio.file.Path;
+import java.util.ArrayDeque;
 import java.util.HashSet;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import com.sun.management.UnixOperatingSystemMXBean;
 
+import jdk.test.lib.util.FileUtils;
+
 public class UnreferencedFOSClosesFd {
 
     enum CleanupType {
@@ -133,8 +139,8 @@
 
         String name = inFile.getPath();
 
+        FileUtils.listFileDescriptors(System.out);
         long fdCount0 = getFdCount();
-        System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
 
         int failCount = 0;
         failCount += test(new FileOutputStream(name), CleanupType.CLEANER);
@@ -153,10 +159,10 @@
 
         // Check the final count of open file descriptors
         long fdCount = getFdCount();
-        System.out.printf("final count of open file descriptors: %d%n", fdCount);
         if (fdCount != fdCount0) {
-            throw new AssertionError("raw fd count wrong: expected: " + fdCount0
-            + ", actual: " + fdCount);
+            System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
+            System.out.printf("final count of open file descriptors: %d%n", fdCount);
+            FileUtils.listFileDescriptors(System.out);
         }
     }
 
--- a/test/jdk/java/io/RandomAccessFile/UnreferencedRAFClosesFd.java	Fri Jun 22 12:51:49 2018 -0700
+++ b/test/jdk/java/io/RandomAccessFile/UnreferencedRAFClosesFd.java	Fri Jun 22 16:06:43 2018 -0400
@@ -24,6 +24,7 @@
 import java.io.File;
 import java.io.FileDescriptor;
 import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.lang.management.ManagementFactory;
 import java.lang.management.OperatingSystemMXBean;
@@ -32,13 +33,19 @@
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
 import java.lang.reflect.Field;
+import java.nio.file.Path;
+import java.util.ArrayDeque;
 import java.util.HashSet;
 
 import com.sun.management.UnixOperatingSystemMXBean;
 
+import jdk.test.lib.util.FileUtils;
+
 /**
  * @test
  * @bug 8080225
+ * @library /test/lib
+ * @build jdk.test.lib.util.FileUtils UnreferencedRAFClosesFd
  * @modules java.base/java.io:open
  * @summary Test to ensure that an unclosed and unreferenced RandomAccessFile closes the fd
  * @run main/othervm UnreferencedRAFClosesFd
@@ -54,8 +61,8 @@
         inFile.createNewFile();
         inFile.deleteOnExit();
 
+        FileUtils.listFileDescriptors(System.out);
         long fdCount0 = getFdCount();
-        System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
 
         String name = inFile.getPath();
         RandomAccessFile raf;
@@ -101,10 +108,10 @@
 
         // Check the final count of open file descriptors
         long fdCount = getFdCount();
-        System.out.printf("final count of open file descriptors: %d%n", fdCount);
         if (fdCount != fdCount0) {
-            throw new AssertionError("raw fd count wrong: expected: " + fdCount0
-                    + ", actual: " + fdCount);
+            System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
+            System.out.printf("final count of open file descriptors: %d%n", fdCount);
+            FileUtils.listFileDescriptors(System.out);
         }
     }
 
--- a/test/lib/jdk/test/lib/util/FileUtils.java	Fri Jun 22 12:51:49 2018 -0700
+++ b/test/lib/jdk/test/lib/util/FileUtils.java	Fri Jun 22 16:06:43 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,18 +26,26 @@
 import jdk.test.lib.Platform;
 
 import java.io.IOException;
+import java.io.PrintStream;
+import java.io.UncheckedIOException;
+import java.lang.ProcessBuilder.Redirect;
 import java.nio.file.DirectoryNotEmptyException;
 import java.nio.file.FileVisitResult;
 import java.nio.file.Files;
 import java.nio.file.NoSuchFileException;
 import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.nio.file.SimpleFileVisitor;
 import java.nio.file.attribute.BasicFileAttributes;
+import java.time.Instant;
+import java.time.Duration;
 import java.util.ArrayList;
+import java.util.ArrayDeque;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Optional;
 import java.util.concurrent.TimeUnit;
 
-
 /**
  * Common library for various test file utility functions.
  */
@@ -227,4 +235,42 @@
         }
         return areFileSystemsAccessible;
     }
+
+    /**
+     * List the open file descriptors (if supported by the 'lsof' command).
+     * @param ps a printStream to send the output to
+     * @throws UncheckedIOException if an error occurs
+     */
+    public static void listFileDescriptors(PrintStream ps) {
+        List<String> lsofDirs = List.of("/usr/bin", "/usr/sbin");
+        Optional<Path> lsof = lsofDirs.stream()
+                .map(s -> Paths.get(s, "lsof"))
+                .filter(f -> Files.isExecutable(f))
+                .findFirst();
+        lsof.ifPresent(exe -> {
+            try {
+                ps.printf("Open File Descriptors:%n");
+                long pid = ProcessHandle.current().pid();
+                ProcessBuilder pb = new ProcessBuilder(exe.toString(), "-p", Integer.toString((int) pid));
+                pb.redirectErrorStream(true);   // combine stderr and stdout
+                pb.redirectOutput(Redirect.PIPE);
+
+                Process p = pb.start();
+                Instant start = Instant.now();
+                p.getInputStream().transferTo(ps);
+
+                try {
+                    int timeout = 10;
+                    if (!p.waitFor(timeout, TimeUnit.SECONDS)) {
+                        System.out.printf("waitFor timed out: %d%n", timeout);
+                    }
+                } catch (InterruptedException ie) {
+                    throw new IOException("interrupted", ie);
+                }
+                ps.println();
+            } catch (IOException ioe) {
+                throw new UncheckedIOException("error listing file descriptors", ioe);
+            }
+        });
+    }
 }