Skip to content

Commit 56733ee

Browse files
committed
Fixed a race condition in discovery manager
1 parent 6093927 commit 56733ee

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ func (dm *DiscoveryManager) Watch() (*PortWatcher, error) {
173173
}
174174
go func() {
175175
dm.watchersMutex.Lock()
176+
defer dm.watchersMutex.Unlock()
177+
178+
// if watcher is still online...
179+
if _, ok := dm.watchers[watcher]; ok {
180+
return
181+
}
182+
176183
// When a watcher is started, send all the current active ports first...
177184
for _, cache := range dm.watchersCache {
178185
for _, ev := range cache {
@@ -181,7 +188,6 @@ func (dm *DiscoveryManager) Watch() (*PortWatcher, error) {
181188
}
182189
// ...and after that add the watcher to the list of watchers receiving events
183190
dm.watchers[watcher] = true
184-
dm.watchersMutex.Unlock()
185191
}()
186192
return watcher, nil
187193
}

0 commit comments

Comments
 (0)