Skip to content

Commit

Permalink
Fixed extremely rare race condition in DiscoveryManager (#2589)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie authored May 3, 2024
1 parent 2063b6a commit 03d8653
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,17 @@ func (dm *DiscoveryManager) Watch() (*PortWatcher, error) {
defer dm.watchersMutex.Unlock()
delete(dm.watchers, watcher)
close(watcher.feed)
watcher.feed = nil
}
go func() {
dm.watchersMutex.Lock()
defer dm.watchersMutex.Unlock()

// Check if the watcher is still alive (it could have been closed before the goroutine started...)
if watcher.feed == nil {
return
}

// When a watcher is started, send all the current active ports first...
for _, cache := range dm.watchersCache {
for _, ev := range cache {
Expand All @@ -182,7 +190,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
}
Expand Down

0 comments on commit 03d8653

Please sign in to comment.