8029516: (fs) WatchKey cancel unreliable on Windows

Alan Bateman Alan.Bateman at oracle.com
Thu Sep 4 16:58:50 UTC 2014

This is a bug is the Windows implementation of WatchService.

The Windows implementation is based on ReadDirectoryChangesW. It is used 
asynchronously with a background thread, called the Poller, to service 
the events as they are queued to an I/O completion port. It's one thread 
per WatchService and there can be many directories registered with the 
same WatchService.

The cancel method is tricky. It's done by the Poller thread on behalf of 
the caller of cancel and works by closing the handle to the directory. 
Unfortunately this isn't quite right as it causes the outstanding 
ReadDirectoryChangesW operation to abort and attempt to set an event in 
the OVERLAPPED structure that might be freed at around that point

To fix this requires the Poller to cancel the outstanding 
ReadDirectoryChangesW and wait for it to complete before close the 
directory and releasing the resources. It unfortunately requires an 
additional (unnamed) event per watched directory but overall the changes 
aren't too bad. I've used the opportunity to clean up a few small things 
in passing.

The awkward part is creating a test that is anyway useful, it is very 
machine and load dependent. The test that I have included will duplicate 
it periodically when running on a loaded system, say when running tests 
concurrently. All the existing tests pass on Windows 32-bit and 64-bit.

The webrev with the changes is here:


More information about the nio-dev mailing list