From e8f3cad95dbceff96987a1985ba0a161e713687c Mon Sep 17 00:00:00 2001 From: Aryan Rai Date: Thu, 29 Jan 2026 03:17:34 +0530 Subject: [PATCH] avoid panic on client disconnec Signed-off-by: Aryan Rai --- pkg/router/handlers.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/router/handlers.go b/pkg/router/handlers.go index c95c221c..f36eae05 100644 --- a/pkg/router/handlers.go +++ b/pkg/router/handlers.go @@ -142,6 +142,24 @@ func (s *Server) handleCodeInterpreterInvoke(c *gin.Context) { s.handleInvoke(c, namespace, name, path, types.CodeInterpreterKind) } +// recoverProxyAbort treats http.ErrAbortHandler from ReverseProxy as a normal client disconnect. +func recoverProxyAbort(sandbox *types.SandboxInfo) { + if r := recover(); r != nil { + if r == http.ErrAbortHandler { + if sandbox != nil { + klog.V(2).Infof( + "Client disconnected, stopping proxy (session: %s)", + sandbox.SessionID, + ) + } else { + klog.V(2).Info("Client disconnected, stopping proxy (session: unknown)") + } + return + } + panic(r) + } +} + // forwardToSandbox forwards the request to the specified sandbox endpoint func (s *Server) forwardToSandbox(c *gin.Context, sandbox *types.SandboxInfo, path string) { // Extract url from sandbox - find matching entry point by path @@ -252,5 +270,6 @@ func (s *Server) forwardToSandbox(c *gin.Context, sandbox *types.SandboxInfo, pa // c.Request = c.Request.WithContext(ctx) // Use the proxy to serve the request + defer recoverProxyAbort(sandbox) proxy.ServeHTTP(c.Writer, c.Request) }