Skip to content

Commit

Permalink
fix: synchronize Group's err
Browse files Browse the repository at this point in the history
  • Loading branch information
thekondor committed Oct 13, 2022
1 parent c7a74b9 commit 7a514ef
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 7a514ef

Please sign in to comment.