Skip to content

Commit 7a514ef

Browse files
committedOct 13, 2022
fix: synchronize Group's err
1 parent c7a74b9 commit 7a514ef

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed
 

‎group.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ func (g *TaskGroup) Wait() {
3232
// TaskGroupWithContext represents a group of related tasks associated to a context
3333
type TaskGroupWithContext struct {
3434
TaskGroup
35-
ctx context.Context
36-
cancel context.CancelFunc
37-
errOnce sync.Once
38-
err error
35+
ctx context.Context
36+
cancel context.CancelFunc
37+
38+
errSync struct {
39+
once sync.Once
40+
guard sync.RWMutex
41+
}
42+
err error
3943
}
4044

4145
// Submit adds a task to this group and sends it to the worker pool to be executed
@@ -57,8 +61,11 @@ func (g *TaskGroupWithContext) Submit(task func() error) {
5761
// don't actually ignore errors
5862
err := task()
5963
if err != nil {
60-
g.errOnce.Do(func() {
64+
g.errSync.once.Do(func() {
65+
g.errSync.guard.Lock()
6166
g.err = err
67+
g.errSync.guard.Unlock()
68+
6269
if g.cancel != nil {
6370
g.cancel()
6471
}
@@ -86,5 +93,9 @@ func (g *TaskGroupWithContext) Wait() error {
8693
case <-g.ctx.Done():
8794
}
8895

89-
return g.err
96+
g.errSync.guard.RLock()
97+
err := g.err
98+
g.errSync.guard.RUnlock()
99+
100+
return err
90101
}

0 commit comments

Comments
 (0)
Please sign in to comment.