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

fix: concurrent SlugifyName access (#18369) #18370

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions USERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ Currently, the following organizations are **officially** using Argo CD:
1. [UFirstGroup](https://www.ufirstgroup.com/en/)
1. [ungleich.ch](https://ungleich.ch/)
1. [Unifonic Inc](https://www.unifonic.com/)
1. [Union Inc](https://www.union.ai/)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have a separate PR for this

1. [Universidad Mesoamericana](https://www.umes.edu.gt/)
1. [Upsider Inc.](https://up-sider.com/lp/)
1. [Urbantz](https://urbantz.com/)
Expand Down
6 changes: 6 additions & 0 deletions applicationset/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"regexp"
"sort"
"strings"
"sync"
"text/template"
"unsafe"

Expand All @@ -26,6 +27,7 @@ import (
)

var sprigFuncMap = sprig.GenericFuncMap() // a singleton for better performance
var slugifyNameMutex sync.Mutex

func init() {
// Avoid allowing the user to learn things about the environment.
Expand Down Expand Up @@ -454,6 +456,10 @@ func NormalizeBitbucketBasePath(basePath string) string {
// Returns:
// - string: The generated URL-friendly slug based on the input name and options.
func SlugifyName(args ...interface{}) string {
// Necessary mutex due to global shared state.
// https://github.com/argoproj/argo-cd/issues/18369
slugifyNameMutex.Lock()
defer slugifyNameMutex.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we can defer locking to below the loop

// Default values for arguments
maxSize := 50
EnableSmartTruncate := true
Expand Down
23 changes: 23 additions & 0 deletions applicationset/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"os"
"path"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -1278,6 +1279,28 @@ func TestSlugify(t *testing.T) {
result := SlugifyName(c.length, c.smartTruncate, c.branch)
assert.Equal(t, c.expectedBasePath, result, c.branch)
}

t.Run("Managed concurrent access", func(t *testing.T) {
const numberOfRoutines = 2

for x := 0; x < 100; x++ {
var wg sync.WaitGroup
wg.Add(numberOfRoutines)
for i := 0; i < numberOfRoutines; i++ {
go func(length int) {
defer wg.Done()
// Call the function concurrently
result := SlugifyName(length, false, "test")
if length == 1 {
assert.Equal(t, "t", result)
} else {
assert.Equal(t, "te", result)
}
}(i + 1)
}
wg.Wait()
}
})
}

func TestGetTLSConfig(t *testing.T) {
Expand Down