RFC: Too many race conditions.

David Daney ddaney@avtrex.com
Tue May 22 05:31:00 GMT 2007


Jeroen Frijters wrote:
> David Daney wrote:
>   
>> The only way to close the race window using user space synchronization
>> is to make the close block while you could be calling write().
>>     
>
> That's not true. You can use ref counting to protect access to the fd and then close the fd when the ref count reaches zero. This way the actual native close can happen in any operation (read/write/close) or the finalizer, but is guaranteed only to happen when no other operation is in progress.
>
> This is how .NET 2.0 does it. See http://blogs.msdn.com/cbrumme/archive/2004/02/20/77460.aspx for more information.
>
>   
The semantics of files and pipes change if you don't close them when the 
application asks for them to be closed.  In a pipe for example the read 
side would block waiting for the EOF.  For a file, the filesystem 
containing the file could not be unmounted.  Waiting for a potentially 
blocking I/O operation to finish before closing could cause deadlock.  
Also counting references requires obtaining some type of mutual 
exclusion lock twice for each I/O operation.  This would add overhead to 
the current implementation.

We cannot cause a close operation to block where it currently does not.  
When the close call returns to the caller, we cannot still have the 
resource open.

David Daney

David Daney



More information about the Java mailing list