< prev index next >

test/jdk/java/io/RandomAccessFile/UnreferencedRAFClosesFd.java

Print this page
rev 50699 : 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.FileDescriptor;
  26 import java.io.FileNotFoundException;

  27 import java.io.RandomAccessFile;
  28 import java.lang.management.ManagementFactory;
  29 import java.lang.management.OperatingSystemMXBean;
  30 import java.lang.ref.Cleaner;
  31 import java.lang.ref.Reference;
  32 import java.lang.ref.ReferenceQueue;
  33 import java.lang.ref.WeakReference;
  34 import java.lang.reflect.Field;


  35 import java.util.HashSet;
  36 
  37 import com.sun.management.UnixOperatingSystemMXBean;
  38 


  39 /**
  40  * @test
  41  * @bug 8080225


  42  * @modules java.base/java.io:open
  43  * @summary Test to ensure that an unclosed and unreferenced RandomAccessFile closes the fd
  44  * @run main/othervm UnreferencedRAFClosesFd
  45  */
  46 public class UnreferencedRAFClosesFd {
  47 
  48     static final String FILE_NAME = "empty.txt";
  49 
  50     /* standalone interface */
  51     public static void main(String argv[]) throws Exception {
  52 
  53         File inFile= new File(System.getProperty("test.dir", "."), FILE_NAME);
  54         inFile.createNewFile();
  55         inFile.deleteOnExit();
  56 

  57         long fdCount0 = getFdCount();
  58         System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
  59 
  60         String name = inFile.getPath();
  61         RandomAccessFile raf;
  62         try {
  63             // raf is explicitly *not* closed to allow cleaner to work
  64             raf = new RandomAccessFile(name, "rw");
  65         } catch (FileNotFoundException e) {
  66             System.out.println("Unexpected exception " + e);
  67             throw(e);
  68         }
  69         FileDescriptor fd = raf.getFD();
  70 
  71         Field fdField = FileDescriptor.class.getDeclaredField("cleanup");
  72         fdField.setAccessible(true);
  73         Cleaner.Cleanable cleanup = (Cleaner.Cleanable)fdField.get(fd);
  74 
  75         // Prepare to wait for FOS, FD, Cleanup to be reclaimed
  76         ReferenceQueue<Object> queue = new ReferenceQueue<>();
  77         HashSet<Reference<?>> pending = new HashSet<>(3);
  78         pending.add(new WeakReference<>(cleanup, queue));


  84                 || !pending.isEmpty()) {
  85             System.out.printf("r: %s, pending: %d%n", r, pending.size());
  86             if (r != null) {
  87                 pending.remove(r);
  88             } else {
  89                 cleanup = null;
  90                 raf = null;
  91                 fd = null;
  92                 System.gc();  // attempt to reclaim the RAF, cleanup, and fd
  93             }
  94         }
  95 
  96         // Keep these variables in scope as gc roots
  97         Reference.reachabilityFence(cleanup);
  98         Reference.reachabilityFence(fd);
  99         Reference.reachabilityFence(raf);
 100         Reference.reachabilityFence(pending);
 101 
 102         // Check the final count of open file descriptors
 103         long fdCount = getFdCount();
 104         System.out.printf("final count of open file descriptors: %d%n", fdCount);
 105         if (fdCount != fdCount0) {
 106             throw new AssertionError("raw fd count wrong: expected: " + fdCount0
 107                     + ", actual: " + fdCount);

 108         }
 109     }
 110 
 111 
 112     // Get the count of open file descriptors, or -1 if not available
 113     private static long getFdCount() {
 114         OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
 115         return  (mxBean instanceof UnixOperatingSystemMXBean)
 116                 ? ((UnixOperatingSystemMXBean) mxBean).getOpenFileDescriptorCount()
 117                 : -1L;
 118     }
 119 }


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.FileDescriptor;
  26 import java.io.FileNotFoundException;
  27 import java.io.IOException;
  28 import java.io.RandomAccessFile;
  29 import java.lang.management.ManagementFactory;
  30 import java.lang.management.OperatingSystemMXBean;
  31 import java.lang.ref.Cleaner;
  32 import java.lang.ref.Reference;
  33 import java.lang.ref.ReferenceQueue;
  34 import java.lang.ref.WeakReference;
  35 import java.lang.reflect.Field;
  36 import java.nio.file.Path;
  37 import java.util.ArrayDeque;
  38 import java.util.HashSet;
  39 
  40 import com.sun.management.UnixOperatingSystemMXBean;
  41 
  42 import jdk.test.lib.util.FileUtils;
  43 
  44 /**
  45  * @test
  46  * @bug 8080225
  47  * @library /test/lib
  48  * @build jdk.test.lib.util.FileUtils UnreferencedRAFClosesFd
  49  * @modules java.base/java.io:open
  50  * @summary Test to ensure that an unclosed and unreferenced RandomAccessFile closes the fd
  51  * @run main/othervm UnreferencedRAFClosesFd
  52  */
  53 public class UnreferencedRAFClosesFd {
  54 
  55     static final String FILE_NAME = "empty.txt";
  56 
  57     /* standalone interface */
  58     public static void main(String argv[]) throws Exception {
  59 
  60         File inFile= new File(System.getProperty("test.dir", "."), FILE_NAME);
  61         inFile.createNewFile();
  62         inFile.deleteOnExit();
  63 
  64         FileUtils.listFileDescriptors(System.out);
  65         long fdCount0 = getFdCount();

  66 
  67         String name = inFile.getPath();
  68         RandomAccessFile raf;
  69         try {
  70             // raf is explicitly *not* closed to allow cleaner to work
  71             raf = new RandomAccessFile(name, "rw");
  72         } catch (FileNotFoundException e) {
  73             System.out.println("Unexpected exception " + e);
  74             throw(e);
  75         }
  76         FileDescriptor fd = raf.getFD();
  77 
  78         Field fdField = FileDescriptor.class.getDeclaredField("cleanup");
  79         fdField.setAccessible(true);
  80         Cleaner.Cleanable cleanup = (Cleaner.Cleanable)fdField.get(fd);
  81 
  82         // Prepare to wait for FOS, FD, Cleanup to be reclaimed
  83         ReferenceQueue<Object> queue = new ReferenceQueue<>();
  84         HashSet<Reference<?>> pending = new HashSet<>(3);
  85         pending.add(new WeakReference<>(cleanup, queue));


  91                 || !pending.isEmpty()) {
  92             System.out.printf("r: %s, pending: %d%n", r, pending.size());
  93             if (r != null) {
  94                 pending.remove(r);
  95             } else {
  96                 cleanup = null;
  97                 raf = null;
  98                 fd = null;
  99                 System.gc();  // attempt to reclaim the RAF, cleanup, and fd
 100             }
 101         }
 102 
 103         // Keep these variables in scope as gc roots
 104         Reference.reachabilityFence(cleanup);
 105         Reference.reachabilityFence(fd);
 106         Reference.reachabilityFence(raf);
 107         Reference.reachabilityFence(pending);
 108 
 109         // Check the final count of open file descriptors
 110         long fdCount = getFdCount();

 111         if (fdCount != fdCount0) {
 112             System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
 113             System.out.printf("final count of open file descriptors: %d%n", fdCount);
 114             FileUtils.listFileDescriptors(System.out);
 115         }
 116     }
 117 
 118 
 119     // Get the count of open file descriptors, or -1 if not available
 120     private static long getFdCount() {
 121         OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
 122         return  (mxBean instanceof UnixOperatingSystemMXBean)
 123                 ? ((UnixOperatingSystemMXBean) mxBean).getOpenFileDescriptorCount()
 124                 : -1L;
 125     }
 126 }
< prev index next >