-
Notifications
You must be signed in to change notification settings - Fork 122
fix: clock sequence race and add stress test; #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Abdurakhman R. <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #219 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 5 5
Lines 517 532 +15
=========================================
+ Hits 517 532 +15 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a race condition in Version-1 UUID generation's clock sequence handling and adds comprehensive stress testing to verify the fix. The issue occurred when the 14-bit clock sequence counter overflowed without proper handling, potentially causing UUID collisions.
- Implements proper 14-bit clock sequence wrapping with bitmask operation
- Adds mandatory timestamp advancement when sequence wraps to zero
- Introduces stress test with table-driven concurrent scenarios
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| generator.go | Fixed clock sequence race condition by adding proper 14-bit wrapping and timestamp advancement |
| race_v1_test.go | Added comprehensive concurrent stress test to verify UUID uniqueness under high contention |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| // Yield the processor briefly to avoid busy-waiting. | ||
| runtime.Gosched() |
Copilot
AI
Sep 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using runtime.Gosched() in a tight loop creates a busy-wait pattern that can consume CPU resources. Consider using a small sleep duration like time.Sleep(time.Microsecond) to reduce CPU usage while waiting for timestamp advancement.
| // Yield the processor briefly to avoid busy-waiting. | |
| runtime.Gosched() | |
| // Sleep briefly to avoid busy-waiting and reduce CPU usage. | |
| time.Sleep(time.Microsecond) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@atlet99 This is backed up by another stackoverflow thread here: https://stackoverflow.com/a/57703034/31566036
What do you think of doing a sleep instead? Might need to be a millisecond sleep instead of a microsecond though...
| } | ||
| mu.Lock() | ||
| if _, exists := seen[u]; exists { | ||
| dupCount++ |
Copilot
AI
Sep 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dupCount variable is accessed without atomic operations while other variables use atomic.AddUint32(). This creates inconsistent synchronization patterns. Consider using atomic.AddUint32(&dupCount, 1) for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not that worried about the consistency with how the ints are incremented in this case.
Issue - #216