Skip to content

Commit 020a35e

Browse files
committed
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.
1 parent 5f2df71 commit 020a35e

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

watcher.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ import (
1212
"gopkg.in/fsnotify.v1"
1313
)
1414

15-
func WaitForReplacement(event fsnotify.Event, watcher *fsnotify.Watcher) {
15+
func WaitForReplacement(filename string, op fsnotify.Op,
16+
watcher *fsnotify.Watcher) {
1617
const sleep_interval = 50 * time.Millisecond
1718

1819
// Avoid a race when fsnofity.Remove is preceded by fsnotify.Chmod.
19-
if event.Op&fsnotify.Chmod != 0 {
20+
if op&fsnotify.Chmod != 0 {
2021
time.Sleep(sleep_interval)
2122
}
2223
for {
23-
if _, err := os.Stat(event.Name); err == nil {
24-
if err := watcher.Add(event.Name); err == nil {
25-
log.Printf("watching resumed for %s", event.Name)
24+
if _, err := os.Stat(filename); err == nil {
25+
if err := watcher.Add(filename); err == nil {
26+
log.Printf("watching resumed for %s", filename)
2627
return
2728
}
2829
}
@@ -52,7 +53,8 @@ func WatchForUpdates(filename string, done <-chan bool, action func()) bool {
5253
// can't be opened.
5354
if event.Op&(fsnotify.Remove|fsnotify.Rename|fsnotify.Chmod) != 0 {
5455
log.Printf("watching interrupted on event: %s", event)
55-
WaitForReplacement(event, watcher)
56+
watcher.Remove(filename)
57+
WaitForReplacement(filename, event.Op, watcher)
5658
}
5759
log.Printf("reloading after event: %s", event)
5860
action()

0 commit comments

Comments
 (0)