{"record":{"id":"860462abec601a2b","repo":"crowdsecurity/crowdsec","slug":"while-setting-watch-error-handler-for-namespace-q","errorCode":null,"errorMessage":"while setting watch error handler for namespace=%q selector=%q: %w","messagePattern":"while setting watch error handler for namespace=%q selector=%q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/acquisition/modules/kubernetes/run.go","lineNumber":112,"sourceCode":"\tinf := f.Core().V1().Pods().Informer()\n\tif err := inf.SetWatchErrorHandler(func(_ *cache.Reflector, watchErr error) {\n\t\tfields := log.Fields{\n\t\t\t\"namespace\": s.config.Namespace,\n\t\t\t\"selector\":  s.config.Selector,\n\t\t\t\"error\":     watchErr,\n\t\t}\n\t\tif apierrors.IsUnauthorized(watchErr) {\n\t\t\ts.logger.WithFields(fields).Error(\"kubernetes informer received Unauthorized, forcing datasource restart\")\n\t\t\tselect {\n\t\t\tcase watchErrCh <- fmt.Errorf(\"kubernetes informer unauthorized for namespace=%q selector=%q unique_id=%q: %w\", s.config.Namespace, s.config.Selector, s.config.UniqueId, watchErr):\n\t\t\tdefault:\n\t\t\t}\n\t\t\tcancelInformer()\n\t\t\treturn\n\t\t}\n\t\ts.logger.WithFields(fields).Warn(\"kubernetes informer watch error\")\n\t}); err != nil {\n\t\treturn fmt.Errorf(\"while setting watch error handler for namespace=%q selector=%q: %w\", s.config.Namespace, s.config.Selector, err)\n\t}\n\n\t// We ignore the ResourceEventHandlerRegistration returned by\n\t// AddEventHandler since we don't need to remove the handlers until shutdown,\n\t// and we will stop the entire informer at that time.\n\ts.logger.WithFields(log.Fields{\n\t\t\"namespace\": s.config.Namespace,\n\t\t\"selector\":  s.config.Selector,\n\t}).Info(\"adding kubernetes event handler\")\n\t_, err = inf.AddEventHandler(cache.ResourceEventHandlerFuncs{\n\t\tAddFunc: func(obj any) {\n\t\t\tp := obj.(*corev1.Pod)\n\t\t\ts.logger.Debugf(\"ADD %s labels=%v\", podRef(p), p.Labels)\n\t\t\ts.tailPod(informerCtx, p, out, &wg)\n\t\t},\n\t\tUpdateFunc: func(oldObj, newObj any) {\n\t\t\toldP := oldObj.(*corev1.Pod)\n\t\t\tnewP := newObj.(*corev1.Pod)","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/acquisition/modules/kubernetes/run.go#L94-L130","documentation":"In Stream, registering the informer's watch error handler failed; this handler is what converts informer watch errors (e.g. Unauthorized) into a datasource restart, so without it the source cannot safely watch pods.","triggerScenarios":"Thrown at pkg/acquisition/modules/kubernetes/run.go:112 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Restart the datasource/crowdsec; this indicates informer lifecycle misuse","Report a bug if it reproduces on startup"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}