Skip to content

[breaking] daemon: Fix concurrency and streamline access to PackageManager #1828

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 17 commits into from
Aug 26, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Thread-safe protect access to instances map
  • Loading branch information
cmaglie committed Aug 22, 2022
commit 1d3b99e166ecb1d55abcad25a12623416d5637a0
26 changes: 16 additions & 10 deletions commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/url"
"os"
"strings"
"sync"

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/cores"
Expand Down Expand Up @@ -49,6 +50,7 @@ var tr = i18n.Tr
// referenced by an int32 handle
var instances = map[int32]*CoreInstance{}
var instancesCount int32 = 1
var instancesMux sync.Mutex

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

// GetPackageManager returns a PackageManager for the given ID, or nil if
// ID doesn't exist
func GetPackageManager(id int32) *packagemanager.PackageManager {
i, ok := instances[id]
if !ok {
i := GetInstance(id)
if i == nil {
return nil
}
return i.PackageManager
}

// GetLibraryManager returns the library manager for the given instance ID
func GetLibraryManager(instanceID int32) *librariesmanager.LibrariesManager {
i, ok := instances[instanceID]
if !ok {
func GetLibraryManager(id int32) *librariesmanager.LibrariesManager {
i := GetInstance(id)
if i == nil {
return nil
}
return i.lm
Expand Down Expand Up @@ -144,9 +148,11 @@ func Create(req *rpc.CreateRequest, extraUserAgent ...string) (*rpc.CreateRespon
)

// Save instance
instancesMux.Lock()
instanceID := instancesCount
instances[instanceID] = instance
instancesCount++
instancesMux.Unlock()

return &rpc.CreateResponse{
Instance: &rpc.Instance{Id: instanceID},
Expand All @@ -166,7 +172,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
if reqInst == nil {
return &arduino.InvalidInstanceError{}
}
instance := instances[reqInst.GetId()]
instance := GetInstance(reqInst.GetId())
if instance == nil {
return &arduino.InvalidInstanceError{}
}
Expand Down Expand Up @@ -413,10 +419,12 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
// Destroy FIXMEDOC
func Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse, error) {
id := req.GetInstance().GetId()

instancesMux.Lock()
defer instancesMux.Unlock()
if _, ok := instances[id]; !ok {
return nil, &arduino.InvalidInstanceError{}
}

delete(instances, id)
return &rpc.DestroyResponse{}, nil
}
Expand Down Expand Up @@ -453,9 +461,7 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ

// UpdateIndex FIXMEDOC
func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB rpc.DownloadProgressCB) (*rpc.UpdateIndexResponse, error) {
id := req.GetInstance().GetId()
_, ok := instances[id]
if !ok {
if GetInstance(req.GetInstance().GetId()) == nil {
return nil, &arduino.InvalidInstanceError{}
}

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy