Skip to content

Commit 7ffaa2f

Browse files
authored
Merge pull request #68388 from wgliang/fixbug/defer-in-loop
fix bug that defer in infinite loop
2 parents b51c190 + 05aeea1 commit 7ffaa2f

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

pkg/cloudprovider/providers/vsphere/vsphere_util.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,23 @@ package vsphere
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
23+
"io/ioutil"
2224
"os"
25+
"path/filepath"
2326
"regexp"
2427
"strings"
2528
"time"
2629

2730
"github.com/golang/glog"
2831
"github.com/vmware/govmomi/vim25"
29-
30-
"fmt"
31-
3232
"github.com/vmware/govmomi/vim25/mo"
33-
"io/ioutil"
33+
3434
"k8s.io/api/core/v1"
3535
k8stypes "k8s.io/apimachinery/pkg/types"
3636
"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib"
3737
"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib/diskmanagers"
3838
"k8s.io/kubernetes/pkg/util/version"
39-
"path/filepath"
4039
)
4140

4241
const (
@@ -298,11 +297,15 @@ func (vs *VSphere) cleanUpDummyVMs(dummyVMPrefix string) {
298297
continue
299298
}
300299
// A write lock is acquired to make sure the cleanUp routine doesn't delete any VM's created by ongoing PVC requests.
301-
defer cleanUpDummyVMLock.Lock()
302-
err = diskmanagers.CleanUpDummyVMs(ctx, vmFolder, dc)
303-
if err != nil {
304-
glog.V(4).Infof("Unable to clean up dummy VM's in the kubernetes cluster: %q. err: %+v", vs.cfg.Workspace.Folder, err)
300+
cleanUpDummyVMs := func() {
301+
cleanUpDummyVMLock.Lock()
302+
defer cleanUpDummyVMLock.Unlock()
303+
err = diskmanagers.CleanUpDummyVMs(ctx, vmFolder, dc)
304+
if err != nil {
305+
glog.V(4).Infof("Unable to clean up dummy VM's in the kubernetes cluster: %q. err: %+v", vs.cfg.Workspace.Folder, err)
306+
}
305307
}
308+
cleanUpDummyVMs()
306309
}
307310
}
308311

0 commit comments

Comments
 (0)