Commit 9c073bb
fix(random): prevent UniqueID collisions from per-call time seeding (#1860)
UniqueID (and Random/RandomString/RandomInt via newRand) created a fresh *rand.Rand seeded with time.Now().UnixNano() on every call. When invoked in a tight loop or in parallel on machines with coarse timer resolution (e.g. Windows), consecutive calls observe the same timestamp, get the same seed, produce the same sequence, and return identical 'unique' IDs.
This leads to duplicate resource names / tfstate keys and flaky parallel tests. TestUniqueID reproduces it reliably on Windows.
Use the process-global, auto-seeded (Go 1.20+) and concurrency-safe math/rand source instead. This removes the collision source and also avoids allocating a new generator on every call. No public API changes; output remains non-deterministic as before (the previous code seeded from the wall clock, so it was never deterministic either).
Amp-Thread-ID: https://ampcode.com/threads/T-019f32a2-bbac-752b-bbf2-2ee88229b925
Co-authored-by: zuwasi <zuwasi@users.noreply.github.com>
Co-authored-by: Amp <amp@ampcode.com>1 parent deec2c1 commit 9c073bb
1 file changed
Lines changed: 2 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
12 | | - | |
| 11 | + | |
13 | 12 | | |
14 | 13 | | |
15 | 14 | | |
| |||
33 | 32 | | |
34 | 33 | | |
35 | 34 | | |
36 | | - | |
37 | 35 | | |
38 | | - | |
| 36 | + | |
39 | 37 | | |
40 | 38 | | |
41 | 39 | | |
42 | 40 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
0 commit comments