Skip to content

Signal the watch key when its watched directory is deleted - #524

Closed
tanvir-ux wants to merge 1 commit into
google:masterfrom
tanvir-ux:fix-deleted-watched-dir-signal
Closed

tanvir-ux wants to merge 1 commit into
google:masterfrom
tanvir-ux:fix-deleted-watched-dir-signal

Conversation

@tanvir-ux

Copy link
Copy Markdown
Contributor

Fixes #72.

When a directory registered with a Jimfs WatchService is deleted, the polling task fails to snapshot it and cancels the key, but it never queues the key. A thread blocked in take() therefore keeps waiting forever and never learns that the key became invalid.

The JDK services handle this case by cancelling and then signalling the key (sun.nio.fs.PollingWatchService does cancel(); signal(); when the directory is no longer accessible, and LinuxWatchService does the same on IN_IGNORED). On the default file system on Linux, poll() returns the key after the directory is deleted, with isValid() == false and reset() == false.

This change makes Jimfs do the same thing by calling key.signal() right after key.cancel() in the polling task's IOException branch. close() still cancels keys without queueing them, so its behavior is unchanged.

Testing:

  • Added PollingWatchServiceTest.testDeletingWatchedDirectoryCancelsAndSignalsKey. It registers a directory, deletes it, and expects poll(1, SECONDS) to return the same key, now invalid.
    • Before the fix it fails: expected specific instance: ...Key@... but was: null.
    • After the fix, PollingWatchServiceTest passes 9/9.
  • Full jimfs module: mvn test passes 5906 tests (1 skipped), JDK 21.

PollingWatchService cancelled the key when the directory could no longer be
snapshotted (for example after it was deleted), but never queued it, so a
thread blocked in WatchService.take() was never woken up and could not find
out that the key was invalid. Signal the key after cancelling it, as the
JDK's own polling and Linux watch services do.

Fixes google#72
@cpovirk cpovirk self-assigned this Oct 6, 2026
@cpovirk cpovirk added type=defect Bug, not working as expected P2 labels Oct 6, 2026
@copybara-service copybara-service Bot closed this in b014510 Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 type=defect Bug, not working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WatchService registered directory gets deleted, but nothing happens.

2 participants