-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix data race with multiple map writers
When the client is initialized with params, the normalizeUser function will concurrently write the params map. This causes the go runtime to panic saying "fatal error: concurrent map writes". This change fixes the data race by always copying the map when normalizing the user.
- Loading branch information
Showing
2 changed files
with
38 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package statsig | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNormalizeUserDataRace(t *testing.T) { | ||
const ( | ||
goroutines = 10 | ||
duration = time.Second | ||
) | ||
options := Options{ | ||
Environment: Environment{ | ||
Params: map[string]string{ | ||
"foo": "bar", | ||
}, | ||
Tier: "awesome", | ||
}, | ||
} | ||
start := time.Now() | ||
var wg sync.WaitGroup | ||
wg.Add(goroutines) | ||
for g := 0; g < goroutines; g++ { | ||
go func() { | ||
defer wg.Done() | ||
for time.Since(start) < duration { | ||
normalizeUser(User{UserID: "cruise-llc"}, options) | ||
} | ||
}() | ||
} | ||
wg.Wait() | ||
} |