Skip to content

Commit b4dd8b8

Browse files
authored
fix: avoid acquiring lock on mutex and semaphore at the same time to prevent deadlock (#521)
* fix: avoid acquiring lock on mutex and semaphore at the same time to prevent deadlock Signed-off-by: Alexander Matyushentsev <[email protected]> * apply reviewer notes Signed-off-by: Alexander Matyushentsev <[email protected]> --------- Signed-off-by: Alexander Matyushentsev <[email protected]>
1 parent ed70eac commit b4dd8b8

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

pkg/cache/cluster.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,8 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo
540540

541541
// load API initial state if no resource version provided
542542
if resourceVersion == "" {
543+
var items []*Resource
543544
resourceVersion, err = c.listResources(ctx, resClient, func(listPager *pager.ListPager) error {
544-
var items []*Resource
545545
err := listPager.EachListItem(ctx, metav1.ListOptions{}, func(obj runtime.Object) error {
546546
if un, ok := obj.(*unstructured.Unstructured); !ok {
547547
return fmt.Errorf("object %s/%s has an unexpected type", un.GroupVersionKind().String(), un.GetName())
@@ -554,16 +554,16 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo
554554
if err != nil {
555555
return fmt.Errorf("failed to load initial state of resource %s: %v", api.GroupKind.String(), err)
556556
}
557-
558-
return runSynced(&c.lock, func() error {
559-
c.replaceResourceCache(api.GroupKind, items, ns)
560-
return nil
561-
})
557+
return nil
562558
})
563559

564560
if err != nil {
565561
return err
566562
}
563+
564+
c.lock.Lock()
565+
c.replaceResourceCache(api.GroupKind, items, ns)
566+
c.lock.Unlock()
567567
}
568568

569569
w, err := watchutil.NewRetryWatcher(resourceVersion, &cache.ListWatch{

0 commit comments

Comments
 (0)