Skip to content

Commit da04da6

Browse files
authored
fix: simultaneous attempts to create parent directories (#2366)
1 parent db21a08 commit da04da6

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

vsphere/resource_vsphere_virtual_disk.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path"
1414
"strconv"
1515
"strings"
16+
"sync"
1617

1718
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1819
"github.com/vmware/govmomi"
@@ -22,6 +23,8 @@ import (
2223
"github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/virtualdisk"
2324
)
2425

26+
var vsphereVirtualDiskMakeDirectoryMutex sync.Mutex
27+
2528
type virtualDisk struct {
2629
size int
2730
vmdkPath string
@@ -170,9 +173,14 @@ func resourceVSphereVirtualDiskCreate(d *schema.ResourceData, meta interface{})
170173
if vDisk.createDirectories {
171174
directoryPathIndex := strings.LastIndex(vDisk.vmdkPath, "/")
172175
if directoryPathIndex > 0 {
176+
// Only allow one MakeDirectory operation at a time in order to avoid
177+
// overlapping attempts to create the same directory, which can result
178+
// in some of the attempts failing.
179+
vsphereVirtualDiskMakeDirectoryMutex.Lock()
173180
vmdkPath := vDisk.vmdkPath[0:directoryPathIndex]
174181
log.Printf("[DEBUG] Creating parent directories: %v", ds.Path(vmdkPath))
175182
err = fm.MakeDirectory(context.TODO(), ds.Path(vmdkPath), dc, true)
183+
vsphereVirtualDiskMakeDirectoryMutex.Unlock()
176184
if err != nil && !isAlreadyExists(err) {
177185
log.Printf("[DEBUG] Failed to create parent directories: %v", err)
178186
return err

0 commit comments

Comments
 (0)