[shell-operator] fix: protect shared informer lifecycle#917
Open
fuldaxxx wants to merge 4 commits into
Open
Conversation
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a shared-informer lifecycle/ownership bug in FactoryStore so that shared informers are anchored to the events-manager/store lifetime rather than the first consumer’s context, preventing other consumers’ snapshots from silently freezing when that first consumer is stopped.
Changes:
- Make
FactoryStorecontext-aware (baseCtx) and derive factory/informer contexts from the store instead of from the first registering consumer. - Add dead-factory detection/recreation, narrow the store lock critical section during initial sync, and stop silently swallowing a watch error handler registration error.
- Add targeted tests covering the shared-informer lifetime, shutdown waiting behavior, and dead-factory recreation.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| pkg/kube_events_manager/monitor_test.go | Updates monitor test setup to pass a context into NewFactoryStore. |
| pkg/kube_events_manager/kube_events_manager.go | Anchors the factory store lifetime to the manager context. |
| pkg/kube_events_manager/factory.go | Reworks shared informer lifecycle ownership and improves concurrency/liveness handling. |
| pkg/kube_events_manager/factory_test.go | Adds regression tests for shared informer ownership, WaitStopped, and dead-factory recreation. |
Comments suppressed due to low confidence (1)
pkg/kube_events_manager/factory.go:158
- Start logs AddEventHandler errors but continues, still recording the (possibly nil) registration and returning nil to the caller. This can leave a broken handler entry and makes failures invisible to resourceInformer.start(). Consider returning the error (after releasing c.mu) so the caller can handle it and Stop won’t later try to remove a nil registration.
registration, err := informer.AddEventHandler(handler)
if err != nil {
log.Warn("Factory store: couldn't add event handler to the factory's informer",
slog.String(pkg.LogKeyNamespace, index.Namespace), slog.String(pkg.LogKeyGVR, index.GVR.String()),
log.Err(err))
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+54
to
58
| func NewFactoryStore(ctx context.Context) *FactoryStore { | ||
| fs := &FactoryStore{ | ||
| data: make(map[FactoryIndex]*Factory), | ||
| stoppedCh: make(map[FactoryIndex]chan struct{}), | ||
| data: make(map[FactoryIndex]*Factory), | ||
| baseCtx: ctx, | ||
| } |
Signed-off-by: Ruslan Gorbunov <ruslan.gorbunov@flant.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Fix a shared-informer lifecycle bug in
FactoryStore(pkg/kube_events_manager/factory.go).A shared informer is a long-lived, store-owned resource, but its context used to be
derived from the first consumer that registered it. Cancelling that consumer's
context (e.g. when its monitor is stopped during a module reload) tore down the shared
informer for every other consumer still using it, silently freezing their snapshots
until the process was restarted. Ownership of the informer lifetime is moved to the
store, plus three supporting fixes (dead-factory detection, a narrower critical section,
and an un-swallowed error).
What this PR does / why we need it
Root cause.
NewFactoryStore()did not receive a context, andadd()derived thefactory context via
context.WithCancel(ctx)from the caller's context. The chainmgr.ctx → monitor.ctx → resourceInformer.ei.ctx → Start(ei.ctx) → add(ei.ctx)meantfactory.ctxwas a child of theei.ctxof whichever monitor created the factory first.Because factories are shared across monitors with the same
FactoryIndex(GVR + namespace + selectors), stopping that first monitor cancelled the shared informer
for all remaining consumers, while the refcount (
handlerRegistrations) was still > 0 sothe factory stayed cached. The surviving consumers were left attached to a dead informer.
This is a classic ownership bug: the lifetime of a shared, long-lived resource must be
owned by the store, not inherited from a transient consumer.
Impact. The failure is silent (no error,
HasSynced()on a stopped-but-once-syncedinformer returns
true) and only recoverable by restarting the process. Insurfaced as an empty
DexClientsnapshot inuser-authn: the hook feeds the desiredobject list into values, Helm prunes anything not in the list, so an empty
to the
dex-client-*Secret andOAuth2Clientbeing deleted — breaking OIDC login tothe cluster. Mass module reloads (e.g. during an upgrade) are the trigger w
Special notes for your reviewer