Skip to content

Commit dbca48e

Browse files
Christopher Schleidenclaude
authored andcommitted
Fix waitGroup.Wait() blocking forever when Add is never called
When Wait() is called without any prior Add(), the counter is already 0 but the internal future was never resolved, causing Wait() to block indefinitely. Return early from Wait() when the counter is 0. Fixes #472 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2824911 commit dbca48e

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

internal/sync/waitgroup.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func NewWaitGroup() WaitGroup {
2121
func (wg *waitGroup) Wait(ctx Context) {
2222
wg.waiting = true
2323

24+
if wg.n == 0 {
25+
return
26+
}
27+
2428
if _, err := wg.f.Get(ctx); err != nil {
2529
panic(err)
2630
}

internal/sync/waitgroup_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ import (
66
"github.com/stretchr/testify/require"
77
)
88

9+
func Test_WaitGroup_WaitWithoutAdd(t *testing.T) {
10+
s := NewScheduler()
11+
ctx := Background()
12+
13+
wg := NewWaitGroup()
14+
15+
s.NewCoroutine(ctx, func(ctx Context) error {
16+
wg.Wait(ctx)
17+
18+
return nil
19+
})
20+
21+
s.Execute()
22+
require.Equal(t, 0, s.RunningCoroutines())
23+
}
24+
925
func Test_WaitGroup_PanicsForInvalidCounters(t *testing.T) {
1026
wg := NewWaitGroup()
1127

0 commit comments

Comments
 (0)