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 <git@patrickpichler.dev>
Co-authored-by: Patrick Pichler <git@patrickpichler.dev>
This commit is contained in:
Patrick Pichler 2023-04-25 10:35:39 +02:00 committed by GitHub
parent 5383d2e858
commit 692cd06c38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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()