JDK-4676183 : deleteOnExit should *not* be used for file-locking
  • Type: Bug
  • Component: docs
  • Sub-Component: guides
  • Affected Version: 1.4.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2002-04-28
  • Updated: 2002-05-09
  • Resolved: 2002-05-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.1 betaFixed
Description
The documentation for File.createNewFile say "This method, in combination with the deleteOnExit() method, can therefore serve as the basis for a simple but reliable cooperative file-locking protocol."  This statement is false and dangerously misguided.

If you mark the file as deleteOnExit *before* invoking createNewFile but the file already exists, you run the risk of deleting a file you didn't create and dropping someone elses lock!  On the other hand, if you mark the file *after* creating it, you lose atomicity: if the program exits before the file is marked, it won't get deleted and the lock will be "wedged".

Luckily, a proper file-locking facility was added in release 1.4 (java.nio.channels Class FileLock).  The disinformative notation on File.createNewFile should be replaced by something like this:

"Note: this method should <i>not</i> be used for file-locking, as the resulting protocol cannot be made to work reliably.  The {@link java.nio.channels.FileLock FileLock} facility should be used instead."

Perhaps this note should be added to the doc comment for File.deleteOnExit as well.

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: hopper-beta FIXED IN: hopper-beta INTEGRATED IN: hopper-beta
14-06-2004

EVALUATION Simple fix. Will add note to both methods as mentioned in the bug report. ###@###.### 2002-05-06
06-05-2002