github-vet / github-vet/rangeloop-pointer-findings
istio/bots: policybot/mgrs/syncmgr/mgr.go; 137 LoC
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Found a possible issue in [istio/bots](https://www.github.com/istio/bots) at [policybot/mgrs/syncmgr/mgr.go](https://github.com/istio/bots/blob/9fa3f10e5af8aad5fed6a8f9e47fbbbd6783c374/policybot/mgrs/syncmgr/mgr.go#L1121-L1257)
Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first issue it finds, so please do not limit your consideration to the contents of the below message.
> range-loop variable repo used in defer or goroutine at line 1143
[Click here to see the code in its original context.](https://github.com/istio/bots/blob/9fa3f10e5af8aad5fed6a8f9e47fbbbd6783c374/policybot/mgrs/syncmgr/mgr.go#L1121-L1257)
Click here to show the 137 line(s) of Go which triggered the analyzer.
```go
for _, repo := range ss.mgr.reg.Repos() {
r, ok := ss.mgr.reg.SingleRecord(refresher.RecordType, repo.OrgAndRepo)
if !ok {
continue
}
tor := r.(*refresher.TestOutputRecord)
g := resultgatherer.TestResultGatherer{
Client: ss.mgr.blobstore,
BucketName: tor.BucketName,
PreSubmitPrefix: tor.PreSubmitTestPath,
PostSubmitPrefix: tor.PostSubmitTestPath,
}
scope.Debugf("Getting post submit test results for org %s", repo.OrgLogin)
var completedTests = make(map[string]bool)
ctLock := sync.RWMutex{}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
ctLock.Lock()
defer ctLock.Unlock()
err := ss.mgr.store.QueryPostSubmitTestResultByDone(ss.ctx, repo.OrgLogin, repo.RepoName,
func(result *storage.PostSubmitTestResult) error {
completedTests[result.RunPath] = true
return nil
})
if err != nil {
scope.Warnf("Unable to fetch previous post submit tests: %s", err)
}
wg.Done()
}()
Paths := g.GetAllPostSubmitTestChan(ss.ctx).WithBuffer(100)
// I think a composition syntax would be better here...
errorChan := Paths.WithContext(ss.ctx).OnError(func(e error) {
// TODO: this should probably be reported out or something...
scope.Warnf("error processing post submit test: %s", e)
}).WithParallelism(50).Transform(func(pathi interface{}) (testRunPaths interface{}, err error) {
var result [][]string
// Wait for a comprehensive list of completed tests
wg.Wait()
ctLock.RLock()
defer ctLock.RUnlock()
bucket := g.Client.Bucket(g.BucketName)
testPref := pathi.(string)
testPrefSplit := strings.Split(testPref, "/")
testName := testPrefSplit[len(testPrefSplit)-2]
runPaths, err := bucket.ListPrefixes(ss.ctx, testPref)
if err != nil {
return nil, err
}
for _, runPath := range runPaths {
if _, ok := completedTests[runPath]; !ok {
result = append(result, []string{testName, runPath})
}
}
testRunPaths = result
return
}).Expand().Transform(func(testRunPathi interface{}) (i interface{}, err error) {
inputArray := testRunPathi.([]string)
testRunPath := inputArray[1]
testName := inputArray[0]
if strings.Contains(testRunPath, "00") {
fmt.Printf("checking post submit test %s\n", testRunPath)
}
return g.GetPostSubmitTestResult(ss.ctx, testName, testRunPath, repo.OrgLogin, repo.RepoName)
}).Batch(5).Transform(func(input interface{}) (t interface{}, err error) {
var testResults []*storage.PostSubmitTestResult
var SuiteOutcomes []*storage.SuiteOutcome
var TestOutcomes []*storage.TestOutcome
var FeatureLabels []*storage.FeatureLabel
for _, i := range input.([]interface{}) {
singleResult := i.(*storage.PostSubtmitAllResult)
testResult := singleResult.TestResult[0]
suiteOutcome := singleResult.SuiteOutcome
testOutcome := singleResult.TestOutcome
featureLabel := singleResult.FeatureLabel
testResults = append(testResults, testResult)
SuiteOutcomes = append(SuiteOutcomes, suiteOutcome...)
TestOutcomes = append(TestOutcomes, testOutcome...)
FeatureLabels = append(FeatureLabels, featureLabel...)
}
fmt.Printf("saving PostSubmitTestResult batch of size %d\n", len(testResults))
err = ss.mgr.store.WritePostSumbitTestResults(ss.ctx, testResults)
if err != nil {
return nil, fmt.Errorf("unable to write PostSumbitTestResults: %v", err)
}
err = ss.mgr.store.WriteSuiteOutcome(ss.ctx, SuiteOutcomes)
if err != nil {
return nil, fmt.Errorf("unable to write SuiteOutcome: %v", err)
}
testOutcomeFeatureLabel := &storage.TestOutcomeFeatureLabel{}
testOutcomeFeatureLabel.TestOutcome = TestOutcomes
testOutcomeFeatureLabel.FeatureLabel = FeatureLabels
return testOutcomeFeatureLabel, nil
}).Batch(1).To(func(input interface{}) error {
for _, i := range input.([]interface{}) {
TestOutcomesList := i.(*storage.TestOutcomeFeatureLabel).TestOutcome
for slice := 0; slice < (len(TestOutcomesList) / 50); slice++ {
err := ss.mgr.store.WriteTestOutcome(ss.ctx, TestOutcomesList[slice*50:(slice+1)*50])
if err != nil {
return fmt.Errorf("unable to write TestOutcome: %v", err)
}
}
if len(TestOutcomesList)%50 != 0 {
err := ss.mgr.store.WriteTestOutcome(ss.ctx, TestOutcomesList[(len(TestOutcomesList)/50)*50:])
if err != nil {
return fmt.Errorf("unable to write TestOutcome: %v", err)
}
}
FeatureLabelList := i.(*storage.TestOutcomeFeatureLabel).FeatureLabel
for slice := 0; slice < (len(FeatureLabelList) / 50); slice++ {
err := ss.mgr.store.WriteFeatureLabel(ss.ctx, FeatureLabelList[slice*50:(slice+1)*50])
if err != nil {
return fmt.Errorf("unable to write FeatureLabel: %v", err)
}
}
if len(FeatureLabelList)%50 != 0 {
err := ss.mgr.store.WriteFeatureLabel(ss.ctx, FeatureLabelList[(len(FeatureLabelList)/50)*50:])
if err != nil {
return fmt.Errorf("unable to write FeatureLabel: %v", err)
}
}
}
return nil
}).WithParallelism(1).Go()
var result *multierror.Error
for err := range errorChan {
result = multierror.Append(err.Err())
}
if result != nil {
return result
}
}
```
Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.
commit ID: 9fa3f10e5af8aad5fed6a8f9e47fbbbd6783c374
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.