diff --git a/pkg/fswatcher/fswatcher.go b/pkg/fswatcher/fswatcher.go index 41e87aadb6c..7588ed3aba7 100644 --- a/pkg/fswatcher/fswatcher.go +++ b/pkg/fswatcher/fswatcher.go @@ -34,6 +34,7 @@ type FSWatcher struct { onChange func() mu sync.RWMutex stop chan struct{} + isClosed bool wg sync.WaitGroup } @@ -70,6 +71,7 @@ func New(filepaths []string, onChange func(), logger *zap.Logger) (*FSWatcher, e fileHashContentMap: make(map[string]string), onChange: onChange, stop: make(chan struct{}), + isClosed: false, } if err = w.setupWatchedPaths(filepaths); err != nil { @@ -146,8 +148,11 @@ func (w *FSWatcher) watch() { // Close closes the watcher and stops the background go routine of FSWatcher. func (w *FSWatcher) Close() error { - close(w.stop) - w.wg.Wait() + if !w.isClosed { + close(w.stop) + w.wg.Wait() + } + w.isClosed = true return w.watcher.Close() }