Fix the goroutine variable capturing in removeObjectsOneByOne

This patch fixes classic golang for-loop variable capturing issue that
leeds to incorrect results in goroutines.

This is fixed in latest versions of golang, but this project uses go 1.15,
so it won't work as expected by an author.
This commit is contained in:
Alexander Narsudinov 2023-10-27 19:59:03 +02:00
parent 25401592e1
commit da3638eb56

View file

@ -197,15 +197,15 @@ func (client *s3Client) removeObjectsOneByOne(bucketName, prefix string) error {
for object := range objectsCh {
guardCh <- 1
go func() {
err := client.minio.RemoveObject(client.ctx, bucketName, object.Key,
minio.RemoveObjectOptions{VersionID: object.VersionID})
go func(obj minio.ObjectInfo) {
err := client.minio.RemoveObject(client.ctx, bucketName, obj.Key,
minio.RemoveObjectOptions{VersionID: obj.VersionID})
if err != nil {
glog.Errorf("Failed to remove object %s, error: %s", object.Key, err)
glog.Errorf("Failed to remove object %s, error: %s", obj.Key, err)
removeErrors++
}
<- guardCh
}()
}(object)
}
for i := 0; i < parallelism; i++ {
guardCh <- 1