8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed

Alan Bateman Alan.Bateman at oracle.com
Tue Dec 2 08:59:41 UTC 2014


On 02/12/2014 01:07, Brian Burkhalter wrote:
> :
>
> Perhaps we are at cross-purposes here. The supplied patch was intended 
> to address only the specific failure exposed by the test in the issue 
> description. The last two paragraphs of the review request were in 
> effect asking whether this issue should be constrained to this 
> specific test error and a second issue filed for the more general 
> case, or whether this issue should be used to cover both.
>
I think it would be better to fix the underlying issue, which is that 
FIS/FOS/RAF will create a FileChannel that is open after the file has 
been closed.

Try the patch below as a starting point. This will create the 
FileChannel and close it before the reference is handed out. A cleaner 
solution would be to create the FileChannel in the closed state but it 
would involve more intrusive changes.

-Alan


diff --git a/src/java.base/share/classes/java/io/FileInputStream.java 
b/src/java.base/share/classes/java/io/FileInputStream.java
--- a/src/java.base/share/classes/java/io/FileInputStream.java
+++ b/src/java.base/share/classes/java/io/FileInputStream.java
@@ -367,6 +367,13 @@
          synchronized (this) {
              if (channel == null) {
                  channel = FileChannelImpl.open(fd, path, true, false, 
this);
+                if (closed) {
+                    try {
+                        channel.close();
+                    } catch (IOException ioe) {
+                        throw new InternalError(ioe); // should not happen
+                    }
+                }
              }
              return channel;
          }
diff --git a/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java 
b/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java
--- a/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java
+++ b/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java
@@ -109,6 +109,9 @@
      // -- Standard channel operations --

      protected void implCloseChannel() throws IOException {
+        if (!fd.valid())
+            return; // nothing to do
+
          // Release and invalidate any locks that we still hold
          if (fileLockTable != null) {
              for (FileLock fl: fileLockTable.removeAll()) {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20141202/8904d1ad/attachment.html>


More information about the nio-dev mailing list