Skip to content

Commit

Permalink
Merge pull request #4242 from xigang/release-1.5
Browse files Browse the repository at this point in the history
cherry pick of #4212: Fix lock race affects watch RestChan not close, causing client watch api to hang.
  • Loading branch information
karmada-bot committed Nov 15, 2023
2 parents 00b0f54 + 7bb1727 commit bc59377
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions pkg/search/proxy/store/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,21 @@ func (w *watchMux) AddSource(watcher watch.Interface, decorator func(watch.Event

// Start run the watcher
func (w *watchMux) Start() {
for _, source := range w.sources {
go w.startWatchSource(source.watcher, source.decorator)
wg := sync.WaitGroup{}
for i := range w.sources {
source := w.sources[i]
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 @@ -218,7 +230,6 @@ func (w *watchMux) Stop() {
case <-w.done:
default:
close(w.done)
close(w.result)
}
}

Expand All @@ -244,19 +255,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 bc59377

Please sign in to comment.