Skip to content

Commit

Permalink
Take account for the fact the Closed function can be called more than…
Browse files Browse the repository at this point in the history
… once.
  • Loading branch information
kennyaz committed Jun 8, 2023
1 parent c85efaf commit 732fc0c
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/fswatcher/fswatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type FSWatcher struct {
onChange func()
mu sync.RWMutex
stop chan struct{}
isClosed bool
wg sync.WaitGroup
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}

Expand Down

0 comments on commit 732fc0c

Please sign in to comment.