From 5a345f1822f3a9ce989f3d8b6e8e16cf3c8d402f Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Tue, 19 Nov 2024 15:30:56 +0100 Subject: [PATCH] Improve context handling in ClusterCache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stefan Büringer buringerst@vmware.com --- controllers/clustercache/cluster_accessor_client.go | 6 ++++-- controllers/remote/cluster_cache_tracker.go | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/controllers/clustercache/cluster_accessor_client.go b/controllers/clustercache/cluster_accessor_client.go index b0632e6f5581..55fe61ca6024 100644 --- a/controllers/clustercache/cluster_accessor_client.go +++ b/controllers/clustercache/cluster_accessor_client.go @@ -226,7 +226,9 @@ func createCachedClient(ctx context.Context, clusterAccessorConfig *clusterAcces return nil, nil, errors.Wrapf(err, "error creating cache") } - cacheCtx, cacheCtxCancel := context.WithCancel(ctx) + // Use a context that is independent of the passed in context, so the cache doesn't get stopped + // when the passed in context is canceled. + cacheCtx, cacheCtxCancel := context.WithCancel(context.Background()) // We need to be able to stop the cache's shared informers, so wrap this in a stoppableCache. cache := &stoppableCache{ @@ -263,7 +265,7 @@ func createCachedClient(ctx context.Context, clusterAccessorConfig *clusterAcces defer cacheSyncCtxCancel() if !cache.WaitForCacheSync(cacheSyncCtx) { cache.Stop() - return nil, nil, errors.Wrapf(cacheCtx.Err(), "error when waiting for cache to sync") + return nil, nil, fmt.Errorf("error when waiting for cache to sync: %w", cacheSyncCtx.Err()) } // Wrap the cached client with a client that sets timeouts on all Get and List calls diff --git a/controllers/remote/cluster_cache_tracker.go b/controllers/remote/cluster_cache_tracker.go index 5ed474797a0e..a52687d1525f 100644 --- a/controllers/remote/cluster_cache_tracker.go +++ b/controllers/remote/cluster_cache_tracker.go @@ -512,7 +512,9 @@ func (t *ClusterCacheTracker) createCachedClient(ctx context.Context, config *re return nil, errors.Wrapf(err, "error creating cached client for remote cluster %q: error creating cache", cluster.String()) } - cacheCtx, cacheCtxCancel := context.WithCancel(ctx) + // Use a context that is independent of the passed in context, so the cache doesn't get stopped + // when the passed in context is canceled. + cacheCtx, cacheCtxCancel := context.WithCancel(context.Background()) // We need to be able to stop the cache's shared informers, so wrap this in a stoppableCache. cache := &stoppableCache{ @@ -549,7 +551,7 @@ func (t *ClusterCacheTracker) createCachedClient(ctx context.Context, config *re defer cacheSyncCtxCancel() if !cache.WaitForCacheSync(cacheSyncCtx) { cache.Stop() - return nil, fmt.Errorf("failed waiting for cache for remote cluster %v to sync: %w", cluster, cacheCtx.Err()) + return nil, fmt.Errorf("failed waiting for cache for remote cluster %v to sync: %w", cluster, cacheSyncCtx.Err()) } // Wrap the cached client with a client that sets timeouts on all Get and List calls