Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8025619

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

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 1.4-pool, 7u40
    • core-libs
    • b44
    • windows_7
    • Verified

        FULL PRODUCT VERSION :
        java version "1.7.0_40"
        Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
        Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [Version 6.1.7601]

        A DESCRIPTION OF THE PROBLEM :
        FileChannel.tryLock() in some cases throws an IOException wrapped in an Error instead of just an IOException.

        In the example, this happens because the channel is acquired on a closed stream. This can also happen for other reasons, as we have received stack dumps from our users where there is no possibility that the channel was acquired from a (programmatically) closed stream. Those also had the message "the handle is invalid". So this is not just due to a coding error: apparently the handle can be invalid due to some sort of file system problem.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Compile and run example code.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Expect an IOException to be thrown and caught.
        ACTUAL -
        An IOException wrapped in an Error is thrown.

        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        Exception in thread "main" java.lang.Error: java.io.IOException: The handle is invalid
        at sun.nio.ch.FileKey.create(FileKey.java:47)
        at sun.nio.ch.SharedFileLockTable.<init>(FileLockTable.java:120)
        at sun.nio.ch.FileLockTable.newSharedFileLockTable(FileLockTable.java:47)
        at sun.nio.ch.FileChannelImpl.fileLockTable(FileChannelImpl.java:988)
        at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1055)
        at FileChannelBug.main(FileChannelBug.java:14)
        Caused by: java.io.IOException: The handle is invalid
        at sun.nio.ch.FileKey.init(Native Method)
        at sun.nio.ch.FileKey.create(FileKey.java:45)
        ... 5 more


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.io.*;
        import java.nio.channels.FileChannel;
         
        public class FileChannelBug {
         
           public static void main(String[] args) {
              try {
                 File f = File.createTempFile("fcbug", ".tmp");
                 f.deleteOnExit();
                 FileInputStream fis = new FileInputStream(f);
                 fis.close();
                 FileChannel fc = fis.getChannel();
              
                 fc.tryLock(0, Long.MAX_VALUE, true);
              }
              catch (IOException e) {
              }
           }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Catch the Error and if the cause is an IOException, throw the cause, otherwise rethrow the Error.

              bpb Brian Burkhalter
              alanb Alan Bateman
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: