8194929: Unreferenced FileDescriptors not closed

Reviewed-by: alanb
This commit is contained in:
Roger Riggs 2018-01-16 10:48:58 -05:00
parent 9ed2fdbe3c
commit 99853dbf51
4 changed files with 80 additions and 7 deletions
make/mapfiles/libjava
test/jdk/java/io

@ -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 @@ SUNWprivate_1.1 {
JNU_ThrowStringIndexOutOfBoundsException;
JNU_ToString;
Java_java_io_FileDescriptor_cleanupClose0;
Java_java_io_FileDescriptor_close0;
Java_java_io_FileDescriptor_initIDs;
Java_java_io_FileDescriptor_sync;

@ -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.FileDescriptor;
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.lang.reflect.Field;
import java.util.HashSet;
import java.util.concurrent.atomic.AtomicInteger;
import com.sun.management.UnixOperatingSystemMXBean;
/**
* Tests for FIS unreferenced.
@ -136,6 +139,9 @@ public class UnreferencedFISClosesFd {
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 @@ public class UnreferencedFISClosesFd {
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 {

@ -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 class UnreferencedFOSClosesFd {
*/
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 @@ public class UnreferencedFOSClosesFd {
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 {

@ -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.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.HashSet;
import com.sun.management.UnixOperatingSystemMXBean;
/**
* @test
* @bug 8080225
@ -51,6 +54,9 @@ public class UnreferencedRAFClosesFd {
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 @@ public class UnreferencedRAFClosesFd {
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;
}
}