privateRand is not safe for concurrent use
andy opened this issue · 3 comments
andy commented
It should be either wrapped in a lock or global rand package object must be used. Quoting rand#NewSource documentation: https://golang.org/pkg/math/rand/#NewSource
NewSource returns a new pseudo-random Source seeded with the given value. Unlike the default Source used by top-level functions, this source is not safe for concurrent use by multiple goroutines.
andy commented
Here is a sample of what go builtin race detector reports:
Read at 0x00c0001c2000 by goroutine 251:
math/rand.(*rngSource).Int63()
/usr/local/Cellar/go/1.12/libexec/src/math/rand/rng.go:239 +0x3f
math/rand.(*Rand).Int31n()
/usr/local/Cellar/go/1.12/libexec/src/math/rand/rand.go:85 +0x84
math/rand.(*Rand).Intn()
/usr/local/Cellar/go/1.12/libexec/src/math/rand/rand.go:172 +0x52
.../vendor/github.com/Pallinder/go-randomdata.FirstName()
.../vendor/github.com/Pallinder/go-randomdata/random_data.go:88 +0x19e
.../vendor/github.com/Pallinder/go-randomdata.FirstName()
.../vendor/github.com/Pallinder/go-randomdata/random_data.go:119 +0x13f
Previous write at 0x00c0001c2000 by goroutine 250:
math/rand.(*rngSource).Int63()
/usr/local/Cellar/go/1.12/libexec/src/math/rand/rng.go:239 +0x55
math/rand.(*Rand).Int31n()
/usr/local/Cellar/go/1.12/libexec/src/math/rand/rand.go:85 +0x84
math/rand.(*Rand).Intn()
/usr/local/Cellar/go/1.12/libexec/src/math/rand/rand.go:172 +0x52
.../vendor/github.com/Pallinder/go-randomdata.FirstName()
.../vendor/github.com/Pallinder/go-randomdata/random_data.go:88 +0x19e
.../vendor/github.com/Pallinder/go-randomdata.FirstName()
.../vendor/github.com/Pallinder/go-randomdata/random_data.go:119 +0x13f
Pallinder commented
Thanks for this, would love a PR if you have one so we can get this fixed in a release.
Pallinder commented
Released in v1.2.0