Skip to content

Fix gRPC BoardList* methods concurrency issues #1804

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 15 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
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
Report discovery start errors
  • Loading branch information
cmaglie committed Aug 10, 2022
commit 7c6784ef5b268cef2cce3f3cc2eae8a80425ce4e
15 changes: 12 additions & 3 deletions arduino/discovery/discoverymanager/discoverymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ func (dm *DiscoveryManager) IDs() []string {

// Start starts all the discoveries in this DiscoveryManager.
// If the discoveries are already running, this function does nothing.
func (dm *DiscoveryManager) Start() {
func (dm *DiscoveryManager) Start() []error {
dm.discoveriesMutex.Lock()
defer dm.discoveriesMutex.Unlock()
if dm.discoveriesRunning {
return
return nil
}

go func() {
Expand All @@ -95,16 +95,25 @@ func (dm *DiscoveryManager) Start() {
}
}()

errs := []error{}
var errsLock sync.Mutex

var wg sync.WaitGroup
for _, d := range dm.discoveries {
wg.Add(1)
go func(d *discovery.PluggableDiscovery) {
dm.startDiscovery(d)
if err := dm.startDiscovery(d); err != nil {
errsLock.Lock()
errs = append(errs, err)
errsLock.Unlock()
}
wg.Done()
}(d)
}
wg.Wait()
dm.discoveriesRunning = true

return errs
}

// Add adds a discovery to the list of managed discoveries
Expand Down
2 changes: 1 addition & 1 deletion cli/arguments/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func GetInstallableLibs() []string {
func GetConnectedBoards() []string {
inst := instance.CreateAndInit()

list, _ := board.List(&rpc.BoardListRequest{
list, _, _ := board.List(&rpc.BoardListRequest{
Instance: inst,
})
var res []string
Expand Down
2 changes: 1 addition & 1 deletion cli/arguments/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (p *Port) GetSearchTimeout() time.Duration {
// discovered Port object together with the FQBN. If the port does not match
// exactly 1 board,
func (p *Port) DetectFQBN(inst *rpc.Instance) (string, *rpc.Port) {
detectedPorts, err := board.List(&rpc.BoardListRequest{
detectedPorts, _, err := board.List(&rpc.BoardListRequest{
Instance: inst,
Timeout: p.timeout.Get().Milliseconds(),
})
Expand Down
5 changes: 4 additions & 1 deletion cli/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,16 @@ func runListCommand(cmd *cobra.Command, args []string) {
os.Exit(0)
}

ports, err := board.List(&rpc.BoardListRequest{
ports, discvoeryErrors, err := board.List(&rpc.BoardListRequest{
Instance: inst,
Timeout: timeoutArg.Get().Milliseconds(),
})
if err != nil {
feedback.Errorf(tr("Error detecting boards: %v"), err)
}
for _, err := range discvoeryErrors {
feedback.Errorf(tr("Error starting discovery: %v"), err)
}
feedback.PrintResult(result{ports})
}

Expand Down
10 changes: 5 additions & 5 deletions commands/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,21 @@ func identify(pm *packagemanager.PackageManager, port *discovery.Port) ([]*rpc.B
// List returns a list of boards found by the loaded discoveries.
// In case of errors partial results from discoveries that didn't fail
// are returned.
func List(req *rpc.BoardListRequest) (r []*rpc.DetectedPort, e error) {
func List(req *rpc.BoardListRequest) (r []*rpc.DetectedPort, discoveryStartErrors []error, e error) {
pm := commands.GetPackageManager(req.GetInstance().Id)
if pm == nil {
return nil, &arduino.InvalidInstanceError{}
return nil, nil, &arduino.InvalidInstanceError{}
}

dm := pm.DiscoveryManager()
dm.Start()
discoveryStartErrors = dm.Start()
time.Sleep(time.Duration(req.GetTimeout()) * time.Millisecond)

retVal := []*rpc.DetectedPort{}
for _, port := range dm.List() {
boards, err := identify(pm, port)
if err != nil {
return nil, err
return nil, discoveryStartErrors, err
}

// boards slice can be empty at this point if neither the cores nor the
Expand All @@ -202,7 +202,7 @@ func List(req *rpc.BoardListRequest) (r []*rpc.DetectedPort, e error) {
}
retVal = append(retVal, b)
}
return retVal, nil
return retVal, discoveryStartErrors, nil
}

// Watch returns a channel that receives boards connection and disconnection events.
Expand Down
2 changes: 1 addition & 1 deletion commands/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (s *ArduinoCoreServerImpl) BoardDetails(ctx context.Context, req *rpc.Board

// BoardList FIXMEDOC
func (s *ArduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardListRequest) (*rpc.BoardListResponse, error) {
ports, err := board.List(req)
ports, _, err := board.List(req)
if err != nil {
return nil, convertErrorToRPCStatus(err)
}
Expand Down