Skip to content

Commit d1cc4cb

Browse files
authored
[cinder-csi-plugin] Protect OsInstances global map with mutex (#3123)
The OsInstances map is accessed from both CreateOpenStackProvider (write) and GetOpenStackProvider (read) without synchronization. Although current usage is single-threaded at startup, this is a latent data race if the driver is ever accessed concurrently. Add a sync.Mutex to guard read and write access to the OsInstances map, preventing potential concurrent map access panics. Signed-off-by: Walter Boring <waboring@hemna.com>
1 parent 629c71e commit d1cc4cb

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

pkg/csi/cinder/openstack/openstack.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"net/http"
2323
"os"
24+
"sync"
2425

2526
"github.com/gophercloud/gophercloud/v2"
2627
"github.com/gophercloud/gophercloud/v2/openstack"
@@ -147,8 +148,9 @@ func GetConfigFromFiles(configFilePaths []string) (Config, error) {
147148
const defaultMaxVolAttachLimit int64 = 256
148149

149150
var (
150-
OsInstances map[string]IOpenStack
151-
configFiles = []string{"/etc/cloud.conf"}
151+
OsInstances map[string]IOpenStack
152+
osInstancesMu sync.Mutex
153+
configFiles = []string{"/etc/cloud.conf"}
152154
)
153155

154156
func InitOpenStackProvider(cfgFiles []string, httpEndpoint string) {
@@ -212,29 +214,35 @@ func CreateOpenStackProvider(cloudName string) (IOpenStack, error) {
212214
}
213215

214216
// Init OpenStack
215-
OsInstances[cloudName] = &OpenStack{
217+
osInstance := &OpenStack{
216218
compute: computeclient,
217219
blockstorage: blockstorageclient,
218220
bsOpts: cfg.BlockStorage,
219221
epOpts: epOpts,
220222
metadataOpts: cfg.Metadata,
221223
}
222224

223-
return OsInstances[cloudName], nil
225+
osInstancesMu.Lock()
226+
OsInstances[cloudName] = osInstance
227+
osInstancesMu.Unlock()
228+
229+
return osInstance, nil
224230
}
225231

226232
// GetOpenStackProvider returns Openstack Instance
227233
func GetOpenStackProvider(cloudName string) (IOpenStack, error) {
228-
OsInstance, OsInstanceDefined := OsInstances[cloudName]
229-
if OsInstanceDefined {
230-
return OsInstance, nil
234+
osInstancesMu.Lock()
235+
osInstance, ok := OsInstances[cloudName]
236+
osInstancesMu.Unlock()
237+
if ok {
238+
return osInstance, nil
231239
}
232-
OsInstance, err := CreateOpenStackProvider(cloudName)
240+
osInstance, err := CreateOpenStackProvider(cloudName)
233241
if err != nil {
234242
return nil, err
235243
}
236244

237-
return OsInstance, nil
245+
return osInstance, nil
238246
}
239247

240248
// GetMetadataOpts returns metadataopts

0 commit comments

Comments
 (0)