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

FileHandler leaking file descriptor of the file lock

XMLWordPrintable

    • b140
    • generic
    • generic
    • Not verified

      File descriptor leak of the file lock created by FileHandler.

      Steps to reproduce the problem on Ubuntu linux-i586:
      --------------
      import java.util.logging.*;

      public class OpenProcess {
          public static void main(String[] args)
              throws Exception {

              FileHandler handler = new FileHandler("log.info", 1000000, 100, true);
              int counter = 0;
              while (counter++ <= 1000) {
                  Thread.sleep(3000);
              }

              handler.close();
          }
      }

      ------------
      import java.util.logging.*;

      public class AnotherProcess {
          public static void main(String[] args)
              throws Exception {

              while (true) {
                  FileHandler fileHandler = null;
                  try {
                      fileHandler = new FileHandler("log.info", 1000000, 100, true);
                  } finally {
                      if (fileHandler != null) {
                          fileHandler.close();
                      }
                  }
              }
          }
      }

      -------------------

      1. First run OpenProcess.java, which creates a FileHandler and then sleeps.
         Doing so will create and lock a .lck file.
      2. Then start AnotherProcess.java, which creates the same FileHandler in a loop.
         FileHandler.close() is called in the finally clause. This throws an exception:

      Exception in thread "main" java.io.FileNotFoundException: log.info.0.1 (Too many open files)
              at java.io.FileOutputStream.open(Native Method)
              at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
              at java.io.FileOutputStream.<init>(FileOutputStream.java:136)
              at java.util.logging.FileHandler.open(FileHandler.java:173)
              at java.util.logging.FileHandler.openFiles(FileHandler.java:438)
              at java.util.logging.FileHandler.<init>(FileHandler.java:363)
              at AnotherProcess.main(AnotherProcess.java:10)

      ---------------------
      When the file lock is held by another process, fc.tryLock() fails and
      it misses to close it before trying the next file.

                      try {
                          FileLock fl = fc.tryLock();
                          if (fl == null) {
                              // We failed to get the lock. Try next file.
      -----> continue;
                          }
                          // We got the lock OK.
                      } catch (IOException ix) {
                          // We got an IOException while trying to get the lock.
                          // This normally indicates that locking is not supported
                          // on the target directory. We have to proceed without
                          // getting a lock. Drop through.
                      }

            mchung Mandy Chung (Inactive)
            mchung Mandy Chung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: