Skip to content

Commit 806c284

Browse files
authored
Merge pull request #11445 from sbueringer/pr-cc-context-fixes
🐛 Improve context handling in ClusterCache
2 parents 56e5338 + 5a345f1 commit 806c284

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

controllers/clustercache/cluster_accessor_client.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ func createCachedClient(ctx context.Context, clusterAccessorConfig *clusterAcces
226226
return nil, nil, errors.Wrapf(err, "error creating cache")
227227
}
228228

229-
cacheCtx, cacheCtxCancel := context.WithCancel(ctx)
229+
// Use a context that is independent of the passed in context, so the cache doesn't get stopped
230+
// when the passed in context is canceled.
231+
cacheCtx, cacheCtxCancel := context.WithCancel(context.Background())
230232

231233
// We need to be able to stop the cache's shared informers, so wrap this in a stoppableCache.
232234
cache := &stoppableCache{
@@ -263,7 +265,7 @@ func createCachedClient(ctx context.Context, clusterAccessorConfig *clusterAcces
263265
defer cacheSyncCtxCancel()
264266
if !cache.WaitForCacheSync(cacheSyncCtx) {
265267
cache.Stop()
266-
return nil, nil, errors.Wrapf(cacheCtx.Err(), "error when waiting for cache to sync")
268+
return nil, nil, fmt.Errorf("error when waiting for cache to sync: %w", cacheSyncCtx.Err())
267269
}
268270

269271
// Wrap the cached client with a client that sets timeouts on all Get and List calls

controllers/remote/cluster_cache_tracker.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,9 @@ func (t *ClusterCacheTracker) createCachedClient(ctx context.Context, config *re
512512
return nil, errors.Wrapf(err, "error creating cached client for remote cluster %q: error creating cache", cluster.String())
513513
}
514514

515-
cacheCtx, cacheCtxCancel := context.WithCancel(ctx)
515+
// Use a context that is independent of the passed in context, so the cache doesn't get stopped
516+
// when the passed in context is canceled.
517+
cacheCtx, cacheCtxCancel := context.WithCancel(context.Background())
516518

517519
// We need to be able to stop the cache's shared informers, so wrap this in a stoppableCache.
518520
cache := &stoppableCache{
@@ -549,7 +551,7 @@ func (t *ClusterCacheTracker) createCachedClient(ctx context.Context, config *re
549551
defer cacheSyncCtxCancel()
550552
if !cache.WaitForCacheSync(cacheSyncCtx) {
551553
cache.Stop()
552-
return nil, fmt.Errorf("failed waiting for cache for remote cluster %v to sync: %w", cluster, cacheCtx.Err())
554+
return nil, fmt.Errorf("failed waiting for cache for remote cluster %v to sync: %w", cluster, cacheSyncCtx.Err())
553555
}
554556

555557
// Wrap the cached client with a client that sets timeouts on all Get and List calls

0 commit comments

Comments
 (0)