Skip to content

Commit

Permalink
Handle file watcher closed exception (dart-lang/watcher#75)
Browse files Browse the repository at this point in the history
Handle file watcher closed exception on windows
  • Loading branch information
MichaelRFairhurst authored Nov 21, 2019
1 parent b8da234 commit 983431b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions pkgs/watcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.9.7+13

* Catch & forward `FileSystemException` from unexpectedly closed file watchers
on windows; the watcher will also be automatically restarted when this occurs.

# 0.9.7+12

* Catch `FileSystemException` during `existsSync()` on Windows.
Expand Down
20 changes: 16 additions & 4 deletions pkgs/watcher/lib/src/directory_watcher/windows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,22 @@ class _WindowsDirectoryWatcher

/// Start or restart the underlying [Directory.watch] stream.
void _startWatch() {
// Batch the events together so that we can dedup events.
var innerStream = Directory(path).watch(recursive: true);
_watchSubscription = innerStream.listen(_onEvent,
onError: _eventsController.addError, onDone: _onDone);
// Note: "watcher closed" exceptions do not get sent over the stream
// returned by watch, and must be caught via a zone handler.
runZoned(() {
var innerStream = Directory(path).watch(recursive: true);
_watchSubscription = innerStream.listen(_onEvent,
onError: _eventsController.addError, onDone: _onDone);
}, onError: (error, StackTrace stackTrace) {
if (error is FileSystemException &&
error.message.startsWith('Directory watcher closed unexpectedly')) {
_watchSubscription.cancel();
_eventsController.addError(error, stackTrace);
_startWatch();
} else {
throw error;
}
});
}

/// Starts or restarts listing the watched directory to get an initial picture
Expand Down
2 changes: 1 addition & 1 deletion pkgs/watcher/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: watcher
version: 0.9.7+12
version: 0.9.7+13

description: >-
A file system watcher. It monitors changes to contents of directories and
Expand Down

0 comments on commit 983431b

Please sign in to comment.