From da3638eb566c9030a40e36534819580757547444 Mon Sep 17 00:00:00 2001 From: Alexander Narsudinov Date: Fri, 27 Oct 2023 19:59:03 +0200 Subject: [PATCH] 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. --- pkg/s3/client.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/s3/client.go b/pkg/s3/client.go index e720d41..563c033 100644 --- a/pkg/s3/client.go +++ b/pkg/s3/client.go @@ -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