Skip to content

Commit 5ef874c

Browse files
authored
fix(sync): AdaptiveWaitGroup counter racy (#701)
causing flaky tests. Reorder counter updates relative to semaphore ops to prevent race condition where `Current()` could temporarily exceed 2485582size+12485582. * `Add()`: inc counter before acquiring semaphore * `Done()`: dec counter before releasing semaphore This eliminates the race window where both `Add()` and `Done()` had pending counter updates, causing `TestThrottling` to fail in CI with `Current()` reaching 6-7 instead of expected max. of 5 (size=4). Signed-off-by: Dwi Siswanto <[email protected]>
1 parent a911e35 commit 5ef874c

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

sync/adaptivewaitgroup.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,23 @@ func (s *AdaptiveWaitGroup) AddWithContext(ctx context.Context) error {
6767
case <-ctx.Done():
6868
return ctx.Err()
6969
default:
70+
s.current.Add(1)
71+
7072
// Attempt to acquire a semaphore slot, handle error if acquisition fails
7173
if err := s.sem.Acquire(ctx, 1); err != nil {
74+
s.current.Add(-1)
7275
return err
7376
}
7477
}
7578

76-
// Safely add to the waitgroup only after acquiring the semaphore
7779
s.wg.Add(1)
78-
s.current.Add(1)
7980
return nil
8081
}
8182

8283
func (s *AdaptiveWaitGroup) Done() {
84+
s.current.Add(-1)
8385
s.sem.Release(1)
8486
s.wg.Done()
85-
s.current.Add(-1)
8687
}
8788

8889
func (s *AdaptiveWaitGroup) Wait() {

0 commit comments

Comments
 (0)