From 020a35e85fa52767e0caea28d7707d273329bbaa Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Thu, 14 May 2015 07:57:30 -0400 Subject: [PATCH] Remove file watch upon interruption TestValidatorOverwriteEmailListViaRenameAndReplace was deadlocking on Windows because, on Windows, fsnotify.Watcher will continue to watch a renamed file using its new name. On other systems, it appears the watch on a file is removed after a rename. The fix is to explicitly remove the watch to ensure the watch is resumed under the original name. --- watcher.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/watcher.go b/watcher.go index 4573ddc..d8e073f 100644 --- a/watcher.go +++ b/watcher.go @@ -12,17 +12,18 @@ import ( "gopkg.in/fsnotify.v1" ) -func WaitForReplacement(event fsnotify.Event, watcher *fsnotify.Watcher) { +func WaitForReplacement(filename string, op fsnotify.Op, + watcher *fsnotify.Watcher) { const sleep_interval = 50 * time.Millisecond // Avoid a race when fsnofity.Remove is preceded by fsnotify.Chmod. - if event.Op&fsnotify.Chmod != 0 { + if op&fsnotify.Chmod != 0 { time.Sleep(sleep_interval) } for { - if _, err := os.Stat(event.Name); err == nil { - if err := watcher.Add(event.Name); err == nil { - log.Printf("watching resumed for %s", event.Name) + if _, err := os.Stat(filename); err == nil { + if err := watcher.Add(filename); err == nil { + log.Printf("watching resumed for %s", filename) return } } @@ -52,7 +53,8 @@ func WatchForUpdates(filename string, done <-chan bool, action func()) bool { // can't be opened. if event.Op&(fsnotify.Remove|fsnotify.Rename|fsnotify.Chmod) != 0 { log.Printf("watching interrupted on event: %s", event) - WaitForReplacement(event, watcher) + watcher.Remove(filename) + WaitForReplacement(filename, event.Op, watcher) } log.Printf("reloading after event: %s", event) action()