From 6697840da3eff78be2964dfaf5406ce96c7c5f97 Mon Sep 17 00:00:00 2001 From: chentiewen Date: Tue, 30 Dec 2025 17:35:56 +0800 Subject: [PATCH] fix: close response body on error paths to prevent connection leak MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When executeRequest returns early due to errors (non-200 HTTP status or non-OK gRPC status), the response body was not closed. This prevented the underlying TCP connection from being returned to the connection pool, causing connection leaks and potential OOM under high load with failed requests. This fix ensures resp.Body is properly closed in all error return paths, following Go's net/http best practices. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 Signed-off-by: chentiewen --- pkg/apiclient/grpcproxy.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/apiclient/grpcproxy.go b/pkg/apiclient/grpcproxy.go index df55bc6fb1691..b2b2c64d38528 100644 --- a/pkg/apiclient/grpcproxy.go +++ b/pkg/apiclient/grpcproxy.go @@ -86,6 +86,9 @@ func (c *client) executeRequest(ctx context.Context, fullMethodName string, msg return nil, err } if resp.StatusCode != http.StatusOK { + if resp.Body != nil { + utilio.Close(resp.Body) + } return nil, fmt.Errorf("%s %s failed with status code %d", req.Method, req.URL, resp.StatusCode) } var code codes.Code @@ -97,6 +100,9 @@ func (c *client) executeRequest(ctx context.Context, fullMethodName string, msg code = codes.Code(statusInt) } if code != codes.OK { + if resp.Body != nil { + utilio.Close(resp.Body) + } return nil, status.Error(code, resp.Header.Get("Grpc-Message")) } }