github-vet / github-vet/rangeloop-pointer-findings

chef/automate: components/automate-deployment/pkg/backup/runner.go; 104 LoC

Open
#13,253 0 comments 0 reactions 0 assignees View on GitHub
fresh large
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [chef/automate](https://www.github.com/chef/automate) at [components/automate-deployment/pkg/backup/runner.go](https://github.com/chef/automate/blob/e95960b49bf7a1f2de053c01c278fa792b12c671/components/automate-deployment/pkg/backup/runner.go#L291-L394)

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 backupTask used in defer or goroutine at line 366

[Click here to see the code in its original context.](https://github.com/chef/automate/blob/e95960b49bf7a1f2de053c01c278fa792b12c671/components/automate-deployment/pkg/backup/runner.go#L291-L394)

Click here to show the 104 line(s) of Go which triggered the analyzer.

```go
for _, backupTask := range backupTasks {
logrus.Infof("Delete backup %s", backupTask.TaskID())

r.eventTerm = make(chan struct{})

deleteCtx := NewContext(
WithContextCtx(ctx),
WithContextBackupTask(backupTask),
WithContextBackupLocationSpecification(r.locationSpec),
WithContextPgConnInfo(r.pgConnInfo),
WithContextEsSidecarInfo(r.esSidecarInfo),
WithContextConnFactory(r.connFactory),
WithContextReleaseManifest(r.releaseManifest),
WithContextBuilderMinioLocationSpec(r.builderMinioLocationSpec),
)

// don't verify the contents of the backup we are just going to delete
// anyway
verifier := NoOpObjectVerifier{}

specs, specless, err := LoadAllSpecsFromBackup(ctx, deleteCtx.bucket, &verifier)
if err != nil {
return errors.Wrap(err, "Failed to load specs")
}

if len(specs) == 0 {
if len(specless) == 0 {
logrus.Warnf("Backup %s does not exist", backupTask.TaskID())
err := deleteCtx.DeleteBackupMetadata()
if err != nil {
logrus.WithError(err).Error("Could not clean up backup metadata")
return errors.Wrapf(err, "Could not clean up backup metadata for %s", backupTask.TaskID())
}
}

logrus.Warnf("No specs for %s. Trying to cleanup", backupTask.TaskID())
}

err = deleteCtx.TransactDelete(func(deleteCtx Context) error {
for _, spec := range specs {
executor := NewExecutor(
WithSpec(spec),
WithCancel(cancel),
)
if err := executor.DeleteBackup(deleteCtx); err != nil {
return err
}
}
// Delete objects for services without specs
if len(specless) > 0 {
logrus.Warnf("Found services with missing specs: %v", specless)
for _, service := range specless {
objs, _, err := deleteCtx.bucket.List(ctx, service, false)
if err != nil {
return errors.Wrap(err, "Failed to list bucket")
}
objPaths := ToObjectPaths(objs)
logrus.Warnf("Deleting extra backup objects found for %s: %v", service, objPaths)
if err := deleteCtx.bucket.Delete(ctx, objPaths); err != nil && !IsNotExist(err) {
return errors.Wrapf(err, "Failed to delete backup objects: %v", objPaths)
}
}
}

return nil
})

if err != nil {
return errors.Wrapf(err, "failed to delete backup %s", backupTask.TaskID())
}

// Retry deleting remaining objects until the timeout expires.
go func() {
for {
if ctx.Err() != nil {
logrus.WithError(err).Errorf("Giving up deleting backup %s", backupTask.TaskID())
return
}
// If any objects remain, something bad has happened
remainingObjs, _, err := deleteCtx.bucket.List(ctx, "", false)
if err != nil {
logrus.WithError(err).Warn("Failed to get remaining objects")
time.Sleep(5 * time.Second)
continue
}

if len(remainingObjs) > 0 {
err := errors.Errorf("Failed to delete backup %s. The following files remain: %v", backupTask.TaskID(), remainingObjs)
logrus.WithError(err).Error("Backup delete failed")
time.Sleep(5 * time.Second)
continue
}

close(r.eventTerm)
return
}
}()

select {
case <-ctx.Done():
return ctx.Err()
case <-r.eventTerm:
}
}

```

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: e95960b49bf7a1f2de053c01c278fa792b12c671

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.