mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
pkg/watch: fix potential deadlock
This commit is contained in:
parent
24b5b7e8d3
commit
435e0b73bb
@ -57,6 +57,7 @@ func (m *Mux) Watch() Interface {
|
||||
m.nextWatcher++
|
||||
w := &muxWatcher{
|
||||
result: make(chan Event),
|
||||
stopped: make(chan struct{}),
|
||||
id: id,
|
||||
m: m,
|
||||
}
|
||||
@ -119,13 +120,18 @@ func (m *Mux) distribute(event Event) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
for _, w := range m.watchers {
|
||||
w.result <- event
|
||||
select {
|
||||
case w.result <- event:
|
||||
case <-w.stopped:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// muxWatcher handles a single watcher of a mux
|
||||
type muxWatcher struct {
|
||||
result chan Event
|
||||
stopped chan struct{}
|
||||
stop sync.Once
|
||||
id int64
|
||||
m *Mux
|
||||
}
|
||||
@ -137,5 +143,8 @@ func (mw *muxWatcher) ResultChan() <-chan Event {
|
||||
|
||||
// Stop stops watching and removes mw from its list.
|
||||
func (mw *muxWatcher) Stop() {
|
||||
mw.stop.Do(func() {
|
||||
close(mw.stopped)
|
||||
mw.m.stopWatching(mw.id)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user