You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(monitor): race condition between timer and context cancelation
This commit fixes a race condition when timer fires ⚡️ and context
gets canceled 💥 at the same time. This race condition was detected the the
TestCollectionCancellation and happens as follows ...
````
monitor_collection_test.go:201:
Error Trace: .../monitor_collection_test.go:201
Error: Not equal:
expected: time.Date(2025, time.June, 11, 8, 14, 45, 720156604, time.Local)
actual : time.Date(2025, time.June, 11, 8, 14, 45, 732389967, time.Local)
Test: TestCollectionCancellation
Messages: No collections should happen after cancellation
````
Race Sequence
--------------
1. **Test Context**: `TestCollectionCancellation` runs with a 100ms timeout context
2. **Timer Setup**: Collection goroutines are scheduled with 10ms intervals
3. **Cancellation**: After ~50ms, test calls `cancel()` which triggers `pm.collectionCancel()`
4. **Run() Exit**: The `Run()` method exits and test captures `snapshotAfterRun.Timestamp`
5. **Race Window**: 💥 There's a brief window where:
- Context is canceled (`pm.collectionCtx.Done()` becomes ready)
- Timer also fires (`<-timer` becomes ready)
- Go's `select` **randomly** chooses between the two ready channels
6. **Failure Case**: When `select` chooses `<-timer`:
- `synchronizedPowerRefresh()` executes and updates snapshot with new timestamp
- Test assertion fails because timestamps don't match
Signed-off-by: Sunil Thaha <[email protected]>
0 commit comments