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

FileDescriptors can sometimes escape being closed by the finalizer

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 7
    • 7
    • core-libs
    • b15
    • generic
    • generic
    • Not verified

      ###@###.###:

      > We have a test program that has 10 threads that exec 1000 processes
      > one at a time, wait for it to terminate then repeat. On linux this
      > quickly encounters IOException because the maximum number of open
      > files is exceeded. The test program is a bad citizen and doesn't
      > explicitly close the process streams.
      >
      > On JDK 1.4.2, 1.5 and 1.6 the test still passes because eventually the
      > finalization of the streams closes the underlying fds.
      >
      > In Java 7 the fds are never reclaimed and the test program goes into
      > an infinite process of throwing IOExceptions.
      >
      > Looking at 6524062 and the code in FileInputStream and FileDescriptor
      > I think I see why. The UNIXProcess class creates FileDescriptor
      > objects and passes them to FIS/FOS constructors. The FileDescriptor
      > constructor sets the useCount to 1. The FIS/FOS constructor increments
      > the useCount to 2. When close() is called during finalization we have
      > the following:
      >
      > int useCount = fd.decrementAndGetUseCount();
      > if ((useCount <= 0) || !isRunningFinalize()) {
      > close0();
      > }
      >
      > We only decremented the useCount *once* so it is still 1, hence we
      > will not call close0() if finalization is active. Hence the fd gets
      > leaked.
      >
      > It seems to me that the FileDescriptor constructor should not be
      > initializing the useCount to 1 as the FD is not yet in use.

            jhangalsunw Jayalaxmi Hangal (Inactive)
            jhangalsunw Jayalaxmi Hangal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: