Skip to content

Commit d72db2f

Browse files
committed
Correctly handle discovery cleanup
1 parent 76ceecc commit d72db2f

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

arduino/discovery/discovery.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,7 @@ func (disc *PluggableDiscovery) killProcess() error {
277277
}
278278
disc.statusMutex.Lock()
279279
defer disc.statusMutex.Unlock()
280-
if disc.eventChan != nil {
281-
close(disc.eventChan)
282-
disc.eventChan = nil
283-
}
280+
disc.stopSync()
284281
disc.state = Dead
285282
logrus.Infof("killed discovery %s process", disc.id)
286283
return nil
@@ -367,13 +364,18 @@ func (disc *PluggableDiscovery) Stop() error {
367364
}
368365
disc.statusMutex.Lock()
369366
defer disc.statusMutex.Unlock()
370-
disc.cachedPorts = map[string]*Port{}
367+
disc.stopSync()
368+
disc.state = Idling
369+
return nil
370+
}
371+
372+
func (disc *PluggableDiscovery) stopSync() {
371373
if disc.eventChan != nil {
374+
disc.eventChan <- &Event{"stop", nil, disc.GetID()}
372375
close(disc.eventChan)
373376
disc.eventChan = nil
377+
disc.cachedPorts = map[string]*Port{}
374378
}
375-
disc.state = Idling
376-
return nil
377379
}
378380

379381
// Quit terminates the discovery. No more commands can be accepted by the discovery.
@@ -427,12 +429,8 @@ func (disc *PluggableDiscovery) StartSync(size int) (<-chan *Event, error) {
427429
disc.statusMutex.Lock()
428430
defer disc.statusMutex.Unlock()
429431
disc.state = Syncing
430-
disc.cachedPorts = map[string]*Port{}
431-
if disc.eventChan != nil {
432-
// In case there is already an existing event channel in use we close it
433-
// before creating a new one.
434-
close(disc.eventChan)
435-
}
432+
// In case there is already an existing event channel in use we close it before creating a new one.
433+
disc.stopSync()
436434
c := make(chan *Event, size)
437435
disc.eventChan = c
438436
return c, nil

arduino/discovery/discoverymanager/discoverymanager.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ func New() *DiscoveryManager {
5252
// Clear resets the DiscoveryManager to its initial state
5353
func (dm *DiscoveryManager) Clear() {
5454
dm.discoveriesMutex.Lock()
55-
for _, d := range dm.discoveries {
56-
d.Quit()
57-
logrus.Infof("Closed and removed discovery %s", d.GetID())
55+
defer dm.discoveriesMutex.Unlock()
56+
57+
if dm.discoveriesRunning {
58+
for _, d := range dm.discoveries {
59+
d.Quit()
60+
logrus.Infof("Closed and removed discovery %s", d.GetID())
61+
}
5862
}
5963
dm.discoveries = map[string]*discovery.PluggableDiscovery{}
60-
dm.discoveriesMutex.Unlock()
6164
}
6265

6366
// IDs returns the list of discoveries' ids in this DiscoveryManager
@@ -209,6 +212,9 @@ func (dm *DiscoveryManager) cacheEvent(ev *discovery.Event) {
209212
cache[eventID] = ev
210213
case "remove":
211214
delete(cache, eventID)
215+
case "quit":
216+
// Remove all the events for this discovery
217+
delete(dm.watchersCache, ev.DiscoveryID)
212218
default:
213219
logrus.Errorf("Unhandled event from discovery: %s", ev.Type)
214220
return

0 commit comments

Comments
 (0)