corpix / uarand

Random user-agent generator for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

uarand is not safe for concurrent use

Sobeston opened this issue · comments

I encountered the following:

panic: runtime error: index out of range
goroutine 373 [running]:math/rand.(*rngSource).Uint64(...)
C:/Go/src/math/rand/rng.go:249
math/rand.(*rngSource).Int63(0xc000093500, 0xc00063e804)
C:/Go/src/math/rand/rng.go:234 +0x89
math/rand.(*Rand).Int63(0xc000072b40, 0xc00063e7e0)
C:/Go/src/math/rand/rand.go:85 +0x3a
math/rand.(*Rand).Int31(0xc000072b40, 0x0)
C:/Go/src/math/rand/rand.go:99 +0x32
math/rand.(*Rand).Int31n(0xc000072b40, 0xc000000336, 0xc000297e38)
C:/Go/src/math/rand/rand.go:134 +0x56
math/rand.(*Rand).Intn(0xc000072b40, 0x336, 0x0)
C:/Go/src/math/rand/rand.go:172 +0x4c
github.com/corpix/uarand.(*UARand).GetRandom(0xc000072b70, 0xc00063e7e0, 0x59)

This is likely the cause:

If you use your own Rand object, you must provide your own locking.
golang/go#3611

Yeah, this is because The default Source is safe for concurrent use by multiple goroutines, but Sources created by NewSource are not. (math/rand)

Ok, I'll add a mutex to GetRandom