From 5d289d38baa50a4d77ae9630a31046eb96ea9201 Mon Sep 17 00:00:00 2001 From: CaIon <1808837298@qq.com> Date: Fri, 27 Jun 2025 21:13:21 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20handle=20response=20body?= =?UTF-8?q?=20errors=20more=20gracefully=20in=20OpenAI=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes: - Replaced error returns with logging for response body copy failures to prevent early termination of the request. - Ensured that the response body is closed properly after writing to the client. - Added comments to clarify the handling of billing and error reporting after the response has been sent. This update improves error handling and maintains resource management in the OpenAI handler. --- relay/channel/openai/relay-openai.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/relay/channel/openai/relay-openai.go b/relay/channel/openai/relay-openai.go index 71590cd6..bbd43be1 100644 --- a/relay/channel/openai/relay-openai.go +++ b/relay/channel/openai/relay-openai.go @@ -623,13 +623,13 @@ func OpenaiHandlerWithUsage(c *gin.Context, resp *http.Response, info *relaycomm c.Writer.WriteHeader(resp.StatusCode) _, err = io.Copy(c.Writer, resp.Body) if err != nil { - return service.OpenAIErrorWrapper(err, "copy_response_body_failed", http.StatusInternalServerError), nil - } - err = resp.Body.Close() - if err != nil { - return service.OpenAIErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil + common.SysError("error copying response body: " + err.Error()) } + _ = resp.Body.Close() + // Once we've written to the client, we should not return errors anymore + // because the upstream has already consumed resources and returned content + // We should still perform billing even if parsing fails var usageResp dto.SimpleResponse err = json.Unmarshal(responseBody, &usageResp) if err != nil {