Commit ccae631f authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #50562 from atlassian/call-cleanup-properly

Automatic merge from submit-queue (batch tested with PRs 51134, 51122, 50562, 50971, 51327) Call the right cleanup function **What this PR does / why we need it**: `defer cleanup()` will always call the function that was returned by the first call to `r.resyncChan()` but it should call the one returned by the last call. **Special notes for your reviewer**: This will print `c1`, not `c2`. See https://play.golang.org/p/FDjDbUxOvI ```go func main() { var c func() c = c1 defer c() c = c2 } func c1 () { fmt.Println("c1") } func c2 () { fmt.Println("c2") } ``` **Release note**: ```release-note NONE ``` /kind bug /sig api-machinery
parents 39581ac9 1ab88c94
...@@ -239,8 +239,6 @@ func (r *Reflector) resyncChan() (<-chan time.Time, func() bool) { ...@@ -239,8 +239,6 @@ func (r *Reflector) resyncChan() (<-chan time.Time, func() bool) {
func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error { func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error {
glog.V(3).Infof("Listing and watching %v from %s", r.expectedType, r.name) glog.V(3).Infof("Listing and watching %v from %s", r.expectedType, r.name)
var resourceVersion string var resourceVersion string
resyncCh, cleanup := r.resyncChan()
defer cleanup()
// Explicitly set "0" as resource version - it's fine for the List() // Explicitly set "0" as resource version - it's fine for the List()
// to be served from cache and potentially be delayed relative to // to be served from cache and potentially be delayed relative to
...@@ -272,6 +270,10 @@ func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error { ...@@ -272,6 +270,10 @@ func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error {
cancelCh := make(chan struct{}) cancelCh := make(chan struct{})
defer close(cancelCh) defer close(cancelCh)
go func() { go func() {
resyncCh, cleanup := r.resyncChan()
defer func() {
cleanup() // Call the last one written into cleanup
}()
for { for {
select { select {
case <-resyncCh: case <-resyncCh:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment