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

tapd: fix mutex deadlock in universe cache #1338

Merged
merged 1 commit into from
Jan 29, 2025
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: 3 additions & 3 deletions tapdb/multiverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (b *MultiverseStore) UniverseRootNode(ctx context.Context,
// root node, as that shouldn't happen (unless the cache is empty).
// This will always return nil if the cache is disabled, so we don't
// need an extra indentation for that check here.
rootNode := b.syncerCache.fetchRoot(id)
rootNode := b.syncerCache.fetchRoot(id, false)
if rootNode != nil {
return *rootNode, nil
}
Expand All @@ -256,7 +256,7 @@ func (b *MultiverseStore) UniverseRootNode(ctx context.Context,
// Because another goroutine might have filled the cache while
// we were waiting for the lock, we'll check again if the item
// is now in the cache.
rootNode = b.syncerCache.fetchRoot(id)
rootNode = b.syncerCache.fetchRoot(id, true)
if rootNode != nil {
return *rootNode, nil
}
Expand All @@ -269,7 +269,7 @@ func (b *MultiverseStore) UniverseRootNode(ctx context.Context,
}

// We now try again to fetch the root node from the cache.
rootNode = b.syncerCache.fetchRoot(id)
rootNode = b.syncerCache.fetchRoot(id, true)
if rootNode != nil {
return *rootNode, nil
}
Expand Down
14 changes: 11 additions & 3 deletions tapdb/multiverse_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,21 @@ func (r *syncerRootNodeCache) fetchRoots(q universe.RootNodesQuery,
}

// fetchRoot reads the cached root for the given ID.
func (r *syncerRootNodeCache) fetchRoot(id universe.Identifier) *universe.Root {
func (r *syncerRootNodeCache) fetchRoot(id universe.Identifier,
haveWriteLock bool) *universe.Root {

if !r.enabled {
return nil
}

r.RLock()
defer r.RUnlock()
// If we've acquired the write lock because we're doing a last lookup
// before potentially populating the cache, we don't need to acquire the
// read lock. If we're just normally reading from the cache, we'll need
// to acquire the read lock.
if !haveWriteLock {
r.RLock()
defer r.RUnlock()
}

root, ok := r.universeRoots[id.Key()]
if !ok {
Expand Down
Loading