From 9debdbf0284866b5ae423e552177969e68afd947 Mon Sep 17 00:00:00 2001 From: Yahor Yuzefovich Date: Wed, 17 Mar 2021 12:44:00 -0700 Subject: [PATCH] contention: properly protect AddContentionEvent by mutex Whenever we're adding a contention event to the registry, we need to acquire the lock. Previously, we correctly did so for SQL keys but forgot to do so for non-SQL keys (the support for which has been recently added). This commit fixes that issue. Release note: None (no stable release with the bug) --- pkg/sql/contention/registry.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/sql/contention/registry.go b/pkg/sql/contention/registry.go index 5f49755be673..24ce9bff03cd 100644 --- a/pkg/sql/contention/registry.go +++ b/pkg/sql/contention/registry.go @@ -241,6 +241,8 @@ func NewRegistry() *Registry { // AddContentionEvent adds a new ContentionEvent to the Registry. func (r *Registry) AddContentionEvent(c roachpb.ContentionEvent) { + r.globalLock.Lock() + defer r.globalLock.Unlock() _, rawTableID, rawIndexID, err := keys.DecodeTableIDIndexID(c.Key) if err != nil { // The key is not a valid SQL key, so we store it in a separate cache. @@ -254,8 +256,6 @@ func (r *Registry) AddContentionEvent(c roachpb.ContentionEvent) { } tableID := descpb.ID(rawTableID) indexID := descpb.IndexID(rawIndexID) - r.globalLock.Lock() - defer r.globalLock.Unlock() if v, ok := r.indexMap.get(tableID, indexID); !ok { // This is the first contention event seen for the given tableID/indexID // pair.