Skip to content

Commit

Permalink
Merge pull request #99 from kubescape/panic
Browse files Browse the repository at this point in the history
prevent race condition on watcher.stopped
  • Loading branch information
David Wertenteil authored Feb 21, 2024
2 parents 7c2d4b9 + 10daaff commit 77bf3fb
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/registry/file/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package file
import (
"errors"
"strings"
"sync"

"github.com/puzpuzpuz/xsync/v2"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -17,14 +18,20 @@ var (
type watcher struct {
stopped bool
eventCh chan watch.Event
m sync.RWMutex
}

// newWatcher creates a new watcher with a given channel
func newWatcher(wc chan watch.Event) *watcher {
return &watcher{false, wc}
return &watcher{
stopped: false,
eventCh: wc,
}
}

func (w *watcher) Stop() {
w.m.Lock()
defer w.m.Unlock()
w.stopped = true
close(w.eventCh)
}
Expand All @@ -34,6 +41,8 @@ func (w *watcher) ResultChan() <-chan watch.Event {
}

func (w *watcher) notify(e watch.Event) bool {
w.m.RLock()
defer w.m.RUnlock()
if w.stopped {
return false
}
Expand Down

0 comments on commit 77bf3fb

Please sign in to comment.