Skip to content

fix: stop NewSubscriptionID from colliding#172

Closed
GregTheGreek wants to merge 1 commit intomainfrom
fix/subid-collisions
Closed

fix: stop NewSubscriptionID from colliding#172
GregTheGreek wants to merge 1 commit intomainfrom
fix/subid-collisions

Conversation

@GregTheGreek
Copy link
Copy Markdown

@GregTheGreek GregTheGreek commented Apr 18, 2026

Might not matter unless doing local dev without docker.

Summary

`NewSubscriptionID` relies on `time.Now().UnixNano()` for uniqueness. On macOS and other coarse-resolution clocks, consecutive calls in a tight loop return the same timestamp, so two subscription IDs collide. This is visible as a persistent failure of `TestSubscriptionID_UniqueIDs` (`comm/subID_test.go:32`) on local runs and any CI image with a lower-resolution monotonic clock.

Swap the nanosecond timestamp for a process-local `atomic.Uint64` counter. Monotonic, lock-free, and immune to clock resolution.

Why not UUIDs?

The identifier segment is an opaque string to every consumer (`Unwrap` splits on `-` and returns `subIDParts[2]` as a string without parsing). A counter keeps the existing format, stays compact, and needs no new dep.

Test plan

  • `go test ./comm -run TestRunSubscriptionIDTestSuite -count=20` passes (previously flaked within the first few runs)
  • Existing `Unwrap` tests still pass — format is unchanged
  • Full test job green on CI

Notes

  • Subscription IDs are process-local; the counter resets on restart. No persisted consumers rely on their absolute value.

Replace time.Now().UnixNano() with a monotonic atomic counter.
The clock-based scheme collides on coarse-resolution clocks
(commonly macOS, where consecutive calls in a tight loop return
the same nanosecond), which surfaced as a persistent
TestSubscriptionID_UniqueIDs failure on local test runs.

The counter keeps the existing "sessionID-msgType-identifier"
string format and Unwrap semantics unchanged - the identifier
segment was already treated as an opaque string by consumers.

Co-Authored-By: Claude
@github-actions
Copy link
Copy Markdown

Go Test coverage is 53.3 %\ ✨ ✨ ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant