mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-14 22:33:34 +00:00
Never leak the etcd watcher's translate goroutine
This commit is contained in:
parent
e09ce3668b
commit
995f022808
@ -100,7 +100,8 @@ type etcdWatcher struct {
|
|||||||
userStop chan struct{}
|
userStop chan struct{}
|
||||||
stopped bool
|
stopped bool
|
||||||
stopLock sync.Mutex
|
stopLock sync.Mutex
|
||||||
// wg is used to avoid calls to etcd after Stop()
|
// wg is used to avoid calls to etcd after Stop(), and to make sure
|
||||||
|
// that the translate goroutine is not leaked.
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
|
|
||||||
// Injectable for testing. Send the event down the outgoing channel.
|
// Injectable for testing. Send the event down the outgoing channel.
|
||||||
@ -146,7 +147,17 @@ func newEtcdWatcher(
|
|||||||
ctx: nil,
|
ctx: nil,
|
||||||
cancel: nil,
|
cancel: nil,
|
||||||
}
|
}
|
||||||
w.emit = func(e watch.Event) { w.outgoing <- e }
|
w.emit = func(e watch.Event) {
|
||||||
|
// Give up on user stop, without this we leak a lot of goroutines in tests.
|
||||||
|
select {
|
||||||
|
case w.outgoing <- e:
|
||||||
|
case <-w.userStop:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// translate will call done. We need to Add() here because otherwise,
|
||||||
|
// if Stop() gets called before translate gets started, there'd be a
|
||||||
|
// problem.
|
||||||
|
w.wg.Add(1)
|
||||||
go w.translate()
|
go w.translate()
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
@ -256,6 +267,7 @@ var (
|
|||||||
// translate pulls stuff from etcd, converts, and pushes out the outgoing channel. Meant to be
|
// translate pulls stuff from etcd, converts, and pushes out the outgoing channel. Meant to be
|
||||||
// called as a goroutine.
|
// called as a goroutine.
|
||||||
func (w *etcdWatcher) translate() {
|
func (w *etcdWatcher) translate() {
|
||||||
|
defer w.wg.Done()
|
||||||
defer close(w.outgoing)
|
defer close(w.outgoing)
|
||||||
defer utilruntime.HandleCrash()
|
defer utilruntime.HandleCrash()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user