gofakeit: gofakeit.UUID() is not thread safe
Hi!
I am using gofakeit/v6 in tests a lot, and the majority of tests run in parallel and use gofakeit methods.
With the tests codebase growth, I face race conditions using gofakeit.UUID()
:
Example test to showcase:
package tests
import (
"testing"
"github.com/brianvoe/gofakeit/v6"
)
func TestUUID(t *testing.T) {
for i := 0; i < 10000; i++ {
go func() {
_ = gofakeit.UUID()
}()
}
}
tests % go test --race gofakeit_test.go
==================
WARNING: DATA RACE
Read at 0x00c0000129b0 by goroutine 10:
math/rand.read()
/usr/local/go/src/math/rand/rand.go:269 +0x84
math/rand.(*Rand).Read()
/usr/local/go/src/math/rand/rand.go:264 +0xd5
github.com/brianvoe/gofakeit/v6.uuid()
/home/it/go/pkg/mod/github.com/brianvoe/gofakeit/v6@v6.17.0/misc.go:36 +0x5a
github.com/brianvoe/gofakeit/v6.UUID()
/home/it/go/pkg/mod/github.com/brianvoe/gofakeit/v6@v6.17.0/misc.go:27 +0x4a
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (12 by maintainers)
Commits related to this issue
- fix test based on https://github.com/brianvoe/gofakeit/issues/205 — committed to marle3003/mokapi by marle3003 8 months ago
Ok lets reverse it.
Don’t you think your being a bit lazy. I built this huge random generator for you and if something doesn’t work perfectly the most effort you put into an issue comment is essentially “its not working”.
The whole point of an issue submission for open source projects is to help the community improve that code for everyone. You are the reason so many developers get burnt out. Submit issues with ZERO contributions. Your github account looks like the Sahara Desert.
because don’t have time - but I do? don’t know how to fix it - Ill take that excuse, but at least TRY don’t understand your code - its one function that is 30 lines many reasons - ok
The biggest issue is the zero effort. If you came in and said “Im a junior developer and I don’t understand too much of Go but i could really use your help fixing this”. I would have jumped on that in a heart beat. Or throw me a coffee or something. Make other developers feel like your not just using there code and walking away.
You might have caught me on a bad day but the underlying issue still stands. Ill go fix it and you’ll do nothing.
v6.23.2
@PumpkinSeed @borosr Check out the fix. Let me know if i missed anything https://github.com/brianvoe/gofakeit/compare/v6.23.1...v6.23.2
Some notes regarding this problem.
After I dig into the problem, the main issue that the
math/rand
package’srand.Rand
’s read function is not thread-safe. It happens because of the shared state of the of the memory allocation. As the race detector pointed it out:math/rand.(*Rand).Read()
.There are multiple possible solutions but I think all ends up in using a
sync.Mutex
wrapper around the Read the function. This mutex should be initiated on the same level as therand.Rand
, because that’s the scope of the memory pool what the mutex should be aware of.@danielrmdiass Out of respect to @brianvoe: He did not insult you a single bit. Interpreting “!!!” as an insult is ridiculous. Also there was no demand. More like an active suggestion.
Instead of reacting to his comment and calling it an “overreaction” - you could have proposed to try fixing it. Which you didn’t. Even after a couple of comments you never mentioned a single “thank you” or that you would try to help.
As far as insults go “grow up plz” is disrespectful, belittling and extremely unfair.
If you were serious about whatever you were planning you should have understood the frustration and got along with support. Remember - even if we sometimes feel treated unfair or don’t feel welcome - our reaction is still up to us. And someone might not be meaning whatever you are interpreting or having a bad day. Please work on your attitude dude and start contributing to the community.
Or stay out of it. Good day to us all 😊
What part do you agree on? The insulting or the demanding? SUBMIT A PR!!! I never said I would not help and the issue was already documented. And never gave me the chance to say if I could help or not. Maybe the fact I have no contributions means something. And doesn’t matter if the point of contributing is or not a factor. No point in continue this.
Have a good day!
Thanks @dminkovski. You are the MVP!
Agree 100% with @brianvoe. Anyone that faces an issue should be open to fix it and ask for help if needed. Don’t point out issues either if you are not willing to do anything about it. That’s literally a waste. Simple Open Source Community Rules @danielrmdiass : Found an issue? Document all you can about it as precise as possible so someone who is not in your brain can try to understand it.
Want the issue fixed? Try yourself as much as you can and ask for help once you are stuck after spending a lot of time. Share your progress and what you learned.
Getting an answer you don’t like? Accept it. Someone is putting time and effort into this product for free - so appreciate it and build up tolerance. Stop discussing and learn.
Want some real help? Be willing to put in some nice words, sponsoring or coffee and lots of patience. Open Source Developers are the MVPs of the Dev Scene.
thanks again and have a good day! ❤️
haha do something plz. Do you always just take code and not give back ever? Have some rest if its too hard for you to contribute to other projects and have some reflections on your lack of caring for the open source community.
If you think me telling you to submit a pr if you want something fixed is me demanding anything from you, you are lost in the sauce my friend.
This repo is everyones as shown by the many contributions sent by other people and shocker your not on the list. Or any list.
I love you
Grow up plz. Do you always solve things by insulting? Have some rest if you are burnt out and have some reflection on what you said and look in the mirror. I never demanded for you to fix this. I just pointed out and asked. You did demand for me to fix. I never demanded you to fix this and I sent you a possible solution. The repo is yours, so you do what you want.
Don’t you think that is a bit of an overreaction. I just pointed out the issue to improve your code. It’s literally the same issue pointed out. The whole point of issues is the fact that people report the issue but don’t intent to fix (because don’t have time/don’t know how to fix it/ don’t understand your code/ many reasons).
I can only say that it seems that the access to the global rand is the issue. Either you can add a lock or use a local one. This seems to fix it. Probably not the best solution for your code structure.