Skip to content

Commit 46f3dbb

Browse files
authored
Fix mutexes in cache entrypoint (#369)
1 parent 21a9742 commit 46f3dbb

2 files changed

Lines changed: 6 additions & 10 deletions

File tree

pkg/cache/crcache/cache.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ func (c *Cache) OpenRepository(ctx context.Context, repositorySpec *configapi.Re
5656
lock.Lock()
5757
defer lock.Unlock()
5858

59-
c.mainLock.RLock()
59+
c.mainLock.Lock()
60+
defer c.mainLock.Unlock()
61+
6062
if repo, ok := c.repositories[key]; ok && repo != nil {
61-
c.mainLock.RUnlock()
6263
// Keep the spec updated in the cache.
6364
repo.repoSpec = repositorySpec
6465
// Check external repo connectivity
@@ -73,7 +74,6 @@ func (c *Cache) OpenRepository(ctx context.Context, repositorySpec *configapi.Re
7374
}
7475
return repo, nil
7576
}
76-
c.mainLock.RUnlock()
7777

7878
externalRepo, err := externalrepo.CreateRepositoryImpl(ctx, repositorySpec, c.options.ExternalRepoOptions)
7979
if err != nil {
@@ -82,9 +82,7 @@ func (c *Cache) OpenRepository(ctx context.Context, repositorySpec *configapi.Re
8282

8383
cachedRepo := newRepository(key, repositorySpec, externalRepo, c.metadataStore, c.options)
8484

85-
c.mainLock.Lock()
8685
c.repositories[key] = cachedRepo
87-
c.mainLock.Unlock()
8886

8987
return cachedRepo, nil
9088
}

pkg/cache/dbcache/dbcache.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ func (c *dbCache) OpenRepository(ctx context.Context, repositorySpec *configapi.
5252
return nil, err
5353
}
5454

55-
c.mainLock.RLock()
55+
c.mainLock.Lock()
56+
defer c.mainLock.Unlock()
57+
5658
if dbRepo, ok := c.repositories[repoKey]; ok {
57-
c.mainLock.RUnlock()
5859
// Keep the spec updated in the cache.
5960
dbRepo.spec = repositorySpec
6061
err := externalrepo.CheckRepositoryConnection(ctx, dbRepo.spec, c.options.ExternalRepoOptions)
@@ -65,7 +66,6 @@ func (c *dbCache) OpenRepository(ctx context.Context, repositorySpec *configapi.
6566
klog.V(2).Infof("dbCache::OpenRepository: verified repo connectivity %+v", repoKey)
6667
return dbRepo, nil
6768
}
68-
c.mainLock.RUnlock()
6969

7070
dbRepo := &dbRepository{
7171
repoKey: repoKey,
@@ -82,9 +82,7 @@ func (c *dbCache) OpenRepository(ctx context.Context, repositorySpec *configapi.
8282
return nil, err
8383
}
8484

85-
c.mainLock.Lock()
8685
c.repositories[repoKey] = dbRepo
87-
c.mainLock.Unlock()
8886

8987
dbRepo.repositorySync = newRepositorySync(dbRepo, c.options)
9088

0 commit comments

Comments
 (0)