Skip to content

Commit

Permalink
fix: data race
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin committed Jan 27, 2022
1 parent 254bf67 commit 95b83ad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
10 changes: 4 additions & 6 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ func RegisterField(fields ...Field) {
registeredFieldsMutex.Lock()
defer registeredFieldsMutex.Unlock()

defer func() {
sort.Slice(registeredFields, func(i, j int) bool {
return registeredFields[i] < registeredFields[j]
})
}()

for _, f := range fields {
var exist bool
for _, existingF := range registeredFields {
Expand All @@ -31,6 +25,10 @@ func RegisterField(fields ...Field) {
registeredFields = append(registeredFields, f)
}
}

sort.Slice(registeredFields, func(i, j int) bool {
return registeredFields[i] < registeredFields[j]
})
}

func UnregisterField(fields ...Field) {
Expand Down
8 changes: 4 additions & 4 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ func ExampleErrorWithStackTrace() {
// github.com/rockbears/log_test.ExampleErrorWithStackTrace
// /Users/fsamin/go/src/github.com/rockbears/log/log_test.go:106
// testing.runExample
// /Users/fsamin/Applications/go/src/testing/run_example.go:63
// /Users/fsamin/Applications/go/src/testing/run_example.go:64
// testing.runExamples
// /Users/fsamin/Applications/go/src/testing/example.go:44
// testing.(*M).Run
// /Users/fsamin/Applications/go/src/testing/testing.go:1419
// /Users/fsamin/Applications/go/src/testing/testing.go:1505
// main.main
// _testmain.go:51
// runtime.main
// /Users/fsamin/Applications/go/src/runtime/proc.go:225
// /Users/fsamin/Applications/go/src/runtime/proc.go:255
// runtime.goexit
// /Users/fsamin/Applications/go/src/runtime/asm_amd64.s:1371] this is an error
// /Users/fsamin/Applications/go/src/runtime/asm_amd64.s:1581] this is an error
}

0 comments on commit 95b83ad

Please sign in to comment.