From 4b4b6cdcab8c8fa6056d1c80340cfec445b512b1 Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Thu, 14 Mar 2019 14:55:44 +0000 Subject: [PATCH] fix race condition issue for smb mount on windows change var name --- pkg/util/mount/BUILD | 1 + pkg/util/mount/mount_windows.go | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/pkg/util/mount/BUILD b/pkg/util/mount/BUILD index f50a934a9d..3a61d016a8 100644 --- a/pkg/util/mount/BUILD +++ b/pkg/util/mount/BUILD @@ -59,6 +59,7 @@ go_library( "//vendor/k8s.io/utils/nsenter:go_default_library", ], "@io_bazel_rules_go//go/platform:windows": [ + "//vendor/k8s.io/utils/keymutex:go_default_library", "//vendor/k8s.io/utils/nsenter:go_default_library", "//vendor/k8s.io/utils/path:go_default_library", ], diff --git a/pkg/util/mount/mount_windows.go b/pkg/util/mount/mount_windows.go index 53dda930b7..4e2efbffb1 100644 --- a/pkg/util/mount/mount_windows.go +++ b/pkg/util/mount/mount_windows.go @@ -28,6 +28,7 @@ import ( "strings" "k8s.io/klog" + "k8s.io/utils/keymutex" utilpath "k8s.io/utils/path" ) @@ -48,6 +49,9 @@ func New(mounterPath string) Interface { } } +// acquire lock for smb mount +var getSMBMountMutex = keymutex.NewHashed(0) + // Mount : mounts source to target with given options. // currently only supports cifs(smb), bind mount(for disk) func (mounter *Mounter) Mount(source string, target string, fstype string, options []string) error { @@ -83,6 +87,10 @@ func (mounter *Mounter) Mount(source string, target string, fstype string, optio return fmt.Errorf("only cifs mount is supported now, fstype: %q, mounting source (%q), target (%q), with options (%q)", fstype, source, target, options) } + // lock smb mount for the same source + getSMBMountMutex.LockKey(source) + defer getSMBMountMutex.UnlockKey(source) + if output, err := newSMBMapping(options[0], options[1], source); err != nil { if isSMBMappingExist(source) { klog.V(2).Infof("SMB Mapping(%s) already exists, now begin to remove and remount", source)