Skip to content

fix(launch): give the job-array id real entropy (#710) - #711

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/710-jobarray-id-entropy
Oct 5, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/710-jobarray-id-entropy

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Closes #710.

The bug

// before
random := fmt.Sprintf("%06x", time.Now().UnixNano()%0xFFFFFF)

Documented as a "random suffix (base36)". It is neither random nor base36 — it is a clock reading in hex. Two failure modes:

It repeats on a coarse clock. time.Now() has microsecond resolution on darwin, where two consecutive calls return the identical value:

a=1791170115400661000 b=1791170115400661000 delta=0  modA=444bc0 modB=444bc0

It wraps on a fine one. 0xFFFFFF ns is ~16.8 ms, so two launches that far apart collide deterministically regardless of resolution. The keyspace was 16.8 ms of wall clock, not 24 bits — and predictable from outside.

Why it matters

#691 made the RunInstances ClientToken derive from this id, specifically so two launches sharing a --job-array-name could not share a token. A colliding id brings #691's failures straight back: EC2 either rejects the cohort with IdempotentParameterMismatch, or returns the first launch's reservation — so spawn can report success while holding instance IDs that are already terminated.

Now 32 bits from crypto/rand. Not math/rand: the value reaches a ClientToken, and a predictable token is the mechanism by which an unrelated launch could be made to collide with yours on purpose. A CSPRNG failure falls back to the clock rather than refusing a launch, and says so rather than degrading silently.

How it surfaced, which is the more interesting part

#691's own gate — TestMemberTokenDiffersBetweenLaunches — compares two consecutive calls. It was failing 5 runs out of 5 locally and passing in CI, because CI runs on Linux where the clock is fine enough for two calls to differ:

$ go test ./cmd/ -run TestMemberTokenDiffersBetweenLaunches -count=5
--- FAIL: TestMemberTokenDiffersBetweenLaunches
    generateJobArrayID returned "gchp-spawn-smoke-20261004-48e7b1" twice

Reproduced on a clean origin/main worktree, so it predates everything in flight. The one gate written for this hid the bug it was written to catch, on exactly the machines that run it. A uniqueness assertion satisfiable by clock granularity is not an assertion — that generalises past this fix.

So the replacement does not rely on the clock being coarse or fine:

  • TestJobArrayIDSuffixIsNotTheClock mints 2000 ids in a tight loop, which outruns any wall clock on any platform.
  • TestJobArrayIDSuffixUsesTheFullKeyspace rejects a monotonically-increasing source, so a regression to any counter is caught, not just to a clock — which pure uniqueness would miss.

Both verified to fail against the old generator; the first names the colliding id and the two call indices.

The whole short suite is now green locally for the first time this session, which is the other confirmation: the pre-existing failure is gone, not suppressed.

The suffix was documented as "random suffix (base36)" and was neither: it was
time.Now().UnixNano() % 0xFFFFFF, formatted as hex. Two failure modes.

On a coarse clock it simply repeats. time.Now() has MICROSECOND resolution on
darwin, where two consecutive calls return the identical value, so any two ids
minted in the same microsecond were equal. Even on a nanosecond clock the value
wraps every 0xFFFFFF ns (~16.8 ms), so two launches that far apart collide
deterministically. The keyspace was 16.8 ms of wall clock, not 24 bits, and it was
predictable from outside.

That un-fixes #691. #691 made the RunInstances ClientToken derive from this id
precisely so two launches sharing a --job-array-name could not share a token; a
colliding id brings the original failures back — EC2 either rejects the cohort with
IdempotentParameterMismatch, or returns the FIRST launch's reservation, so spawn
reports success holding instances that are already terminated.

crypto/rand, not math/rand: the value reaches a ClientToken, and a predictable
token is the mechanism by which an unrelated launch could be made to collide with
yours deliberately. A CSPRNG failure falls back to the clock rather than refusing
to launch, and says so rather than degrading silently.

How this surfaced is worth recording. #691's own gate,
TestMemberTokenDiffersBetweenLaunches, compares two consecutive calls — so it
failed 5 runs out of 5 on macOS and passed in CI on Linux, where the clock is fine
enough for two calls to differ. The one gate written for this hid the bug on
exactly the machines that run it: a uniqueness assertion satisfied by clock
granularity is not an assertion.

The replacement mints 2000 ids in a tight loop, which outruns any wall clock on any
platform, and a second test rejects a monotonically-increasing source so a
regression to a counter is caught as well as a regression to a clock. Both verified
to fail against the old generator.
@scttfrdmn
scttfrdmn force-pushed the fix/710-jobarray-id-entropy branch from 7148700 to d1cb3e3 Compare October 5, 2026 04:30
@scttfrdmn
scttfrdmn merged commit beee80a into main Oct 5, 2026
6 checks passed
@scttfrdmn
scttfrdmn deleted the fix/710-jobarray-id-entropy branch October 5, 2026 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant