Skip to content

Commit

Permalink
Prevent potential fsnotify deadlock issue
Browse files Browse the repository at this point in the history
Based on recommendations here: https://github.com/fsnotify/fsnotify#faq

Issue #68
  • Loading branch information
robgonnella committed Jul 11, 2021
1 parent 724dbf9 commit c2eebed
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions v2/core/file_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,40 @@ func (f *FileWatcher) Watch() error {

f.logger.Infof("Watching %s for changes", f.file)

for {
if f.watcher == nil {
f.logger.Debug("file watcher already closed")
return nil
go func() {
for {
if f.watcher == nil {
f.logger.Debug("file watcher already closed")
return nil
}

select {
case event, ok := <-f.watcher.Events:
if !ok {
f.logger.Error("unknown file watch error")
return
}
f.logger.Debugf("event: %+v", event)
if event.Op&fsnotify.Write == fsnotify.Write {
f.logger.Debugf("modified file: %s", event.Name)
for _, l := range f.listeners {
go l()
}
}
case err, ok := <-f.watcher.Errors:
if !ok {
f.logger.Debug("unknown file watch error")
return
}
f.watcher.Close()
f.watcher = nil
f.logger.WithError(err).Error("Watch error")
return
}
}
}()

for {
select {
case <-f.stop:
f.watcher.Remove(f.file)
Expand All @@ -75,27 +103,6 @@ func (f *FileWatcher) Watch() error {
f.watcher.Close()
f.watcher = nil
return nil
case event, ok := <-f.watcher.Events:
if !ok {
f.logger.Error("unknown file watch error")
return nil
}
f.logger.Debugf("event: %+v", event)
if event.Op&fsnotify.Write == fsnotify.Write {
f.logger.Debugf("modified file: %s", event.Name)
for _, l := range f.listeners {
go l()
}
}
case err, ok := <-f.watcher.Errors:
if !ok {
f.logger.Debug("unknown file watch error")
return nil
}
f.watcher.Close()
f.watcher = nil
f.logger.WithError(err).Error("Watch error")
return err
}
}
}
Expand Down

0 comments on commit c2eebed

Please sign in to comment.