Skip to content
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
9 changes: 5 additions & 4 deletions uaparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,12 @@ func New(options ...Option) (*Parser, error) {
}

if parser.RegexDefinitions == nil {
// Clone the cached default so the per-parser mustCompile() writes
// to its own pointer set instead of the shared sync.OnceValue
// slice elements.
regexesDefinitions := defaultRegexesDefinitions()
parser.RegexDefinitions = &regexesDefinitions
}

if parser.config.UseSort {
parser.RegexDefinitions = regexesDefinitions.Clone()
} else if parser.config.UseSort {
parser.RegexDefinitions = parser.RegexDefinitions.Clone()
}

Expand Down
25 changes: 25 additions & 0 deletions uaparser/race_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package uaparser

import (
"sync"
"testing"
)

// TestNewConcurrent_NoRaceOnDefaultRegexDefinitions reproduces the data race
// reported in issue #100. Without the fix, multiple goroutines calling New()
// with the default cached RegexDefinitions racing on the shared []*uaParser
// pointers triggers a race detector failure under -race.
Comment on lines +8 to +11

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

hm, I think our CI doesn't run with -race - perhaps that could be easily added? That would guarantee that we stay race-free much better than relying on contributors to manually run tests with -race. Any chance of adding that to this PR?

func TestNewConcurrent_NoRaceOnDefaultRegexDefinitions(t *testing.T) {
const n = 16
var wg sync.WaitGroup
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
defer wg.Done()
if _, err := New(); err != nil {
t.Errorf("New: %v", err)
}
}()
}
wg.Wait()
}
Loading