Repository navigation
Description
Found because TestMemberTokenDiffersBetweenLaunches — the gate added for #691 — fails 100% of the time on macOS and passes in CI. The test is right and the code is wrong.
The code
// cmd/launch_jobarray.go:26
func generateJobArrayID(name string) string {
timestamp := time.Now().Format("20060102")
// Generate 6-character random suffix (base36: 0-9a-z)
random := fmt.Sprintf("%06x", time.Now().UnixNano()%0xFFFFFF)
return fmt.Sprintf("%s-%s-%s", name, timestamp, random)
}The comment says random. There is no randomness in it — it is UnixNano modulo 0xFFFFFF, which is a clock reading, and the base is hex not base36. Two consequences:
It collides when the clock is coarse. time.Now() on darwin has microsecond resolution, and two consecutive calls return the identical value:
a=1791170115400661000 b=1791170115400661000 delta=0 modA=444bc0 modB=444bc0
a=1791170115400718000 b=1791170115400718000 delta=0 modA=452a68 modB=452a68
So any two generateJobArrayID calls inside the same microsecond produce the same id. Hence:
$ go test ./cmd/ -run TestMemberTokenDiffersBetweenLaunches -count=5
--- FAIL: TestMemberTokenDiffersBetweenLaunches
generateJobArrayID returned "gchp-spawn-smoke-20261004-48e7b1" twice;
this test needs two distinct launches of the same nameFive runs, five failures. Reproduced on origin/main in a clean worktree, so it is not something in flight.
It collides deterministically at a fixed period even on a fine clock. 0xFFFFFF ns is ~16.777 ms, so the suffix wraps every 16.8 ms. Two launches separated by very close to that interval get the same suffix regardless of clock resolution. The id has roughly 16.8 ms of keyspace, not 24 bits of entropy, and it is fully predictable from the wall clock.
Why this matters beyond a red test
#691 fixed the RunInstances ClientToken to derive from the per-launch array id rather than from --job-array-name, precisely so two launches sharing a name could not share a token. That fix is only as good as the array id being distinct. With a colliding id, #691's failure modes come straight back:
- parameters differ →
IdempotentParameterMismatchand the whole cohort fails; - parameters identical → EC2 returns the original reservation instead of launching, so spawn can report success while holding instance IDs that are already terminated, or two arrays can share instances.
The array id is also what cleanupAbandonedPGs and the placement-group names are keyed on, so a collision crosses two cohorts' infrastructure.
And the gate is not actually running
CI runs on self-hosted Linux, where time.Now() has nanosecond resolution, so consecutive calls differ and the test passes. The one gate that exists for this hid the bug it was written to catch, on exactly the machines that run it. That is worth noting independently of the fix: a uniqueness assertion that depends on clock resolution is not an assertion.
Fix
Use real randomness:
func generateJobArrayID(name string) string {
var b [4]byte
if _, err := rand.Read(b[:]); err != nil {
// fall back to the clock rather than failing a launch
binary.BigEndian.PutUint32(b[:], uint32(time.Now().UnixNano()))
}
return fmt.Sprintf("%s-%s-%s", name, time.Now().Format("20060102"), hex.EncodeToString(b[:]))
}crypto/rand rather than math/rand because the id ends up in a ClientToken, and a predictable token is the mechanism by which an unrelated launch could be made to collide with yours on purpose. The cost is negligible — once per launch.
The test should then assert distinctness over many iterations rather than two, so a 24-bit suffix's birthday behaviour is visible, and should not be satisfiable by a clock.
Happy to take this; filing it first because it is a separate defect from the #685/#682 work in flight and it partially un-fixes a shipped change.
Activity
fix(launch): give the job-array id real entropy (#710)
fix(launch): give the job-array id real entropy (#710) (#711)
chore(release): v0.121.0
Stop TestJobArrayIDSuffixIsNotTheClock flaking on a random collision (#…
Stop TestJobArrayIDSuffixIsNotTheClock flaking on a random collision (#…