From 4116c15c8f9baed5220abbcfae199251965ba243 Mon Sep 17 00:00:00 2001 From: Nikita B Date: Sat, 20 Sep 2025 16:35:49 +0200 Subject: [PATCH 1/2] [apiserver] [126379]: replace call to HandleCrash with call to HandleCrashWithContext where ctx is available for caller Signed-off-by: Nikita B --- .../request/headerrequest/requestheader_controller.go | 2 +- .../k8s.io/apiserver/pkg/registry/generic/registry/store.go | 4 ++-- .../server/dynamiccertificates/configmap_cafile_content.go | 2 +- .../server/dynamiccertificates/dynamic_cafile_content.go | 2 +- .../server/dynamiccertificates/dynamic_serving_content.go | 2 +- staging/src/k8s.io/apiserver/pkg/server/genericapiserver.go | 2 +- staging/src/k8s.io/apiserver/pkg/server/hooks.go | 2 +- .../options/encryptionconfig/controller/controller.go | 6 +++--- .../k8s.io/apiserver/pkg/storage/cacher/cache_watcher.go | 2 +- .../apiserver/pkg/storage/cacher/progress/watch_progress.go | 2 +- .../pkg/storage/feature/feature_support_checker.go | 2 +- .../pkg/storage/value/encrypt/envelope/grpc_service.go | 2 +- .../storage/value/encrypt/envelope/kmsv2/grpc_service.go | 2 +- .../k8s.io/apiserver/pkg/util/peerproxy/peer_discovery.go | 2 +- 14 files changed, 17 insertions(+), 17 deletions(-) diff --git a/staging/src/k8s.io/apiserver/pkg/authentication/request/headerrequest/requestheader_controller.go b/staging/src/k8s.io/apiserver/pkg/authentication/request/headerrequest/requestheader_controller.go index a9e76fe0118..e5419d0cec9 100644 --- a/staging/src/k8s.io/apiserver/pkg/authentication/request/headerrequest/requestheader_controller.go +++ b/staging/src/k8s.io/apiserver/pkg/authentication/request/headerrequest/requestheader_controller.go @@ -174,7 +174,7 @@ func (c *RequestHeaderAuthRequestController) AllowedClientNames() []string { // Run starts RequestHeaderAuthRequestController controller and blocks until stopCh is closed. func (c *RequestHeaderAuthRequestController) Run(ctx context.Context, workers int) { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) defer c.queue.ShutDown() klog.Infof("Starting %s", c.name) diff --git a/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go b/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go index 722a312270a..e71645393fd 100644 --- a/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go +++ b/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go @@ -1263,7 +1263,7 @@ func (e *Store) DeleteCollection(ctx context.Context, deleteValidation rest.Vali for i := 0; i < workersNumber; i++ { go func() { // panics don't cross goroutine boundaries - defer utilruntime.HandleCrash(func(panicReason interface{}) { + defer utilruntime.HandleCrashWithContext(ctx, func(_ context.Context, panicReason interface{}) { errs <- fmt.Errorf("DeleteCollection goroutine panicked: %v", panicReason) }) defer wg.Done() @@ -1288,7 +1288,7 @@ func (e *Store) DeleteCollection(ctx context.Context, deleteValidation rest.Vali } // In case of all workers exit, notify distributor. go func() { - defer utilruntime.HandleCrash(func(panicReason interface{}) { + defer utilruntime.HandleCrashWithContext(ctx, func(_ context.Context, panicReason interface{}) { errs <- fmt.Errorf("DeleteCollection workers closer panicked: %v", panicReason) }) wg.Wait() diff --git a/staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/configmap_cafile_content.go b/staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/configmap_cafile_content.go index dbe9e375f26..5af3dbe866d 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/configmap_cafile_content.go +++ b/staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/configmap_cafile_content.go @@ -199,7 +199,7 @@ func (c *ConfigMapCAController) RunOnce(ctx context.Context) error { // Run starts the kube-apiserver and blocks until stopCh is closed. func (c *ConfigMapCAController) Run(ctx context.Context, workers int) { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) defer c.queue.ShutDown() klog.InfoS("Starting controller", "name", c.name) diff --git a/staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/dynamic_cafile_content.go b/staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/dynamic_cafile_content.go index 0fcf82bd0db..2f21951349f 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/dynamic_cafile_content.go +++ b/staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/dynamic_cafile_content.go @@ -155,7 +155,7 @@ func (c *DynamicFileCAContent) RunOnce(ctx context.Context) error { // Run starts the controller and blocks until stopCh is closed. func (c *DynamicFileCAContent) Run(ctx context.Context, workers int) { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) defer c.queue.ShutDown() klog.InfoS("Starting controller", "name", c.name) diff --git a/staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/dynamic_serving_content.go b/staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/dynamic_serving_content.go index e0dd0474b1c..268fe70b0d5 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/dynamic_serving_content.go +++ b/staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/dynamic_serving_content.go @@ -129,7 +129,7 @@ func (c *DynamicCertKeyPairContent) RunOnce(ctx context.Context) error { // Run starts the controller and blocks until context is killed. func (c *DynamicCertKeyPairContent) Run(ctx context.Context, workers int) { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) defer c.queue.ShutDown() klog.InfoS("Starting controller", "name", c.name) diff --git a/staging/src/k8s.io/apiserver/pkg/server/genericapiserver.go b/staging/src/k8s.io/apiserver/pkg/server/genericapiserver.go index e687f099df1..5dc2b061ea6 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/genericapiserver.go +++ b/staging/src/k8s.io/apiserver/pkg/server/genericapiserver.go @@ -530,7 +530,7 @@ func (s preparedGenericAPIServer) RunWithContext(ctx context.Context) error { // If UDS profiling is enabled, start a local http server listening on that socket if s.UnprotectedDebugSocket != nil { go func() { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) klog.Error(s.UnprotectedDebugSocket.RunWithContext(ctx)) }() } diff --git a/staging/src/k8s.io/apiserver/pkg/server/hooks.go b/staging/src/k8s.io/apiserver/pkg/server/hooks.go index 150b40b4755..c52ed28f748 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/hooks.go +++ b/staging/src/k8s.io/apiserver/pkg/server/hooks.go @@ -196,7 +196,7 @@ func runPostStartHook(name string, entry postStartHookEntry, context PostStartHo var err error func() { // don't let the hook *accidentally* panic and kill the server - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(context) err = entry.hook(context) }() // if the hook intentionally wants to kill server, let it. diff --git a/staging/src/k8s.io/apiserver/pkg/server/options/encryptionconfig/controller/controller.go b/staging/src/k8s.io/apiserver/pkg/server/options/encryptionconfig/controller/controller.go index 5c249bcc08b..9314d6d125e 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/options/encryptionconfig/controller/controller.go +++ b/staging/src/k8s.io/apiserver/pkg/server/options/encryptionconfig/controller/controller.go @@ -92,7 +92,7 @@ func NewDynamicEncryptionConfiguration( // Run starts the controller and blocks until ctx is canceled. func (d *DynamicEncryptionConfigContent) Run(ctx context.Context) { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) klog.InfoS("Starting controller", "name", d.name) defer klog.InfoS("Shutting down controller", "name", d.name) @@ -101,7 +101,7 @@ func (d *DynamicEncryptionConfigContent) Run(ctx context.Context) { wg.Add(1) go func() { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) defer wg.Done() defer d.queue.ShutDown() <-ctx.Done() @@ -109,7 +109,7 @@ func (d *DynamicEncryptionConfigContent) Run(ctx context.Context) { wg.Add(1) go func() { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) defer wg.Done() d.runWorker(ctx) }() diff --git a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cache_watcher.go b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cache_watcher.go index a74cf8d4a3e..808491dbd60 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cache_watcher.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cache_watcher.go @@ -434,7 +434,7 @@ func (c *cacheWatcher) sendWatchCacheEvent(event *watchCacheEvent) { } func (c *cacheWatcher) processInterval(ctx context.Context, cacheInterval *watchCacheInterval, resourceVersion uint64) { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) defer close(c.result) defer c.Stop() diff --git a/staging/src/k8s.io/apiserver/pkg/storage/cacher/progress/watch_progress.go b/staging/src/k8s.io/apiserver/pkg/storage/cacher/progress/watch_progress.go index 76fe73f1365..9880fc6c36a 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/cacher/progress/watch_progress.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/cacher/progress/watch_progress.go @@ -71,7 +71,7 @@ func (pr *ConditionalProgressRequester) Run(stopCh <-chan struct{}) { ctx = metadata.NewOutgoingContext(ctx, pr.contextMetadata) } go func() { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) <-stopCh pr.mux.Lock() defer pr.mux.Unlock() diff --git a/staging/src/k8s.io/apiserver/pkg/storage/feature/feature_support_checker.go b/staging/src/k8s.io/apiserver/pkg/storage/feature/feature_support_checker.go index 77f5ab05c9c..e664d22a646 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/feature/feature_support_checker.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/feature/feature_support_checker.go @@ -109,7 +109,7 @@ func (f *defaultFeatureSupportChecker) checkClient(ctx context.Context, c client } f.checkingEndpoint[ep] = struct{}{} go func(ep string) { - defer runtime.HandleCrash() + defer runtime.HandleCrashWithContext(ctx) err := delayFunc.Until(ctx, true, true, func(ctx context.Context) (done bool, err error) { internalErr := f.clientSupportsRequestWatchProgress(ctx, c, ep) return internalErr == nil, nil diff --git a/staging/src/k8s.io/apiserver/pkg/storage/value/encrypt/envelope/grpc_service.go b/staging/src/k8s.io/apiserver/pkg/storage/value/encrypt/envelope/grpc_service.go index b2a5fd145cd..a5987c7893f 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/value/encrypt/envelope/grpc_service.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/value/encrypt/envelope/grpc_service.go @@ -86,7 +86,7 @@ func NewGRPCService(ctx context.Context, endpoint string, callTimeout time.Durat s.kmsClient = kmsapi.NewKeyManagementServiceClient(s.connection) go func() { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) <-ctx.Done() _ = s.connection.Close() diff --git a/staging/src/k8s.io/apiserver/pkg/storage/value/encrypt/envelope/kmsv2/grpc_service.go b/staging/src/k8s.io/apiserver/pkg/storage/value/encrypt/envelope/kmsv2/grpc_service.go index 78cc02e57a6..7cd7d1ce2b7 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/value/encrypt/envelope/kmsv2/grpc_service.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/value/encrypt/envelope/kmsv2/grpc_service.go @@ -83,7 +83,7 @@ func NewGRPCService(ctx context.Context, endpoint, providerName string, callTime s.kmsClient = kmsapi.NewKeyManagementServiceClient(s.connection) go func() { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) <-ctx.Done() _ = s.connection.Close() diff --git a/staging/src/k8s.io/apiserver/pkg/util/peerproxy/peer_discovery.go b/staging/src/k8s.io/apiserver/pkg/util/peerproxy/peer_discovery.go index 8ceef28bb0f..b652cdbf010 100644 --- a/staging/src/k8s.io/apiserver/pkg/util/peerproxy/peer_discovery.go +++ b/staging/src/k8s.io/apiserver/pkg/util/peerproxy/peer_discovery.go @@ -42,7 +42,7 @@ const ( ) func (h *peerProxyHandler) RunPeerDiscoveryCacheSync(ctx context.Context, workers int) { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) defer h.peerLeaseQueue.ShutDown() defer func() { err := h.apiserverIdentityInformer.Informer().RemoveEventHandler(h.leaseRegistration) From d98076bdc0d794758db317c948550708d0a5cebb Mon Sep 17 00:00:00 2001 From: Nikita B Date: Tue, 30 Sep 2025 19:39:38 +0200 Subject: [PATCH 2/2] [apiserver] [126379]: replace call to HandleCrash with call to HandleCrashWithContext where ctx is available for caller: remove obsolete comment Signed-off-by: Nikita B --- staging/src/k8s.io/apiserver/pkg/server/hooks.go | 1 - 1 file changed, 1 deletion(-) diff --git a/staging/src/k8s.io/apiserver/pkg/server/hooks.go b/staging/src/k8s.io/apiserver/pkg/server/hooks.go index c52ed28f748..3a828061e89 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/hooks.go +++ b/staging/src/k8s.io/apiserver/pkg/server/hooks.go @@ -195,7 +195,6 @@ func (s *GenericAPIServer) isPostStartHookRegistered(name string) bool { func runPostStartHook(name string, entry postStartHookEntry, context PostStartHookContext) { var err error func() { - // don't let the hook *accidentally* panic and kill the server defer utilruntime.HandleCrashWithContext(context) err = entry.hook(context) }()