bwmarrin / snowflake

A simple to use Go (golang) package to generate or parse Twitter snowflake IDs
BSD 2-Clause "Simplified" License
2.98k stars 371 forks source link

Data race-unsafe use of package-level variables #15

Closed bconway closed 5 years ago

bconway commented 5 years ago

Global package-level variables are being used unsafely from the perspective of the race detector. For example, trying to use NewNode() and Generate() at the same time will trip the race detector for nodeShift in:

snowflake/snowflake.go:97
snowflake/snowflake.go:132

Steps to reproduce:

Other package-level variables besides nodeShift may also affected.

bwmarrin commented 5 years ago

Thanks for the information, I'll review it.

bwmarrin commented 5 years ago

@blocknonip it's been awhile, but I've got some time to care for my github packages :)

I'm working on a solution to this issue and I can see where a data race would happen just by looking at the code. I tried to write a test that would showcase it using t.Parallel() however so far I've been unsuccessful. Any change you can provide an example test that would cause the race detector to complain?

bconway commented 5 years ago

Thanks, looks great!