Skip to content

Commit

Permalink
fix: explicitly pass in filter to async analysis go routine (#326)
Browse files Browse the repository at this point in the history
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 <git@patrickpichler.dev>
Co-authored-by: Patrick Pichler <git@patrickpichler.dev>
  • Loading branch information
patrickpichler and Patrick Pichler committed Apr 25, 2023
1 parent 5383d2e commit 692cd06
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)))
}
Expand All @@ -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 {
Expand All @@ -202,7 +202,7 @@ func (a *Analysis) RunAnalysis() []error {
a.Results = append(a.Results, results...)
mutex.Unlock()
<-semaphore
}(analyzer)
}(analyzer, filter)
}
}
wg.Wait()
Expand Down

0 comments on commit 692cd06

Please sign in to comment.