Skip to content

Commit

Permalink
controlplane: fix panic: send on closed channel
Browse files Browse the repository at this point in the history
Rarely, the control plane test panics, due to a send on a closed
channel. This can occur in a narrow race window in the filteringWatcher:

1. Stop is called on the child watcher
2. Child watcher calls stop on parent watcher
3. Concurrently, an event is dequeued from the parent result chan, and
   we enter the filtering logic.
4. The parent result chan is closed, and we close the child event channel
5. The filter is matched, and we attempt to write on the closed channel,
   which causes the panic.

Instead of closing the channel in the Stop method, close the channel
from the writing goroutine (as is commonly considered best practice in
Go.)

Fixes: fa89802 (controlplane: Implement filtering of objects with field selectors)

Signed-off-by: David Bimmler <david.bimmler@isovalent.com>
  • Loading branch information
bimmlerd authored and aanm committed Feb 22, 2024
1 parent 51101e9 commit 7df437a
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions test/controlplane/suite/testcase.go
Expand Up @@ -469,8 +469,6 @@ var _ watch.Interface = &filteringWatcher{}

func (fw *filteringWatcher) Stop() {
fw.parent.Stop()
close(fw.events)
fw.events = nil
}

func (fw *filteringWatcher) ResultChan() <-chan watch.Event {
Expand All @@ -486,6 +484,7 @@ func (fw *filteringWatcher) ResultChan() <-chan watch.Event {
fw.events <- event
}
}
close(fw.events)
}()
return fw.events
}
Expand Down

0 comments on commit 7df437a

Please sign in to comment.