Skip to content

Commit 03d8653

Browse files
authored
Fixed extremely rare race condition in DiscoveryManager (#2589)
1 parent 2063b6a commit 03d8653

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

internal/arduino/discovery/discoverymanager/discoverymanager.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,17 @@ func (dm *DiscoveryManager) Watch() (*PortWatcher, error) {
171171
defer dm.watchersMutex.Unlock()
172172
delete(dm.watchers, watcher)
173173
close(watcher.feed)
174+
watcher.feed = nil
174175
}
175176
go func() {
176177
dm.watchersMutex.Lock()
178+
defer dm.watchersMutex.Unlock()
179+
180+
// Check if the watcher is still alive (it could have been closed before the goroutine started...)
181+
if watcher.feed == nil {
182+
return
183+
}
184+
177185
// When a watcher is started, send all the current active ports first...
178186
for _, cache := range dm.watchersCache {
179187
for _, ev := range cache {
@@ -182,7 +190,6 @@ func (dm *DiscoveryManager) Watch() (*PortWatcher, error) {
182190
}
183191
// ...and after that add the watcher to the list of watchers receiving events
184192
dm.watchers[watcher] = true
185-
dm.watchersMutex.Unlock()
186193
}()
187194
return watcher, nil
188195
}

0 commit comments

Comments
 (0)