Skip to content

Commit ccae631

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#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
2 parents 39581ac + 1ab88c9 commit ccae631

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

staging/src/k8s.io/client-go/tools/cache/reflector.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,6 @@ func (r *Reflector) resyncChan() (<-chan time.Time, func() bool) {
239239
func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error {
240240
glog.V(3).Infof("Listing and watching %v from %s", r.expectedType, r.name)
241241
var resourceVersion string
242-
resyncCh, cleanup := r.resyncChan()
243-
defer cleanup()
244242

245243
// Explicitly set "0" as resource version - it's fine for the List()
246244
// to be served from cache and potentially be delayed relative to
@@ -272,6 +270,10 @@ func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error {
272270
cancelCh := make(chan struct{})
273271
defer close(cancelCh)
274272
go func() {
273+
resyncCh, cleanup := r.resyncChan()
274+
defer func() {
275+
cleanup() // Call the last one written into cleanup
276+
}()
275277
for {
276278
select {
277279
case <-resyncCh:

0 commit comments

Comments
 (0)