8194929: Unreferenced FileDescriptors not closed
authorrriggs
Tue, 16 Jan 2018 10:48:58 -0500
changeset 48534 12d9ff9e0a4b
parent 48533 789efc16f4b1
child 48535 5f9977540ac9
8194929: Unreferenced FileDescriptors not closed Reviewed-by: alanb
make/mapfiles/libjava/mapfile-vers
test/jdk/java/io/FileInputStream/UnreferencedFISClosesFd.java
test/jdk/java/io/FileOutputStream/UnreferencedFOSClosesFd.java
test/jdk/java/io/RandomAccessFile/UnreferencedRAFClosesFd.java
--- a/make/mapfiles/libjava/mapfile-vers	Tue Jan 16 20:56:01 2018 +0530
+++ b/make/mapfiles/libjava/mapfile-vers	Tue Jan 16 10:48:58 2018 -0500
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 1997, 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
@@ -74,6 +74,7 @@
 		JNU_ThrowStringIndexOutOfBoundsException;
 		JNU_ToString;
 
+		Java_java_io_FileDescriptor_cleanupClose0;
 		Java_java_io_FileDescriptor_close0;
 		Java_java_io_FileDescriptor_initIDs;
 		Java_java_io_FileDescriptor_sync;
--- a/test/jdk/java/io/FileInputStream/UnreferencedFISClosesFd.java	Tue Jan 16 20:56:01 2018 +0530
+++ b/test/jdk/java/io/FileInputStream/UnreferencedFISClosesFd.java	Tue Jan 16 10:48:58 2018 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 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
@@ -35,6 +35,8 @@
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.lang.management.OperatingSystemMXBean;
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
@@ -42,6 +44,7 @@
 import java.util.HashSet;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import com.sun.management.UnixOperatingSystemMXBean;
 
 /**
  * Tests for FIS unreferenced.
@@ -136,6 +139,9 @@
 
         String name = inFile.getPath();
 
+        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);
 
@@ -150,6 +156,22 @@
         if (failCount > 0) {
             throw new AssertionError("Failed test count: " + failCount);
         }
+
+        // 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);
+        }
+    }
+
+    // Get the count of open file descriptors, or -1 if not available
+    private static long getFdCount() {
+        OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
+        return  (mxBean instanceof UnixOperatingSystemMXBean)
+                ? ((UnixOperatingSystemMXBean) mxBean).getOpenFileDescriptorCount()
+                : -1L;
     }
 
     private static int test(FileInputStream fis, CleanupType cleanType) throws Exception {
--- a/test/jdk/java/io/FileOutputStream/UnreferencedFOSClosesFd.java	Tue Jan 16 20:56:01 2018 +0530
+++ b/test/jdk/java/io/FileOutputStream/UnreferencedFOSClosesFd.java	Tue Jan 16 10:48:58 2018 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 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
@@ -30,15 +30,22 @@
  * the specification.
  * @run main/othervm UnreferencedFOSClosesFd
  */
-import java.io.*;
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.lang.management.OperatingSystemMXBean;
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
 import java.lang.reflect.Field;
 import java.util.HashSet;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import com.sun.management.UnixOperatingSystemMXBean;
+
 public class UnreferencedFOSClosesFd {
 
     enum CleanupType {
@@ -118,12 +125,17 @@
      */
     public static void main(String argv[]) throws Exception {
 
+
+
         File inFile = new File(System.getProperty("test.dir", "."), FILE_NAME);
         inFile.createNewFile();
         inFile.deleteOnExit();
 
         String name = inFile.getPath();
 
+        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);
 
@@ -138,8 +150,23 @@
         if (failCount > 0) {
             throw new AssertionError("Failed test count: " + failCount);
         }
+
+        // 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);
+        }
     }
 
+    // Get the count of open file descriptors, or -1 if not available
+    private static long getFdCount() {
+        OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
+        return  (mxBean instanceof UnixOperatingSystemMXBean)
+                ? ((UnixOperatingSystemMXBean) mxBean).getOpenFileDescriptorCount()
+                : -1L;
+    }
 
     private static int test(FileOutputStream fos, CleanupType cleanType) throws Exception {
 
--- a/test/jdk/java/io/RandomAccessFile/UnreferencedRAFClosesFd.java	Tue Jan 16 20:56:01 2018 +0530
+++ b/test/jdk/java/io/RandomAccessFile/UnreferencedRAFClosesFd.java	Tue Jan 16 10:48:58 2018 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 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
@@ -23,9 +23,10 @@
 
 import java.io.File;
 import java.io.FileDescriptor;
-import java.io.FileOutputStream;
 import java.io.FileNotFoundException;
 import java.io.RandomAccessFile;
+import java.lang.management.ManagementFactory;
+import java.lang.management.OperatingSystemMXBean;
 import java.lang.ref.Cleaner;
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
@@ -33,6 +34,8 @@
 import java.lang.reflect.Field;
 import java.util.HashSet;
 
+import com.sun.management.UnixOperatingSystemMXBean;
+
 /**
  * @test
  * @bug 8080225
@@ -51,6 +54,9 @@
         inFile.createNewFile();
         inFile.deleteOnExit();
 
+        long fdCount0 = getFdCount();
+        System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
+
         String name = inFile.getPath();
         RandomAccessFile raf;
         try {
@@ -92,5 +98,22 @@
         Reference.reachabilityFence(fd);
         Reference.reachabilityFence(raf);
         Reference.reachabilityFence(pending);
+
+        // 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);
+        }
+    }
+
+
+    // Get the count of open file descriptors, or -1 if not available
+    private static long getFdCount() {
+        OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
+        return  (mxBean instanceof UnixOperatingSystemMXBean)
+                ? ((UnixOperatingSystemMXBean) mxBean).getOpenFileDescriptorCount()
+                : -1L;
     }
 }