Skip to content

Commit 5f9c668

Browse files
committed
Correctly handle discovery cleanup
1 parent 0294945 commit 5f9c668

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

Diff for: arduino/discovery/discovery.go

+11-13
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,7 @@ func (disc *PluggableDiscovery) killProcess() error {
274274
}
275275
disc.statusMutex.Lock()
276276
defer disc.statusMutex.Unlock()
277-
if disc.eventChan != nil {
278-
close(disc.eventChan)
279-
disc.eventChan = nil
280-
}
277+
disc.stopSync()
281278
disc.state = Dead
282279
logrus.Infof("killed discovery %s process", disc.id)
283280
return nil
@@ -364,13 +361,18 @@ func (disc *PluggableDiscovery) Stop() error {
364361
}
365362
disc.statusMutex.Lock()
366363
defer disc.statusMutex.Unlock()
367-
disc.cachedPorts = map[string]*Port{}
364+
disc.stopSync()
365+
disc.state = Idling
366+
return nil
367+
}
368+
369+
func (disc *PluggableDiscovery) stopSync() {
368370
if disc.eventChan != nil {
371+
disc.eventChan <- &Event{"stop", nil, disc.GetID()}
369372
close(disc.eventChan)
370373
disc.eventChan = nil
374+
disc.cachedPorts = map[string]*Port{}
371375
}
372-
disc.state = Idling
373-
return nil
374376
}
375377

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

Diff for: arduino/discovery/discoverymanager/discoverymanager.go

+10-4
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
@@ -211,6 +214,9 @@ func (dm *DiscoveryManager) cacheEvent(ev *discovery.Event) {
211214
cache[eventID] = ev
212215
case "remove":
213216
delete(cache, eventID)
217+
case "quit":
218+
// Remove all the events for this discovery
219+
delete(dm.watchersCache, ev.DiscoveryID)
214220
default:
215221
logrus.Errorf("Unhandled event from discovery: %s", ev.Type)
216222
return

0 commit comments

Comments
 (0)