Skip to content

Commit 631d80d

Browse files
committed
Fixed send-on-closed channel bug in DiscoveryManager
1 parent f03580c commit 631d80d

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

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

i18n/data/en.po

+7-7
Original file line numberDiff line numberDiff line change
@@ -2541,7 +2541,7 @@ msgstr "destination dir %s already exists, cannot install"
25412541
msgid "directory doesn't exist: %s"
25422542
msgstr "directory doesn't exist: %s"
25432543

2544-
#: arduino/discovery/discoverymanager/discoverymanager.go:112
2544+
#: arduino/discovery/discoverymanager/discoverymanager.go:106
25452545
msgid "discovery %[1]s process not started: %[2]w"
25462546
msgstr "discovery %[1]s process not started: %[2]w"
25472547

@@ -2840,7 +2840,7 @@ msgstr "library not valid"
28402840
msgid "library path does not exist: %s"
28412841
msgstr "library path does not exist: %s"
28422842

2843-
#: arduino/discovery/discoverymanager/discoverymanager.go:225
2843+
#: arduino/discovery/discoverymanager/discoverymanager.go:217
28442844
msgid "listing ports from discovery %[1]s: %[2]w"
28452845
msgstr "listing ports from discovery %[1]s: %[2]w"
28462846

@@ -3038,7 +3038,7 @@ msgstr "platform not installed"
30383038
msgid "please use --build-property instead."
30393039
msgstr "please use --build-property instead."
30403040

3041-
#: arduino/discovery/discoverymanager/discoverymanager.go:67
3041+
#: arduino/discovery/discoverymanager/discoverymanager.go:61
30423042
msgid "pluggable discovery already added: %s"
30433043
msgstr "pluggable discovery already added: %s"
30443044

@@ -3055,7 +3055,7 @@ msgstr "port not found: %[1]s %[2]s"
30553055
msgid "protocol version not supported: requested 1, got %d"
30563056
msgstr "protocol version not supported: requested 1, got %d"
30573057

3058-
#: arduino/discovery/discoverymanager/discoverymanager.go:188
3058+
#: arduino/discovery/discoverymanager/discoverymanager.go:189
30593059
msgid "quitting discovery %[1]s: %[2]w"
30603060
msgstr "quitting discovery %[1]s: %[2]w"
30613061

@@ -3193,19 +3193,19 @@ msgstr "skipping loading of boards %s: malformed custom board options"
31933193
msgid "source is not a directory"
31943194
msgstr "source is not a directory"
31953195

3196-
#: arduino/discovery/discoverymanager/discoverymanager.go:149
3196+
#: arduino/discovery/discoverymanager/discoverymanager.go:142
31973197
msgid "start syncing discovery %[1]s: %[2]w"
31983198
msgstr "start syncing discovery %[1]s: %[2]w"
31993199

3200-
#: arduino/discovery/discoverymanager/discoverymanager.go:128
3200+
#: arduino/discovery/discoverymanager/discoverymanager.go:122
32013201
msgid "starting discovery %[1]s: %[2]w"
32023202
msgstr "starting discovery %[1]s: %[2]w"
32033203

32043204
#: commands/board/list.go:302
32053205
msgid "stopping discoveries: %s"
32063206
msgstr "stopping discoveries: %s"
32073207

3208-
#: arduino/discovery/discoverymanager/discoverymanager.go:172
3208+
#: arduino/discovery/discoverymanager/discoverymanager.go:173
32093209
msgid "stopping discovery %[1]s: %[2]w"
32103210
msgstr "stopping discovery %[1]s: %[2]w"
32113211

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)