Skip to content

Conversation

@VladyslavG
Copy link
Contributor

@VladyslavG VladyslavG commented Apr 24, 2024

Fixes #1186

On Windows deleting watched directory is firing event error which is caught by NodeWatchService.ensureWatcher:

      directoryWatcher.once("error", (e) => {
        this.onWatchError(e, path);
      });

this in turn causes 2 effects:

  1. watcher.close(); in the onWatchError
  2. debounced (by onPathEvent) this.fsWatchers.delete(path); in the emitEvent method.

If unwatchAllPaths was invoked between these 2 effects we would enter a state when unwatchAllPaths can't finish.
This happened due to

  1. unwatchAllPaths assumes that all this.fsWatchers are not closed
  2. unwatchAllPaths invokes close on each watcher and waits for the close event on all of watchers
    But this can't work because watcher.close(); was called in the onWatchError, so we are left with a method which can't end.

The proposed fix is to remove watcher from this.fsWatchers inside the onWatchError immediately after watcher is closed.

@VladyslavG VladyslavG marked this pull request as ready for review April 24, 2024 10:55
@VladyslavG VladyslavG requested review from AviVahl and alisey April 24, 2024 10:55
Copy link
Contributor

@AviVahl AviVahl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look fine.

On a related note, this API (WatchService) got deprecated and will get removed in a future major. We should use fs.watch with recursive: true instead, with additional watchers for files we want immediate events for.

@alisey alisey merged commit 29b7d7d into main Apr 24, 2024
@alisey alisey deleted the vladg/fix-closing branch April 24, 2024 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: unwatchAllPaths can't finish if invoked immediately after one of watched directories is removed

4 participants