controller/podautoscaler: Improve goroutine mgmt

Make sure all threads are terminated when Run returns.
This commit is contained in:
Ondra Kupka
2025-10-27 14:56:40 +01:00
parent 51ef94c547
commit 5f423d7ba8
2 changed files with 17 additions and 6 deletions

View File

@@ -199,20 +199,26 @@ func NewHorizontalController(
// Run begins watching and syncing.
func (a *HorizontalController) Run(ctx context.Context, workers int) {
defer utilruntime.HandleCrash()
defer a.queue.ShutDown()
logger := klog.FromContext(ctx)
logger.Info("Starting HPA controller")
defer logger.Info("Shutting down HPA controller")
var wg sync.WaitGroup
defer func() {
logger.Info("Shutting down HPA controller")
a.queue.ShutDown()
wg.Wait()
}()
if !cache.WaitForNamedCacheSyncWithContext(ctx, a.hpaListerSynced, a.podListerSynced) {
return
}
for i := 0; i < workers; i++ {
go wait.UntilWithContext(ctx, a.worker, time.Second)
wg.Go(func() {
wait.UntilWithContext(ctx, a.worker, time.Second)
})
}
<-ctx.Done()
}

View File

@@ -825,9 +825,14 @@ func coolCPUCreationTime() metav1.Time {
func (tc *testCase) runTestWithController(t *testing.T, hpaController *HorizontalController, informerFactory informers.SharedInformerFactory) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
informerFactory.Start(ctx.Done())
go hpaController.Run(ctx, 5)
var wg sync.WaitGroup
wg.Go(func() {
hpaController.Run(ctx, 5)
})
defer wg.Wait()
defer cancel()
tc.Lock()
shouldWait := tc.verifyEvents