From 5aa5bdf26a7fed98333b84b419fbf8cd5717289b Mon Sep 17 00:00:00 2001 From: bufdev Date: Fri, 1 Nov 2024 13:35:50 -0400 Subject: [PATCH] fix --- private/pkg/thread/thread.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/private/pkg/thread/thread.go b/private/pkg/thread/thread.go index b50d54ab6d..92cebacaac 100644 --- a/private/pkg/thread/thread.go +++ b/private/pkg/thread/thread.go @@ -74,8 +74,13 @@ func Parallelize(ctx context.Context, jobs []func(context.Context) error, option } semaphoreC := make(chan struct{}, Parallelism()*multiplier) var errs []error - var wg sync.WaitGroup var lock sync.Mutex + addError := func(err error) { + lock.Lock() + errs = append(errs, err) + lock.Unlock() + } + var wg sync.WaitGroup var stop bool for _, job := range jobs { if stop { @@ -90,20 +95,18 @@ func Parallelize(ctx context.Context, jobs []func(context.Context) error, option select { case <-ctx.Done(): stop = true - errs = append(errs, ctx.Err()) + addError(ctx.Err()) case semaphoreC <- struct{}{}: select { case <-ctx.Done(): stop = true - errs = append(errs, ctx.Err()) + addError(ctx.Err()) default: job := job wg.Add(1) go func() { if err := job(ctx); err != nil { - lock.Lock() - errs = append(errs, err) - lock.Unlock() + addError(err) if cancel != nil { cancel() }