Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(383)

Unified Diff: lib/src/file_watcher/native.dart

Issue 1228703007: Fix a race condition in file watcher. (Closed) Base URL: git@github.com:dart-lang/watcher@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/src/file_watcher/native.dart
diff --git a/lib/src/file_watcher/native.dart b/lib/src/file_watcher/native.dart
index f5699bbb4162b0efdd3285bdc1b97555cead8d55..1862e7b05cdd259311681d0c19a0a6564cd73a1e 100644
--- a/lib/src/file_watcher/native.dart
+++ b/lib/src/file_watcher/native.dart
@@ -60,10 +60,17 @@ class _NativeFileWatcher implements FileWatcher, ManuallyClosedWatcher {
}
_onDone() async {
- // If the file exists now, it was probably removed and quickly replaced;
- // this can happen for example when another file is moved on top of it.
- // Re-subscribe and report a modify event.
- if (await new File(path).exists()) {
+ var fileExists = await new File(path).exists();
+
+ // Check for this after checking whether the file exists because it's
+ // possible that [close] was called between [File.exists] being called and
+ // it completing.
+ if (_eventsController.isClosed) return;
+
+ if (fileExists) {
+ // If the file exists now, it was probably removed and quickly replaced;
+ // this can happen for example when another file is moved on top of it.
+ // Re-subscribe and report a modify event.
_eventsController.add(new WatchEvent(ChangeType.MODIFY, path));
_listen();
} else {

Powered by Google App Engine
This is Rietveld 408576698