Commit 718bbbf2 authored by Cristian Maglie's avatar Cristian Maglie Committed by Silvano Cerza

Fixed send-on-closed channel bug in DiscoveryManager (#1472)

parent 90536095
...@@ -27,8 +27,7 @@ import ( ...@@ -27,8 +27,7 @@ import (
// DiscoveryManager is required to handle multiple pluggable-discovery that // DiscoveryManager is required to handle multiple pluggable-discovery that
// may be shared across platforms // may be shared across platforms
type DiscoveryManager struct { type DiscoveryManager struct {
discoveries map[string]*discovery.PluggableDiscovery discoveries map[string]*discovery.PluggableDiscovery
globalEventCh chan *discovery.Event
} }
var tr = i18n.Tr var tr = i18n.Tr
...@@ -36,8 +35,7 @@ var tr = i18n.Tr ...@@ -36,8 +35,7 @@ var tr = i18n.Tr
// New creates a new DiscoveryManager // New creates a new DiscoveryManager
func New() *DiscoveryManager { func New() *DiscoveryManager {
return &DiscoveryManager{ return &DiscoveryManager{
discoveries: map[string]*discovery.PluggableDiscovery{}, discoveries: map[string]*discovery.PluggableDiscovery{},
globalEventCh: nil,
} }
} }
...@@ -45,10 +43,6 @@ func New() *DiscoveryManager { ...@@ -45,10 +43,6 @@ func New() *DiscoveryManager {
func (dm *DiscoveryManager) Clear() { func (dm *DiscoveryManager) Clear() {
dm.QuitAll() dm.QuitAll()
dm.discoveries = map[string]*discovery.PluggableDiscovery{} dm.discoveries = map[string]*discovery.PluggableDiscovery{}
if dm.globalEventCh != nil {
close(dm.globalEventCh)
dm.globalEventCh = nil
}
} }
// IDs returns the list of discoveries' ids in this DiscoveryManager // IDs returns the list of discoveries' ids in this DiscoveryManager
...@@ -134,9 +128,8 @@ func (dm *DiscoveryManager) StartAll() []error { ...@@ -134,9 +128,8 @@ func (dm *DiscoveryManager) StartAll() []error {
// StartSyncAll the discoveries for this DiscoveryManager, // StartSyncAll the discoveries for this DiscoveryManager,
// returns an error for each discovery failing to start syncing // returns an error for each discovery failing to start syncing
func (dm *DiscoveryManager) StartSyncAll() (<-chan *discovery.Event, []error) { func (dm *DiscoveryManager) StartSyncAll() (<-chan *discovery.Event, []error) {
if dm.globalEventCh == nil { eventSink := make(chan *discovery.Event, 5)
dm.globalEventCh = make(chan *discovery.Event, 5) var wg sync.WaitGroup
}
errs := dm.parallelize(func(d *discovery.PluggableDiscovery) error { errs := dm.parallelize(func(d *discovery.PluggableDiscovery) error {
state := d.State() state := d.State()
if state != discovery.Idling || state == discovery.Syncing { if state != discovery.Idling || state == discovery.Syncing {
...@@ -148,14 +141,22 @@ func (dm *DiscoveryManager) StartSyncAll() (<-chan *discovery.Event, []error) { ...@@ -148,14 +141,22 @@ func (dm *DiscoveryManager) StartSyncAll() (<-chan *discovery.Event, []error) {
if err != nil { if err != nil {
return fmt.Errorf(tr("start syncing discovery %[1]s: %[2]w"), d.GetID(), err) return fmt.Errorf(tr("start syncing discovery %[1]s: %[2]w"), d.GetID(), err)
} }
wg.Add(1)
go func() { go func() {
for ev := range eventCh { for ev := range eventCh {
dm.globalEventCh <- ev eventSink <- ev
} }
wg.Done()
}() }()
return nil return nil
}) })
return dm.globalEventCh, errs go func() {
wg.Wait()
eventSink <- &discovery.Event{Type: "quit"}
close(eventSink)
}()
return eventSink, errs
} }
// StopAll the discoveries for this DiscoveryManager, // StopAll the discoveries for this DiscoveryManager,
...@@ -189,15 +190,6 @@ func (dm *DiscoveryManager) QuitAll() []error { ...@@ -189,15 +190,6 @@ func (dm *DiscoveryManager) QuitAll() []error {
} }
return nil return nil
}) })
// Close the global channel only if there were no errors
// quitting all alive discoveries
if len(errs) == 0 && dm.globalEventCh != nil {
// Let events consumers that discoveries are quitting no more events
// will be sent on this channel
dm.globalEventCh <- &discovery.Event{Type: "quit"}
close(dm.globalEventCh)
dm.globalEventCh = nil
}
return errs return errs
} }
......
...@@ -2555,7 +2555,7 @@ msgstr "destination dir %s already exists, cannot install" ...@@ -2555,7 +2555,7 @@ msgstr "destination dir %s already exists, cannot install"
msgid "directory doesn't exist: %s" msgid "directory doesn't exist: %s"
msgstr "directory doesn't exist: %s" msgstr "directory doesn't exist: %s"
#: arduino/discovery/discoverymanager/discoverymanager.go:112 #: arduino/discovery/discoverymanager/discoverymanager.go:106
msgid "discovery %[1]s process not started: %[2]w" msgid "discovery %[1]s process not started: %[2]w"
msgstr "discovery %[1]s process not started: %[2]w" msgstr "discovery %[1]s process not started: %[2]w"
...@@ -2860,7 +2860,7 @@ msgstr "library not valid" ...@@ -2860,7 +2860,7 @@ msgstr "library not valid"
msgid "library path does not exist: %s" msgid "library path does not exist: %s"
msgstr "library path does not exist: %s" msgstr "library path does not exist: %s"
#: arduino/discovery/discoverymanager/discoverymanager.go:225 #: arduino/discovery/discoverymanager/discoverymanager.go:217
msgid "listing ports from discovery %[1]s: %[2]w" msgid "listing ports from discovery %[1]s: %[2]w"
msgstr "listing ports from discovery %[1]s: %[2]w" msgstr "listing ports from discovery %[1]s: %[2]w"
...@@ -3053,7 +3053,7 @@ msgstr "platform not installed" ...@@ -3053,7 +3053,7 @@ msgstr "platform not installed"
msgid "please use --build-property instead." msgid "please use --build-property instead."
msgstr "please use --build-property instead." msgstr "please use --build-property instead."
#: arduino/discovery/discoverymanager/discoverymanager.go:67 #: arduino/discovery/discoverymanager/discoverymanager.go:61
msgid "pluggable discovery already added: %s" msgid "pluggable discovery already added: %s"
msgstr "pluggable discovery already added: %s" msgstr "pluggable discovery already added: %s"
...@@ -3069,7 +3069,7 @@ msgstr "port not found: %[1]s %[2]s" ...@@ -3069,7 +3069,7 @@ msgstr "port not found: %[1]s %[2]s"
msgid "protocol version not supported: requested 1, got %d" msgid "protocol version not supported: requested 1, got %d"
msgstr "protocol version not supported: requested 1, got %d" msgstr "protocol version not supported: requested 1, got %d"
#: arduino/discovery/discoverymanager/discoverymanager.go:188 #: arduino/discovery/discoverymanager/discoverymanager.go:189
msgid "quitting discovery %[1]s: %[2]w" msgid "quitting discovery %[1]s: %[2]w"
msgstr "quitting discovery %[1]s: %[2]w" msgstr "quitting discovery %[1]s: %[2]w"
...@@ -3206,11 +3206,11 @@ msgstr "skipping loading of boards %s: malformed custom board options" ...@@ -3206,11 +3206,11 @@ msgstr "skipping loading of boards %s: malformed custom board options"
msgid "source is not a directory" msgid "source is not a directory"
msgstr "source is not a directory" msgstr "source is not a directory"
#: arduino/discovery/discoverymanager/discoverymanager.go:149 #: arduino/discovery/discoverymanager/discoverymanager.go:142
msgid "start syncing discovery %[1]s: %[2]w" msgid "start syncing discovery %[1]s: %[2]w"
msgstr "start syncing discovery %[1]s: %[2]w" msgstr "start syncing discovery %[1]s: %[2]w"
#: arduino/discovery/discoverymanager/discoverymanager.go:128 #: arduino/discovery/discoverymanager/discoverymanager.go:122
msgid "starting discovery %[1]s: %[2]w" msgid "starting discovery %[1]s: %[2]w"
msgstr "starting discovery %[1]s: %[2]w" msgstr "starting discovery %[1]s: %[2]w"
...@@ -3218,7 +3218,7 @@ msgstr "starting discovery %[1]s: %[2]w" ...@@ -3218,7 +3218,7 @@ msgstr "starting discovery %[1]s: %[2]w"
msgid "stopping discoveries: %s" msgid "stopping discoveries: %s"
msgstr "stopping discoveries: %s" msgstr "stopping discoveries: %s"
#: arduino/discovery/discoverymanager/discoverymanager.go:172 #: arduino/discovery/discoverymanager/discoverymanager.go:173
msgid "stopping discovery %[1]s: %[2]w" msgid "stopping discovery %[1]s: %[2]w"
msgstr "stopping discovery %[1]s: %[2]w" msgstr "stopping discovery %[1]s: %[2]w"
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment