United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4676183 deleteOnExit should *not* be used for file-locking
JDK-4676183 : deleteOnExit should *not* be used for file-locking

Details
Type:
Bug
Submit Date:
2002-04-28
Status:
Resolved
Updated Date:
2002-05-09
Project Name:
JDK
Resolved Date:
2002-05-09
Component:
docs
OS:
generic
Sub-Component:
guides
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
1.4.0
Fixed Versions:
1.4.1 (beta)

Related Reports

Sub Tasks

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
EVALUATION

Simple fix. Will add note to both methods as mentioned in the bug report.

###@###.### 2002-05-06
                                     
2002-05-06
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
hopper-beta

FIXED IN:
hopper-beta

INTEGRATED IN:
hopper-beta


                                     
2004-06-14



Hardware and Software, Engineered to Work Together