diff --git a/internal/arduino/discovery/discoverymanager/discoverymanager.go b/internal/arduino/discovery/discoverymanager/discoverymanager.go index 831a332545f..fa90d708377 100644 --- a/internal/arduino/discovery/discoverymanager/discoverymanager.go +++ b/internal/arduino/discovery/discoverymanager/discoverymanager.go @@ -173,6 +173,13 @@ func (dm *DiscoveryManager) Watch() (*PortWatcher, error) { } go func() { dm.watchersMutex.Lock() + defer dm.watchersMutex.Unlock() + + // if watcher is still online... + if _, ok := dm.watchers[watcher]; ok { + return + } + // When a watcher is started, send all the current active ports first... for _, cache := range dm.watchersCache { for _, ev := range cache { @@ -181,7 +188,6 @@ func (dm *DiscoveryManager) Watch() (*PortWatcher, error) { } // ...and after that add the watcher to the list of watchers receiving events dm.watchers[watcher] = true - dm.watchersMutex.Unlock() }() return watcher, nil }