Comments (2)
I don't believe uuid.NewV4()
is thread safe. here's the source for NewV4():
func (g *rfc4122Generator) NewV4() (UUID, error) {
u := UUID{}
if _, err := g.rand.Read(u[:]); err != nil {
return Nil, err
}
u.SetVersion(V4)
u.SetVariant(VariantRFC4122)
return u, nil
}
the rfc4122Generator
struct is global. the rand
member (io.Reader
) is not protected by any sort of mutex, and io.Reader
s just read from a raw byte buffer.
you could wrap NewV4()
to be thread safe, something like:
var uuidGenerator sync.Mutex
func NewV4() (uuid.UUID, error) {
uuidGenerator.Lock()
defer uuidGenerator.Unlock()
return uuid.NewV4()
}
although using goroutines to generate UUIDs, perhaps using channels to queue requests & responses is the more idiomatic go way.
from go.uuid.
Globals and thread-safety are only of concern if there are writes happening to the global (or more specifically, to some piece of shared data, globally-scoped-or-not) concurrently with reads. (Ie. a "global" or shared var that's read-only throughout run time after some initial writes at init time is fully benign.) Not seeing such a situation here: a never-EOF'ing read-only rand reader should be "safeish" to a first intuition to begin with. What sort of state might it have to protect, really? But to verify our first intuition, we check the linux/unix/bsd/plan9 impl and lookie, there is seemingly a mutex on the underlying /dev/rand
-consuming io.Reader
anyway! see https://golang.org/src/crypto/rand/rand_unix.go --- so another locking/sync on top of it would buy nothing (but cost extra).
Haven't checked the Win impl, but the stdlib authors should surely be consistent in the locking/synchronization semantics of impls across OSes here. Anyone concerned can skim over the sources just as easily as I demo'd above 😁
from go.uuid.
Related Issues (20)
- assignment mismatch: 2 variables but uuid.NewV4 returns 1 values HOT 4
- uuid.NewV4() - fixed in master, but bugged in all releases HOT 2
- Readme Coverage and GoDoc coverage report different values HOT 1
- PSA: This repo is dead
- assignment mismatch: 2 variables but uuid.NewV4 returns 1 values HOT 6
- release version problem HOT 6
- incompatible code between HEAD of master and tag v1.2.0 HOT 1
- Need new release/tag for vulnerability fix HOT 1
- Why did I get this mistake? HOT 1
- IMPORTANT: Unresolved CVE on latest release (CVE-2021-3538 ) HOT 1
- Create functions that allows go get empty/default uuid HOT 3
- It's time to switch to gitee.com/gofrs/uuid HOT 1
- Can you tag the fix that is in master using ReadFull as 1.2.1? HOT 1
- CVE-2021-3538 HOT 1
- Is it thread-Safe?! HOT 1
- Fix Request: CVE-2021-3538 HOT 2
- Issue using go.uuid in docker container HOT 3
- New Release possible? HOT 4
- could not get new version for go.mod HOT 1
- NewV1() get same result on Windows HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go.uuid.