Skip to content

fix: simultaneous attempts to create parent directories #2366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions vsphere/resource_vsphere_virtual_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path"
"strconv"
"strings"
"sync"

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

var vsphereVirtualDiskMakeDirectoryMutex sync.Mutex

type virtualDisk struct {
size int
vmdkPath string
Expand Down Expand Up @@ -170,9 +173,14 @@ func resourceVSphereVirtualDiskCreate(d *schema.ResourceData, meta interface{})
if vDisk.createDirectories {
directoryPathIndex := strings.LastIndex(vDisk.vmdkPath, "/")
if directoryPathIndex > 0 {
// Only allow one MakeDirectory operation at a time in order to avoid
// overlapping attempts to create the same directory, which can result
// in some of the attempts failing.
vsphereVirtualDiskMakeDirectoryMutex.Lock()
vmdkPath := vDisk.vmdkPath[0:directoryPathIndex]
log.Printf("[DEBUG] Creating parent directories: %v", ds.Path(vmdkPath))
err = fm.MakeDirectory(context.TODO(), ds.Path(vmdkPath), dc, true)
vsphereVirtualDiskMakeDirectoryMutex.Unlock()
if err != nil && !isAlreadyExists(err) {
log.Printf("[DEBUG] Failed to create parent directories: %v", err)
return err
Expand Down