Skip to content

Commit

Permalink
Merge pull request #37 from thekondor/1.8.1-fix-group-wait-race
Browse files Browse the repository at this point in the history
Synchronize read & write of `TaskGroupWithContext`'s `err` variable
  • Loading branch information
alitto authored Oct 14, 2022
2 parents c7a74b9 + 7a514ef commit fbc6f5d
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ func (g *TaskGroup) Wait() {
// TaskGroupWithContext represents a group of related tasks associated to a context
type TaskGroupWithContext struct {
TaskGroup
ctx context.Context
cancel context.CancelFunc
errOnce sync.Once
err error
ctx context.Context
cancel context.CancelFunc

errSync struct {
once sync.Once
guard sync.RWMutex
}
err error
}

// Submit adds a task to this group and sends it to the worker pool to be executed
Expand All @@ -57,8 +61,11 @@ func (g *TaskGroupWithContext) Submit(task func() error) {
// don't actually ignore errors
err := task()
if err != nil {
g.errOnce.Do(func() {
g.errSync.once.Do(func() {
g.errSync.guard.Lock()
g.err = err
g.errSync.guard.Unlock()

if g.cancel != nil {
g.cancel()
}
Expand Down Expand Up @@ -86,5 +93,9 @@ func (g *TaskGroupWithContext) Wait() error {
case <-g.ctx.Done():
}

return g.err
g.errSync.guard.RLock()
err := g.err
g.errSync.guard.RUnlock()

return err
}

0 comments on commit fbc6f5d

Please sign in to comment.