Skip to content

Commit 5bf2583

Browse files
silvanocerzacmaglie
authored andcommitted
Fix some issues with board list watcher
1 parent fbbcec8 commit 5bf2583

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

arduino/discovery/discoverymanager/discoverymanager.go

+16
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ func (dm *DiscoveryManager) StopAll() error {
106106
return nil
107107
}
108108

109+
// QuitAll quits all the discoveries managed by this DiscoveryManager.
110+
// Returns the first error it meets or nil
111+
func (dm *DiscoveryManager) QuitAll() error {
112+
for _, d := range dm.discoveries {
113+
err := d.Quit()
114+
if err != nil {
115+
return err
116+
}
117+
}
118+
if dm.globalEventCh != nil {
119+
close(dm.globalEventCh)
120+
dm.globalEventCh = nil
121+
}
122+
return nil
123+
}
124+
109125
func (dm *DiscoveryManager) List() []*discovery.Port {
110126
res := []*discovery.Port{}
111127
for _, disc := range dm.discoveries {

commands/board/list.go

+11
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ func Watch(instanceID int32, interrupt <-chan bool) (<-chan *rpc.BoardListWatchR
237237
outChan := make(chan *rpc.BoardListWatchResponse)
238238

239239
go func() {
240+
defer close(outChan)
240241
for _, err := range errs {
241242
outChan <- &rpc.BoardListWatchResponse{
242243
EventType: "error",
@@ -272,6 +273,16 @@ func Watch(instanceID int32, interrupt <-chan bool) (<-chan *rpc.BoardListWatchR
272273
Error: boardsError,
273274
}
274275
case <-interrupt:
276+
err := pm.DiscoveryManager().QuitAll()
277+
if err != nil {
278+
outChan <- &rpc.BoardListWatchResponse{
279+
EventType: "error",
280+
Error: err.Error(),
281+
}
282+
// Don't close the channel if quitting all discoveries
283+
// failed, otherwise some processes might be left running.
284+
continue
285+
}
275286
return
276287
}
277288
}

0 commit comments

Comments
 (0)