Skip to content

Commit 34f33cf

Browse files
committed
Discoveries are now closed and unregistered after failure
1 parent 56a2ccd commit 34f33cf

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

arduino/discovery/discovery.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -374,21 +374,12 @@ func (disc *PluggableDiscovery) Stop() error {
374374
}
375375

376376
// Quit terminates the discovery. No more commands can be accepted by the discovery.
377-
func (disc *PluggableDiscovery) Quit() error {
378-
if err := disc.sendCommand("QUIT\n"); err != nil {
379-
return err
380-
}
381-
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
382-
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "QUIT", err)
383-
} else if msg.EventType != "quit" {
384-
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "quit", msg.EventType)
385-
} else if msg.Error {
386-
return errors.Errorf(tr("command failed: %s"), msg.Message)
387-
} else if strings.ToUpper(msg.Message) != "OK" {
388-
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
377+
func (disc *PluggableDiscovery) Quit() {
378+
_ = disc.sendCommand("QUIT\n")
379+
if _, err := disc.waitMessage(time.Second * 5); err != nil {
380+
logrus.Errorf("Quitting discovery %s: %s", disc.id, err)
389381
}
390382
disc.killProcess()
391-
return nil
392383
}
393384

394385
// List executes an enumeration of the ports and returns a list of the available

arduino/discovery/discovery_client/main.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ out:
135135
}
136136

137137
for _, disc := range discoveries {
138-
if err := disc.Quit(); err != nil {
139-
log.Fatal("Error stopping discovery:", err)
140-
}
138+
disc.Quit()
141139
fmt.Println("Discovery QUITed")
142140
for disc.State() == discovery.Alive {
143141
time.Sleep(time.Millisecond)

arduino/discovery/discoverymanager/discoverymanager.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/arduino/arduino-cli/arduino/discovery"
2323
"github.com/arduino/arduino-cli/i18n"
2424
"github.com/pkg/errors"
25+
"github.com/sirupsen/logrus"
2526
)
2627

2728
// DiscoveryManager is required to handle multiple pluggable-discovery that
@@ -64,6 +65,14 @@ func (dm *DiscoveryManager) Add(disc *discovery.PluggableDiscovery) error {
6465
return nil
6566
}
6667

68+
// remove quits and deletes the discovery with specified id
69+
// from the discoveries managed by this DiscoveryManager
70+
func (dm *DiscoveryManager) remove(id string) {
71+
dm.discoveries[id].Quit()
72+
delete(dm.discoveries, id)
73+
logrus.Infof("Closed and removed discovery %s", id)
74+
}
75+
6776
// parallelize runs function f concurrently for each discovery.
6877
// Returns a list of errors returned by each call of f.
6978
func (dm *DiscoveryManager) parallelize(f func(d *discovery.PluggableDiscovery) error) []error {
@@ -103,6 +112,7 @@ func (dm *DiscoveryManager) RunAll() []error {
103112
}
104113

105114
if err := d.Run(); err != nil {
115+
dm.remove(d.GetID())
106116
return fmt.Errorf(tr("discovery %[1]s process not started: %[2]w"), d.GetID(), err)
107117
}
108118
return nil
@@ -119,6 +129,7 @@ func (dm *DiscoveryManager) StartAll() []error {
119129
return nil
120130
}
121131
if err := d.Start(); err != nil {
132+
dm.remove(d.GetID())
122133
return fmt.Errorf(tr("starting discovery %[1]s: %[2]w"), d.GetID(), err)
123134
}
124135
return nil
@@ -139,6 +150,7 @@ func (dm *DiscoveryManager) StartSyncAll() (<-chan *discovery.Event, []error) {
139150

140151
eventCh, err := d.StartSync(5)
141152
if err != nil {
153+
dm.remove(d.GetID())
142154
return fmt.Errorf(tr("start syncing discovery %[1]s: %[2]w"), d.GetID(), err)
143155
}
144156

@@ -170,6 +182,7 @@ func (dm *DiscoveryManager) StopAll() []error {
170182
}
171183

172184
if err := d.Stop(); err != nil {
185+
dm.remove(d.GetID())
173186
return fmt.Errorf(tr("stopping discovery %[1]s: %[2]w"), d.GetID(), err)
174187
}
175188
return nil
@@ -185,9 +198,7 @@ func (dm *DiscoveryManager) QuitAll() []error {
185198
return nil
186199
}
187200

188-
if err := d.Quit(); err != nil {
189-
return fmt.Errorf(tr("quitting discovery %[1]s: %[2]w"), d.GetID(), err)
190-
}
201+
d.Quit()
191202
return nil
192203
})
193204
return errs

commands/board/list.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,14 @@ func Watch(instanceID int32, interrupt <-chan bool) (<-chan *rpc.BoardListWatchR
296296
Error: boardsError,
297297
}
298298
case <-interrupt:
299-
errs := dm.StopAll()
300-
if len(errs) > 0 {
299+
for _, err := range dm.StopAll() {
300+
// Discoveries that return errors have their process
301+
// closed and are removed from the list of discoveries
302+
// in the manager
301303
outChan <- &rpc.BoardListWatchResponse{
302304
EventType: "error",
303-
Error: tr("stopping discoveries: %s", errs),
305+
Error: tr("stopping discoveries: %s", err),
304306
}
305-
// Don't close the channel if quitting all discoveries
306-
// failed, otherwise some processes might be left running.
307-
continue
308307
}
309308
return
310309
}

0 commit comments

Comments
 (0)