Skip to content

Commit 0636b04

Browse files
silvanocerzacmaglie
authored andcommitted
Enhanced handling of some discoveries states
1 parent c78f12c commit 0636b04

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

arduino/discovery/discoverymanager/discoverymanager.go

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ func (dm *DiscoveryManager) Add(disc *discovery.PluggableDiscovery) error {
5858
// returns the first error it meets or nil
5959
func (dm *DiscoveryManager) RunAll() error {
6060
for _, d := range dm.discoveries {
61+
if d.IsAlive() {
62+
// This discovery is already running, nothing to do
63+
continue
64+
}
65+
6166
if err := d.Run(); err != nil {
6267
return err
6368
}
@@ -81,6 +86,11 @@ func (dm *DiscoveryManager) StartAll() error {
8186
func (dm *DiscoveryManager) StartSyncAll() (<-chan *discovery.Event, []error) {
8287
errs := []error{}
8388
for _, d := range dm.discoveries {
89+
if d.IsEventMode() {
90+
// Already started, nothing to do
91+
continue
92+
}
93+
8494
eventCh := d.EventChannel(5)
8595
if err := d.StartSync(); err != nil {
8696
errs = append(errs, err)

commands/board/list.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,11 @@ func Watch(instanceID int32, interrupt <-chan bool) (<-chan *rpc.BoardListWatchR
273273
Error: boardsError,
274274
}
275275
case <-interrupt:
276-
err := pm.DiscoveryManager().QuitAll()
276+
err := pm.DiscoveryManager().StopAll()
277277
if err != nil {
278278
outChan <- &rpc.BoardListWatchResponse{
279279
EventType: "error",
280-
Error: err.Error(),
280+
Error: fmt.Sprintf("stopping discoveries: %s", err),
281281
}
282282
// Don't close the channel if quitting all discoveries
283283
// failed, otherwise some processes might be left running.

commands/daemon/daemon.go

+36-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package daemon
1717

1818
import (
1919
"context"
20+
"fmt"
2021
"io"
2122

2223
"github.com/arduino/arduino-cli/arduino/utils"
@@ -28,6 +29,7 @@ import (
2829
"github.com/arduino/arduino-cli/commands/sketch"
2930
"github.com/arduino/arduino-cli/commands/upload"
3031
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
32+
"github.com/sirupsen/logrus"
3133
)
3234

3335
// ArduinoCoreServerImpl FIXMEDOC
@@ -73,14 +75,37 @@ func (s *ArduinoCoreServerImpl) BoardListWatch(stream rpc.ArduinoCoreService_Boa
7375
return err
7476
}
7577

76-
interrupt := make(chan bool)
78+
if msg.Instance == nil {
79+
err = fmt.Errorf("no instance specified")
80+
stream.Send(&rpc.BoardListWatchResponse{
81+
EventType: "error",
82+
Error: err.Error(),
83+
})
84+
return err
85+
}
86+
87+
interrupt := make(chan bool, 1)
7788
go func() {
78-
msg, err := stream.Recv()
79-
if err != nil {
80-
interrupt <- true
81-
}
82-
if msg != nil {
83-
interrupt <- msg.Interrupt
89+
defer close(interrupt)
90+
for {
91+
msg, err := stream.Recv()
92+
// Handle client closing the stream and eventual errors
93+
if err == io.EOF {
94+
logrus.Info("boards watcher stream closed")
95+
interrupt <- true
96+
return
97+
} else if err != nil {
98+
logrus.Infof("interrupting boards watcher: %v", err)
99+
interrupt <- true
100+
return
101+
}
102+
103+
// Message received, does the client want to interrupt?
104+
if msg != nil && msg.Interrupt {
105+
logrus.Info("boards watcher interrupted by client")
106+
interrupt <- msg.Interrupt
107+
return
108+
}
84109
}
85110
}()
86111

@@ -90,7 +115,10 @@ func (s *ArduinoCoreServerImpl) BoardListWatch(stream rpc.ArduinoCoreService_Boa
90115
}
91116

92117
for event := range eventsChan {
93-
stream.Send(event)
118+
err = stream.Send(event)
119+
if err != nil {
120+
logrus.Infof("sending board watch message: %v", err)
121+
}
94122
}
95123

96124
return nil

0 commit comments

Comments
 (0)