From 0caae6f704165fdb8efa89af97cbd55c44756fe7 Mon Sep 17 00:00:00 2001 From: Ondra Kupka Date: Wed, 29 Oct 2025 11:48:19 +0100 Subject: [PATCH] controller/volume/pvcprotection: Improve goroutine mgmt Make sure all threads are terminated when Run returns. --- .../pvc_protection_controller.go | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/pkg/controller/volume/pvcprotection/pvc_protection_controller.go b/pkg/controller/volume/pvcprotection/pvc_protection_controller.go index 331fe546efa..526016d1dc0 100644 --- a/pkg/controller/volume/pvcprotection/pvc_protection_controller.go +++ b/pkg/controller/volume/pvcprotection/pvc_protection_controller.go @@ -161,22 +161,30 @@ func NewPVCProtectionController(logger klog.Logger, pvcInformer coreinformers.Pe // Run runs the controller goroutines. func (c *Controller) Run(ctx context.Context, workers int) { defer utilruntime.HandleCrash() - defer c.queue.ShutDown() - defer c.pvcProcessingStore.namespaceQueue.ShutDown() logger := klog.FromContext(ctx) logger.Info("Starting PVC protection controller") - defer logger.Info("Shutting down PVC protection controller") + + var wg sync.WaitGroup + defer func() { + logger.Info("Shutting down PVC protection controller") + c.queue.ShutDown() + c.pvcProcessingStore.namespaceQueue.ShutDown() + wg.Wait() + }() if !cache.WaitForNamedCacheSyncWithContext(ctx, c.pvcListerSynced, c.podListerSynced) { return } - go wait.UntilWithContext(ctx, c.runMainWorker, time.Second) + wg.Go(func() { + wait.UntilWithContext(ctx, c.runMainWorker, time.Second) + }) for i := 0; i < workers; i++ { - go wait.UntilWithContext(ctx, c.runProcessNamespaceWorker, time.Second) + wg.Go(func() { + wait.UntilWithContext(ctx, c.runProcessNamespaceWorker, time.Second) + }) } - <-ctx.Done() }