From 692cd06c385c1c6f458994f6e975a9fce2bc1c57 Mon Sep 17 00:00:00 2001 From: Patrick Pichler Date: Tue, 25 Apr 2023 10:35:39 +0200 Subject: [PATCH] fix: explicitly pass in filter to async analysis go routine (#326) Before the filter inside the func literal was capturing the value from the outer loop. This is a subtle mistake, since in combination with running the function literal as go routine, the value of filter could have already changed at invocation time. To fix this, the filter is now passed in as an argument to the func literal. Signed-off-by: Patrick Pichler Co-authored-by: Patrick Pichler --- pkg/analysis/analysis.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/analysis/analysis.go b/pkg/analysis/analysis.go index 465d10ae2f..075cfea32c 100644 --- a/pkg/analysis/analysis.go +++ b/pkg/analysis/analysis.go @@ -161,7 +161,7 @@ func (a *Analysis) RunAnalysis() []error { if analyzer, ok := analyzerMap[filter]; ok { semaphore <- struct{}{} wg.Add(1) - go func(analyzer common.IAnalyzer) { + go func(analyzer common.IAnalyzer, filter string) { defer wg.Done() results, err := analyzer.Analyze(analyzerConfig) if err != nil { @@ -173,7 +173,7 @@ func (a *Analysis) RunAnalysis() []error { a.Results = append(a.Results, results...) mutex.Unlock() <-semaphore - }(analyzer) + }(analyzer, filter) } else { errorList = append(errorList, fmt.Errorf(fmt.Sprintf("\"%s\" filter does not exist. Please run k8sgpt filters list.", filter))) } @@ -190,7 +190,7 @@ func (a *Analysis) RunAnalysis() []error { if analyzer, ok := analyzerMap[filter]; ok { semaphore <- struct{}{} wg.Add(1) - go func(analyzer common.IAnalyzer) { + go func(analyzer common.IAnalyzer, filter string) { defer wg.Done() results, err := analyzer.Analyze(analyzerConfig) if err != nil { @@ -202,7 +202,7 @@ func (a *Analysis) RunAnalysis() []error { a.Results = append(a.Results, results...) mutex.Unlock() <-semaphore - }(analyzer) + }(analyzer, filter) } } wg.Wait()