Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Improve context handling in ClusterCache #11445

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions controllers/clustercache/cluster_accessor_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before we used errors.Wrap and with the wrong context. The result was that if the cache actually never synced. cacheCtx.Err() was nil and then errors.Wrapf also returned nil. This leads to panics down the line

}

// Wrap the cached client with a client that sets timeouts on all Get and List calls
Expand Down
6 changes: 4 additions & 2 deletions controllers/remote/cluster_cache_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down