Skip to content

Commit ff3f98d

Browse files
committed
Fix waitgroup when waiting after Done
1 parent 0c9c67d commit ff3f98d

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

internal/sync/waitgroup.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ func (wg *waitGroup) Add(delta int) {
3737
panic("WaitGroup misuse: Add called concurrently with Wait")
3838
}
3939

40-
if wg.n > 0 || !wg.waiting {
41-
return
42-
}
43-
44-
if wg.n == 0 {
40+
if delta < 0 && wg.n == 0 {
4541
wg.f.Set(struct{}{}, nil)
4642
}
4743
}

internal/sync/waitgroup_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ func Test_WaitGroup_PanicsForInvalidCounters(t *testing.T) {
1414
})
1515
}
1616

17+
func Test_WaitGroup_WaitAfterdone(t *testing.T) {
18+
s := NewScheduler()
19+
ctx := Background()
20+
21+
wg := NewWaitGroup()
22+
wg.Add(2)
23+
wg.Done()
24+
wg.Done()
25+
26+
s.NewCoroutine(ctx, func(ctx Context) error {
27+
wg.Wait(ctx)
28+
29+
return nil
30+
})
31+
32+
s.Execute()
33+
require.Equal(t, 0, s.RunningCoroutines())
34+
}
35+
1736
func Test_WaitGroup_Blocks(t *testing.T) {
1837
s := NewScheduler()
1938
ctx := Background()

0 commit comments

Comments
 (0)