Skip to content

Fixed extremely rare race condition in DiscoveryManager #2589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading