From e3e67c6ad58215dce23782905bed0ad45e152ad4 Mon Sep 17 00:00:00 2001 From: Justin SB Date: Fri, 27 Jan 2023 11:23:24 -0500 Subject: [PATCH] executor: Avoid writing into slice without a mutex This is likely safe without a mutex, but I don't believe it is guaranteed to be safe. --- upup/pkg/fi/executor.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/upup/pkg/fi/executor.go b/upup/pkg/fi/executor.go index f076a33d5b..586686e979 100644 --- a/upup/pkg/fi/executor.go +++ b/upup/pkg/fi/executor.go @@ -176,13 +176,19 @@ func (e *executor[T]) forkJoin(tasks []*taskState[T]) []error { return nil } - var wg sync.WaitGroup results := make([]error, len(tasks)) + var resultsMutex sync.Mutex + + var wg sync.WaitGroup for i := 0; i < len(tasks); i++ { wg.Add(1) go func(ts *taskState[T], index int) { - results[index] = fmt.Errorf("function panic") defer wg.Done() + + resultsMutex.Lock() + results[index] = fmt.Errorf("function panic") + resultsMutex.Unlock() + klog.V(2).Infof("Executing task %q: %v\n", ts.key, ts.task) if taskNormalize, ok := ts.task.(TaskNormalize[T]); ok { @@ -192,7 +198,11 @@ func (e *executor[T]) forkJoin(tasks []*taskState[T]) []error { } } - results[index] = ts.task.Run(e.context) + result := ts.task.Run(e.context) + + resultsMutex.Lock() + results[index] = result + resultsMutex.Unlock() }(tasks[i], i) }