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

[v10.0.x] Util: Fix panic when generating UIDs concurrently #69538

Merged
merged 1 commit into from
Jun 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/util/shortid_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"math/rand"
"regexp"
"sync"
"time"

"github.com/google/uuid"
Expand All @@ -12,6 +13,11 @@ var uidrand = rand.New(rand.NewSource(time.Now().UnixNano()))
var alphaRunes = []rune("abcdefghijklmnopqrstuvwxyz")
var hexLetters = []rune("abcdef")

// We want to protect our number generator as they are not thread safe. Not using
// the mutex could result in panics in certain cases where UIDs would be generated
// at the same time.
var mtx sync.Mutex

// Legacy UID pattern
var validUIDPattern = regexp.MustCompile(`^[a-zA-Z0-9\-\_]*$`).MatchString

Expand All @@ -30,6 +36,8 @@ func IsShortUIDTooLong(uid string) bool {
// it is guaranteed to have a character as the first letter
// This UID will be a valid k8s name
func GenerateShortUID() string {
mtx.Lock()
defer mtx.Unlock()
uid, err := uuid.NewRandom()
if err != nil {
// This should never happen... but this seems better than a panic
Expand Down
20 changes: 20 additions & 0 deletions pkg/util/shortid_generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"sync"
"testing"

"github.com/google/uuid"
Expand All @@ -16,6 +17,25 @@ func TestAllowedCharMatchesUidPattern(t *testing.T) {
}
}

// Run with "go test -race -run ^TestThreadSafe$ github.com/grafana/grafana/pkg/util"
func TestThreadSafe(t *testing.T) {
// This test was used to showcase the bug, unfortunately there is
// no way to enable the -race flag programmatically.
t.Skip()
// Use 1000 go routines to create 100 UIDs each at roughly the same time.
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
go func() {
for ii := 0; ii < 100; ii++ {
_ = GenerateShortUID()
}
wg.Done()
}()
wg.Add(1)
}
wg.Wait()
}

func TestRandomUIDs(t *testing.T) {
for i := 0; i < 100; i++ {
v := GenerateShortUID()
Expand Down