Skip to content

Commit 0a26c4a

Browse files
committed
Thread-safe protect access to instances map
1 parent 83dac2e commit 0a26c4a

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

commands/instances.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net/url"
2222
"os"
2323
"strings"
24+
"sync"
2425

2526
"github.com/arduino/arduino-cli/arduino"
2627
"github.com/arduino/arduino-cli/arduino/cores"
@@ -49,6 +50,7 @@ var tr = i18n.Tr
4950
// referenced by an int32 handle
5051
var instances = map[int32]*CoreInstance{}
5152
var instancesCount int32 = 1
53+
var instancesMux sync.Mutex
5254

5355
// CoreInstance is an instance of the Arduino Core Services. The user can
5456
// instantiate as many as needed by providing a different configuration
@@ -66,23 +68,25 @@ type InstanceContainer interface {
6668
// GetInstance returns a CoreInstance for the given ID, or nil if ID
6769
// doesn't exist
6870
func GetInstance(id int32) *CoreInstance {
71+
instancesMux.Lock()
72+
defer instancesMux.Unlock()
6973
return instances[id]
7074
}
7175

7276
// GetPackageManager returns a PackageManager for the given ID, or nil if
7377
// ID doesn't exist
7478
func GetPackageManager(id int32) *packagemanager.PackageManager {
75-
i, ok := instances[id]
76-
if !ok {
79+
i := GetInstance(id)
80+
if i == nil {
7781
return nil
7882
}
7983
return i.PackageManager
8084
}
8185

8286
// GetLibraryManager returns the library manager for the given instance ID
83-
func GetLibraryManager(instanceID int32) *librariesmanager.LibrariesManager {
84-
i, ok := instances[instanceID]
85-
if !ok {
87+
func GetLibraryManager(id int32) *librariesmanager.LibrariesManager {
88+
i := GetInstance(id)
89+
if i == nil {
8690
return nil
8791
}
8892
return i.lm
@@ -144,9 +148,11 @@ func Create(req *rpc.CreateRequest, extraUserAgent ...string) (*rpc.CreateRespon
144148
)
145149

146150
// Save instance
151+
instancesMux.Lock()
147152
instanceID := instancesCount
148153
instances[instanceID] = instance
149154
instancesCount++
155+
instancesMux.Unlock()
150156

151157
return &rpc.CreateResponse{
152158
Instance: &rpc.Instance{Id: instanceID},
@@ -166,7 +172,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
166172
if reqInst == nil {
167173
return &arduino.InvalidInstanceError{}
168174
}
169-
instance := instances[reqInst.GetId()]
175+
instance := GetInstance(reqInst.GetId())
170176
if instance == nil {
171177
return &arduino.InvalidInstanceError{}
172178
}
@@ -413,10 +419,12 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
413419
// Destroy FIXMEDOC
414420
func Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse, error) {
415421
id := req.GetInstance().GetId()
422+
423+
instancesMux.Lock()
424+
defer instancesMux.Unlock()
416425
if _, ok := instances[id]; !ok {
417426
return nil, &arduino.InvalidInstanceError{}
418427
}
419-
420428
delete(instances, id)
421429
return &rpc.DestroyResponse{}, nil
422430
}
@@ -453,9 +461,7 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
453461

454462
// UpdateIndex FIXMEDOC
455463
func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB rpc.DownloadProgressCB) (*rpc.UpdateIndexResponse, error) {
456-
id := req.GetInstance().GetId()
457-
_, ok := instances[id]
458-
if !ok {
464+
if GetInstance(req.GetInstance().GetId()) == nil {
459465
return nil, &arduino.InvalidInstanceError{}
460466
}
461467

0 commit comments

Comments
 (0)