From 93e5e0e8a0de6e4c40416c255c6f0de06662a1e7 Mon Sep 17 00:00:00 2001 From: Alexander Zielenski <351783+alexzielenski@users.noreply.github.com> Date: Mon, 29 Aug 2022 12:34:35 -0700 Subject: [PATCH] hold listener lock while waiting for goroutines to finish Kubernetes-commit: 7ce19b75a8dbca12837ed9f4c5c2828c38b82a03 --- tools/cache/shared_informer.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/tools/cache/shared_informer.go b/tools/cache/shared_informer.go index d65dba77..f23d55f1 100644 --- a/tools/cache/shared_informer.go +++ b/tools/cache/shared_informer.go @@ -751,21 +751,18 @@ func (p *sharedProcessor) run(stopCh <-chan struct{}) { }() <-stopCh - func() { - p.listenersLock.Lock() - defer p.listenersLock.Unlock() - for listener := range p.listeners { - close(listener.addCh) // Tell .pop() to stop. .pop() will tell .run() to stop - } + p.listenersLock.Lock() + defer p.listenersLock.Unlock() + for listener := range p.listeners { + close(listener.addCh) // Tell .pop() to stop. .pop() will tell .run() to stop + } - // Wipe out list of listeners since they are now closed - // (processorListener cannot be re-used) - p.listeners = nil + // Wipe out list of listeners since they are now closed + // (processorListener cannot be re-used) + p.listeners = nil - // Reset to false since there are nil listeners, also to block new listeners - // that are added from being run now that the processor was stopped - p.listenersStarted = false - }() + // Reset to false since no listeners are running + p.listenersStarted = false p.wg.Wait() // Wait for all .pop() and .run() to stop } @@ -789,7 +786,6 @@ func (p *sharedProcessor) shouldResync() bool { listener.determineNextResync(now) } } - return resyncNeeded }