Skip to content

Commit

Permalink
fix goroutine leak in watchMux
Browse files Browse the repository at this point in the history
fix: fix exec failure with karamada-aggregated-apiserver
Signed-off-by: yingjinhui <yingjinhui@didiglobal.com>
  • Loading branch information
ikaven1024 committed Nov 12, 2023
1 parent 1b2c6ed commit 3c07a34
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions pkg/search/proxy/store/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,21 @@ func (w *watchMux) AddSource(watcher watch.Interface, decorator func(watch.Event

// Start run the watcher
func (w *watchMux) Start() {
wg := sync.WaitGroup{}
for _, source := range w.sources {
go w.startWatchSource(source.watcher, source.decorator)
source := source
wg.Add(1)
go func() {
defer wg.Done()
w.startWatchSource(source.watcher, source.decorator)
}()
}

go func() {
// close result chan after all goroutines exit, avoiding data race.
defer close(w.result)
wg.Wait()
}()
}

// ResultChan implements watch.Interface
Expand All @@ -220,7 +232,6 @@ func (w *watchMux) Stop() {
case <-w.done:
default:
close(w.done)
close(w.result)
}
}

Expand All @@ -246,19 +257,8 @@ func (w *watchMux) startWatchSource(source watch.Interface, decorator func(watch
select {
case <-w.done:
return
default:
case w.result <- copyEvent:
}

func() {
w.lock.RLock()
defer w.lock.RUnlock()
select {
case <-w.done:
return
default:
w.result <- copyEvent
}
}()
}
}

Expand Down

0 comments on commit 3c07a34

Please sign in to comment.