Skip to content

Commit 718bbbf

Browse files
cmagliesilvanocerza
authored andcommitted
Fixed send-on-closed channel bug in DiscoveryManager (#1472)
1 parent 9053609 commit 718bbbf

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

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

+14-22
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,22 @@ import (
2727
// DiscoveryManager is required to handle multiple pluggable-discovery that
2828
// may be shared across platforms
2929
type DiscoveryManager struct {
30-
discoveries map[string]*discovery.PluggableDiscovery
31-
globalEventCh chan *discovery.Event
30+
discoveries map[string]*discovery.PluggableDiscovery
3231
}
3332

3433
var tr = i18n.Tr
3534

3635
// New creates a new DiscoveryManager
3736
func New() *DiscoveryManager {
3837
return &DiscoveryManager{
39-
discoveries: map[string]*discovery.PluggableDiscovery{},
40-
globalEventCh: nil,
38+
discoveries: map[string]*discovery.PluggableDiscovery{},
4139
}
4240
}
4341

4442
// Clear resets the DiscoveryManager to its initial state
4543
func (dm *DiscoveryManager) Clear() {
4644
dm.QuitAll()
4745
dm.discoveries = map[string]*discovery.PluggableDiscovery{}
48-
if dm.globalEventCh != nil {
49-
close(dm.globalEventCh)
50-
dm.globalEventCh = nil
51-
}
5246
}
5347

5448
// IDs returns the list of discoveries' ids in this DiscoveryManager
@@ -134,9 +128,8 @@ func (dm *DiscoveryManager) StartAll() []error {
134128
// StartSyncAll the discoveries for this DiscoveryManager,
135129
// returns an error for each discovery failing to start syncing
136130
func (dm *DiscoveryManager) StartSyncAll() (<-chan *discovery.Event, []error) {
137-
if dm.globalEventCh == nil {
138-
dm.globalEventCh = make(chan *discovery.Event, 5)
139-
}
131+
eventSink := make(chan *discovery.Event, 5)
132+
var wg sync.WaitGroup
140133
errs := dm.parallelize(func(d *discovery.PluggableDiscovery) error {
141134
state := d.State()
142135
if state != discovery.Idling || state == discovery.Syncing {
@@ -148,14 +141,22 @@ func (dm *DiscoveryManager) StartSyncAll() (<-chan *discovery.Event, []error) {
148141
if err != nil {
149142
return fmt.Errorf(tr("start syncing discovery %[1]s: %[2]w"), d.GetID(), err)
150143
}
144+
145+
wg.Add(1)
151146
go func() {
152147
for ev := range eventCh {
153-
dm.globalEventCh <- ev
148+
eventSink <- ev
154149
}
150+
wg.Done()
155151
}()
156152
return nil
157153
})
158-
return dm.globalEventCh, errs
154+
go func() {
155+
wg.Wait()
156+
eventSink <- &discovery.Event{Type: "quit"}
157+
close(eventSink)
158+
}()
159+
return eventSink, errs
159160
}
160161

161162
// StopAll the discoveries for this DiscoveryManager,
@@ -189,15 +190,6 @@ func (dm *DiscoveryManager) QuitAll() []error {
189190
}
190191
return nil
191192
})
192-
// Close the global channel only if there were no errors
193-
// quitting all alive discoveries
194-
if len(errs) == 0 && dm.globalEventCh != nil {
195-
// Let events consumers that discoveries are quitting no more events
196-
// will be sent on this channel
197-
dm.globalEventCh <- &discovery.Event{Type: "quit"}
198-
close(dm.globalEventCh)
199-
dm.globalEventCh = nil
200-
}
201193
return errs
202194
}
203195

Diff for: i18n/data/en.po

+7-7
Original file line numberDiff line numberDiff line change
@@ -2555,7 +2555,7 @@ msgstr "destination dir %s already exists, cannot install"
25552555
msgid "directory doesn't exist: %s"
25562556
msgstr "directory doesn't exist: %s"
25572557

2558-
#: arduino/discovery/discoverymanager/discoverymanager.go:112
2558+
#: arduino/discovery/discoverymanager/discoverymanager.go:106
25592559
msgid "discovery %[1]s process not started: %[2]w"
25602560
msgstr "discovery %[1]s process not started: %[2]w"
25612561

@@ -2860,7 +2860,7 @@ msgstr "library not valid"
28602860
msgid "library path does not exist: %s"
28612861
msgstr "library path does not exist: %s"
28622862

2863-
#: arduino/discovery/discoverymanager/discoverymanager.go:225
2863+
#: arduino/discovery/discoverymanager/discoverymanager.go:217
28642864
msgid "listing ports from discovery %[1]s: %[2]w"
28652865
msgstr "listing ports from discovery %[1]s: %[2]w"
28662866

@@ -3053,7 +3053,7 @@ msgstr "platform not installed"
30533053
msgid "please use --build-property instead."
30543054
msgstr "please use --build-property instead."
30553055

3056-
#: arduino/discovery/discoverymanager/discoverymanager.go:67
3056+
#: arduino/discovery/discoverymanager/discoverymanager.go:61
30573057
msgid "pluggable discovery already added: %s"
30583058
msgstr "pluggable discovery already added: %s"
30593059

@@ -3069,7 +3069,7 @@ msgstr "port not found: %[1]s %[2]s"
30693069
msgid "protocol version not supported: requested 1, got %d"
30703070
msgstr "protocol version not supported: requested 1, got %d"
30713071

3072-
#: arduino/discovery/discoverymanager/discoverymanager.go:188
3072+
#: arduino/discovery/discoverymanager/discoverymanager.go:189
30733073
msgid "quitting discovery %[1]s: %[2]w"
30743074
msgstr "quitting discovery %[1]s: %[2]w"
30753075

@@ -3206,19 +3206,19 @@ msgstr "skipping loading of boards %s: malformed custom board options"
32063206
msgid "source is not a directory"
32073207
msgstr "source is not a directory"
32083208

3209-
#: arduino/discovery/discoverymanager/discoverymanager.go:149
3209+
#: arduino/discovery/discoverymanager/discoverymanager.go:142
32103210
msgid "start syncing discovery %[1]s: %[2]w"
32113211
msgstr "start syncing discovery %[1]s: %[2]w"
32123212

3213-
#: arduino/discovery/discoverymanager/discoverymanager.go:128
3213+
#: arduino/discovery/discoverymanager/discoverymanager.go:122
32143214
msgid "starting discovery %[1]s: %[2]w"
32153215
msgstr "starting discovery %[1]s: %[2]w"
32163216

32173217
#: commands/board/list.go:302
32183218
msgid "stopping discoveries: %s"
32193219
msgstr "stopping discoveries: %s"
32203220

3221-
#: arduino/discovery/discoverymanager/discoverymanager.go:172
3221+
#: arduino/discovery/discoverymanager/discoverymanager.go:173
32223222
msgid "stopping discovery %[1]s: %[2]w"
32233223
msgstr "stopping discovery %[1]s: %[2]w"
32243224

Diff for: i18n/rice-box.go

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)