fix: stop leaking informers and their caches after tracking ends
Summary
A long-lived process that tracks resources over and over (for example a controller that runs a Helm-compatible deploy per reconcile) keeps shared informers alive after tracking ends, each holding the cache of a resource type for a whole namespace, until the process restarts. A goroutine dump shows informers that got the stop signal but wait in wg.Wait forever, and client-go listeners stuck in chansend inside a kubedog event handler. It happens when a handler has more events to deliver than the tracker channel can buffer at the moment the tracker stops reading, e.g. the initial Add events for several Pods of a Deployment that is already ready.
What
- After a tracker stops (its context is canceled or its informer cleanup is called), informer event handlers stop waiting on tracker channels, so the shared informer stops on the factory stop channel and releases its cache. Applies to the Pod, Deployment, StatefulSet, DaemonSet, Job, Canary and ReplicaSet trackers, the generic resource state and events watchers, and the event informer.
- While the tracker keeps reading, every event is delivered as before: channel capacities and send order are unchanged, nothing is dropped.
- A fatal watch error is no longer sent to the factory's watch error channel once the factory is stopped. Before, a full channel nobody read blocked the reflector and kept the informer alive the same way.
- When starting a Job, DaemonSet, StatefulSet or Pod tracker fails halfway, the informers it already started are released.
- New exported helper
utils.SendOrDone(ctx, ch, v): sendsvor gives up oncectxis done. - Not changed: sends from tracker loops to consumer-facing channels (
Ready,Failed,Status,PodError,AddedReplicaSet, …) still block when the consumer is gone. They can leave a tracker goroutine behind, but not an informer and its cache. - UNVERIFIED: not reproduced against a real cluster. Every handler is covered by a regression test on a fake dynamic client that shows the informer goroutines stay alive without the fix and exit with it.
Why
Informer handlers run in the client-go listener goroutine. A handler blocked on a channel send keeps sharedProcessor.run in wg.Wait() after the stop, so sharedIndexInformer.Run never returns and the indexer stays referenced. 38643e9 made the ReplicaSet informer sends cancellation-aware; every other handler, and the watch error handler, kept the plain send. Bigger channel buffers were not an option: they only move the threshold, since the number of objects a handler replays on start is unbounded.