fix: clock sequence race and add stress test;#219
fix: clock sequence race and add stress test;#219atlet99 wants to merge 5 commits intogofrs:masterfrom
Conversation
Signed-off-by: Abdurakhman R. <joha.shadibekov@gmail.com>
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 429 438 +9
=========================================
+ Hits 429 438 +9 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
@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++ |
There was a problem hiding this comment.
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.
I'm not that worried about the consistency with how the ints are incremented in this case.
|
Hi, I'd still like to see about getting this merged in :) I dont want to obligate you to any contribution you don't have time for - would it be ok if I picked this up and finished it? |
Issue - #216