United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6593729 After failed file close, do not repeat the close operation.
JDK-6593729 : After failed file close, do not repeat the close operation.

Details
Type:
Bug
Submit Date:
2007-08-16
Status:
Resolved
Updated Date:
2010-12-07
Project Name:
JDK
Resolved Date:
2007-09-26
Component:
core-libs
OS:
generic
Sub-Component:
java.io
CPU:
generic
Priority:
P2
Resolution:
Fixed
Affected Versions:
5.0u11
Fixed Versions:
5.0u14 (b02)

Related Reports
Backport:
Backport:

Sub Tasks

Description
When a close() operation on an OS file descriptor is succesful, the record of the FD is set to -1, and on subsequent close() operations the actual call to the OS' close() is not performed.

If an error occurs in the OS' close(), we do not set our record of the FD to -1, so subsequent calls will re-attempt the close().

We should not re-attempt the close().  

If the underlying OS library/syscall close() fails, e.g. Stale NFS Handle, then this is a permanent problem.  Also, the fd number may be available for reuse by other operations allocating file descriptors.  

A customer testcase has demonstrated a FileInputStream.close() throwing an exception due to "Stale NFS Handle", and subsequent calls to close() giving "Bad File Handle".

If the fd is considered closed by the OS, the number could get reused: so it could become valid.  We should mark the fd as -1 as soon as close() fails, and not retry the close again - or we risk closing a vaild fd created by some other thread.

[ removed my own comment that we could retry after ENOSPC: this can happen over NFS, but the fd does become invalid after the failed close - we should not retry the close() ]

EINTR is also possible, but we already restart in that case.

                                    

Comments
SUGGESTED FIX

src/solaris/native/java/io/

$ sccs diffs -C io_util_md.c

------- io_util_md.c -------
*** /tmp/sccs.eMaWGZ    Wed Aug 22 02:35:47 2007
--- io_util_md.c        Wed Aug 22 02:28:08 2007
***************
*** 54,64 ****
              close(devnull);
        }
      } else if (fd != -1) {
!         if (JVM_Close(fd) == -1) {
              JNU_ThrowIOExceptionWithLastError(env, "close failed");
-       } else {
-             SET_FD(this, -1, fid);
-         }
      }
  }

--- 54,62 ----
              close(devnull);
        }
      } else if (fd != -1) {
!         SET_FD(this, -1, fid);
!         if (JVM_Close(fd) == -1)
              JNU_ThrowIOExceptionWithLastError(env, "close failed");
      }
  }

[ editing my earlier suggestion ]

Similarly for Windows.
                                     
2007-08-16
EVALUATION

As description and suggested fix.
                                     
2007-08-23



Hardware and Software, Engineered to Work Together