From 69a032dea3688fbab0d67686d1631ac9d2ea95f3 Mon Sep 17 00:00:00 2001 From: sususu98 Date: Wed, 4 Nov 2026 18:48:02 +0800 Subject: [PATCH] perf,fix(logging,websocket): avoid 200 OK body buffering and synchronize ws disconnect error logging --- internal/api/middleware/request_logging.go | 1 + .../api/middleware/request_logging_test.go | 22 ++++-- internal/api/middleware/response_writer.go | 78 ++++++------------- internal/clienterror/client_error.go | 5 +- sdk/api/handlers/claude/code_handlers.go | 37 +++++++-- .../openai/openai_responses_websocket.go | 33 +++++--- 6 files changed, 94 insertions(+), 82 deletions(-) diff --git a/internal/api/middleware/request_logging.go b/internal/api/middleware/request_logging.go index b975381395..36d5860cc6 100644 --- a/internal/api/middleware/request_logging.go +++ b/internal/api/middleware/request_logging.go @@ -62,6 +62,7 @@ func RequestLoggingMiddleware(logger logging.RequestLogger) gin.HandlerFunc { // Create response writer wrapper wrapper := NewResponseWriterWrapper(c.Writer, logger, requestInfo) + wrapper.ginContext = c if !loggerEnabled { wrapper.logOnErrorOnly = true } diff --git a/internal/api/middleware/request_logging_test.go b/internal/api/middleware/request_logging_test.go index 9e3a3c380b..b1bf861ad3 100644 --- a/internal/api/middleware/request_logging_test.go +++ b/internal/api/middleware/request_logging_test.go @@ -957,7 +957,7 @@ func TestDecodeCapturedZstdRefusesAnOversizedWindow(t *testing.T) { } } -func TestResponseWriterLogOnErrorOnlyCapsSingleLargeWrite(t *testing.T) { +func TestResponseWriterLogOnErrorOnlyZeroBufferUntilActionableError(t *testing.T) { gin.SetMode(gin.TestMode) rec := httptest.NewRecorder() c, _ := gin.CreateTestContext(rec) @@ -969,19 +969,29 @@ func TestResponseWriterLogOnErrorOnlyCapsSingleLargeWrite(t *testing.T) { Method: http.MethodPost, }) wrapper.logOnErrorOnly = true + wrapper.ginContext = c + // Normal 200 OK writes (even 4 MiB) must not allocate/buffer any bytes when request-log is disabled. largePayload := bytes.Repeat([]byte("a"), 4<<20) // 4 MiB if _, err := wrapper.Write(largePayload); err != nil { t.Fatalf("Write failed: %v", err) } - if wrapper.body.Len() != maxDeferredErrorStreamBodyBytes { - t.Fatalf("wrapper.body.Len() = %d, want %d", wrapper.body.Len(), maxDeferredErrorStreamBodyBytes) + if wrapper.body.Len() != 0 { + t.Fatalf("wrapper.body.Len() for 200 OK = %d, want 0", wrapper.body.Len()) } - if _, err := wrapper.WriteString("extra"); err != nil { + // Once an actionable mid-stream error is recorded, subsequent error frame writes are buffered. + c.Set("API_RESPONSE_ERROR", []*interfaces.ErrorMessage{ + { + StatusCode: http.StatusBadRequest, + Error: errors.New("claude refusal (cyber): blocked"), + }, + }) + const errFrame = "event: error\ndata: {\"type\":\"error\"}\n\n" + if _, err := wrapper.WriteString(errFrame); err != nil { t.Fatalf("WriteString failed: %v", err) } - if wrapper.body.Len() != maxDeferredErrorStreamBodyBytes { - t.Fatalf("wrapper.body.Len() after WriteString = %d, want %d", wrapper.body.Len(), maxDeferredErrorStreamBodyBytes) + if got := wrapper.body.String(); got != errFrame { + t.Fatalf("wrapper.body.String() = %q, want %q", got, errFrame) } } diff --git a/internal/api/middleware/response_writer.go b/internal/api/middleware/response_writer.go index d934a7f33b..6602693846 100644 --- a/internal/api/middleware/response_writer.go +++ b/internal/api/middleware/response_writer.go @@ -47,6 +47,7 @@ type ResponseWriterWrapper struct { streamDone chan struct{} // streamDone signals when the streaming goroutine completes. logger logging.RequestLogger // logger is the instance of the request logger service. requestInfo *RequestInfo // requestInfo holds the details of the original request. + ginContext *gin.Context // ginContext holds the Gin request context for mid-stream error inspection. statusCode int // statusCode stores the HTTP status code of the response. headers map[string][]string // headers stores the response headers. logOnErrorOnly bool // logOnErrorOnly enables logging only when an error response is detected. @@ -119,7 +120,9 @@ func (w *ResponseWriterWrapper) Write(data []byte) (int, error) { return n, err } - w.bufferResponseBodyBytes(data) + if w.shouldBufferResponseBody() { + w.body.Write(data) + } return n, err } @@ -161,8 +164,6 @@ func (w *ResponseWriterWrapper) closeConnectionIfRequestBodyUnconsumed(statusCod } } -const maxDeferredErrorStreamBodyBytes = 256 << 10 // 256 KiB - func (w *ResponseWriterWrapper) shouldBufferResponseBody() bool { if w.logger != nil && w.logger.IsEnabled() { return true @@ -171,57 +172,12 @@ func (w *ResponseWriterWrapper) shouldBufferResponseBody() bool { return false } status := w.effectiveStatusCode() - if status == clienterror.StatusClientClosedRequest { - return false + if status < http.StatusBadRequest { + if apiErrStatus := actionableAPIResponseErrorStatus(extractAPIResponseErrors(w.ginContext)); apiErrStatus >= http.StatusBadRequest { + status = apiErrStatus + } } - if status >= http.StatusBadRequest { - return true - } - return w.body != nil && w.body.Len() < maxDeferredErrorStreamBodyBytes -} - -func (w *ResponseWriterWrapper) bufferResponseBodyBytes(data []byte) { - if len(data) == 0 || w.body == nil || !w.shouldBufferResponseBody() { - return - } - if w.logger != nil && w.logger.IsEnabled() { - w.body.Write(data) - return - } - if w.effectiveStatusCode() >= http.StatusBadRequest { - w.body.Write(data) - return - } - remaining := maxDeferredErrorStreamBodyBytes - w.body.Len() - if remaining <= 0 { - return - } - if len(data) > remaining { - data = data[:remaining] - } - w.body.Write(data) -} - -func (w *ResponseWriterWrapper) bufferResponseBodyString(data string) { - if len(data) == 0 || w.body == nil || !w.shouldBufferResponseBody() { - return - } - if w.logger != nil && w.logger.IsEnabled() { - w.body.WriteString(data) - return - } - if w.effectiveStatusCode() >= http.StatusBadRequest { - w.body.WriteString(data) - return - } - remaining := maxDeferredErrorStreamBodyBytes - w.body.Len() - if remaining <= 0 { - return - } - if len(data) > remaining { - data = data[:remaining] - } - w.body.WriteString(data) + return status >= http.StatusBadRequest && status != clienterror.StatusClientClosedRequest } // WriteString wraps the underlying ResponseWriter's WriteString method to capture response data. @@ -247,7 +203,9 @@ func (w *ResponseWriterWrapper) WriteString(data string) (int, error) { return n, err } - w.bufferResponseBodyString(data) + if w.shouldBufferResponseBody() { + w.body.WriteString(data) + } return n, err } @@ -894,6 +852,18 @@ func hasActionableAPIResponseErrors(apiErrors []*interfaces.ErrorMessage) bool { return false } +func extractAPIResponseErrors(c *gin.Context) []*interfaces.ErrorMessage { + if c == nil { + return nil + } + if apiResponseError, exists := c.Get("API_RESPONSE_ERROR"); exists { + if apiErrors, ok := apiResponseError.([]*interfaces.ErrorMessage); ok { + return apiErrors + } + } + return nil +} + func actionableAPIResponseErrorStatus(apiErrors []*interfaces.ErrorMessage) int { for i := len(apiErrors) - 1; i >= 0; i-- { errMsg := apiErrors[i] diff --git a/internal/clienterror/client_error.go b/internal/clienterror/client_error.go index f8b0ffe217..ec93a56a19 100644 --- a/internal/clienterror/client_error.go +++ b/internal/clienterror/client_error.go @@ -2,6 +2,7 @@ package clienterror import ( + "bytes" "context" "errors" "net/http" @@ -160,7 +161,7 @@ const GeminiFilterBlockPolicyURL = "https://policies.google.com/terms/generative // Upstream injects this frame inside an HTTP 200 SSE stream with only candidates[0].content.parts[0].text // and without content.role, responseId, modelVersion, or usageMetadata. func GeminiFilterBlockMessage(payload []byte) (string, bool) { - if len(payload) == 0 || !ojson.ValidBytes(payload) { + if len(payload) == 0 || !bytes.Contains(payload, []byte(GeminiFilterBlockMessagePrefix)) || !ojson.ValidBytes(payload) { return "", false } for _, path := range []string{"responseId", "response.responseId", "modelVersion", "response.modelVersion", "usageMetadata", "response.usageMetadata"} { @@ -220,7 +221,7 @@ func isGeminiFilterBlockText(text string) bool { // message object or a streaming message_delta event) and returns the refusal explanation // when stop_details indicates a policy/cyber refusal block. func ClaudePolicyRefusalMessage(payload []byte) (string, bool) { - if len(payload) == 0 || !ojson.ValidBytes(payload) { + if len(payload) == 0 || !bytes.Contains(payload, []byte("stop_details")) || !ojson.ValidBytes(payload) { return "", false } for _, prefix := range []string{"delta", ""} { diff --git a/sdk/api/handlers/claude/code_handlers.go b/sdk/api/handlers/claude/code_handlers.go index 6c5e5dd412..1f862cddf5 100644 --- a/sdk/api/handlers/claude/code_handlers.go +++ b/sdk/api/handlers/claude/code_handlers.go @@ -334,6 +334,10 @@ func (h *ClaudeCodeAPIHandler) handleStreamingResponse(c *gin.Context, rawJSON [ var streamState claudeSSEStreamState streamState.trailingNewlines = 2 streamState.ObserveChunk(chunk) + if streamState.policyRefusalErr != nil && !streamState.loggedTerminalError { + h.LoggingAPIResponseError(c, streamState.policyRefusalErr) + streamState.loggedTerminalError = true + } if len(chunk) > 0 { _, _ = c.Writer.Write(chunk) flusher.Flush() @@ -349,13 +353,15 @@ func (h *ClaudeCodeAPIHandler) handleStreamingResponse(c *gin.Context, rawJSON [ } type claudeSSEStreamState struct { - sawErrorEvent bool - sawMessageStop bool - sawBareMessageJSON bool - trailingNewlines int - currentEvent string - eventData [][]byte - lineBuf bytes.Buffer + sawErrorEvent bool + sawMessageStop bool + sawBareMessageJSON bool + loggedTerminalError bool + policyRefusalErr *interfaces.ErrorMessage + trailingNewlines int + currentEvent string + eventData [][]byte + lineBuf bytes.Buffer } func (s *claudeSSEStreamState) ObserveChunk(chunk []byte) { @@ -429,6 +435,14 @@ func (s *claudeSSEStreamState) observeLine(line []byte) { } func (s *claudeSSEStreamState) observeJSONPayload(payload []byte, hasDataPrefix bool) { + if s.policyRefusalErr == nil { + if refusalMsg, blocked := clienterror.ClaudePolicyRefusalMessage(payload); blocked { + s.policyRefusalErr = &interfaces.ErrorMessage{ + StatusCode: http.StatusBadRequest, + Error: errors.New(refusalMsg), + } + } + } root := util.ParseGJSONBytesNoCopy(payload) switch root.Get("type").String() { case "message_stop": @@ -482,6 +496,10 @@ func (h *ClaudeCodeAPIHandler) forwardClaudeStreamWithState(c *gin.Context, flus return } state.ObserveChunk(chunk) + if state.policyRefusalErr != nil && !state.loggedTerminalError { + h.LoggingAPIResponseError(c, state.policyRefusalErr) + state.loggedTerminalError = true + } _, _ = c.Writer.Write(chunk) }, WriteTerminalError: func(errMsg *interfaces.ErrorMessage) { @@ -489,7 +507,10 @@ func (h *ClaudeCodeAPIHandler) forwardClaudeStreamWithState(c *gin.Context, flus if errMsg == nil { return } - h.LoggingAPIResponseError(c, errMsg) + if !state.loggedTerminalError { + h.LoggingAPIResponseError(c, errMsg) + state.loggedTerminalError = true + } if state.sawErrorEvent || (state.sawMessageStop && clienterror.IsClaudePolicyRefusalError(errMsg.Error)) { state.ensureSSEEventBoundary(c.Writer) return diff --git a/sdk/api/handlers/openai/openai_responses_websocket.go b/sdk/api/handlers/openai/openai_responses_websocket.go index 52e3230739..71e7049670 100644 --- a/sdk/api/handlers/openai/openai_responses_websocket.go +++ b/sdk/api/handlers/openai/openai_responses_websocket.go @@ -201,30 +201,30 @@ func (w *responsesWebsocketWriter) closeWithPayload(payload []byte) (bool, error return true, errClose } -func (w *responsesWebsocketWriter) closeForUpstreamDisconnect(err error, wsTimelineLog ...websocketTimelineAppender) { +func (w *responsesWebsocketWriter) closeForUpstreamDisconnect(err error, wsTimelineLog ...websocketTimelineAppender) bool { if w == nil || w.conn == nil { - return + return false } var timelineLog websocketTimelineAppender if len(wsTimelineLog) > 0 { timelineLog = wsTimelineLog[0] } if matched, _ := w.closeForUpstreamError(err); matched { - return + return true } errMsg := handlers.ExecutionErrorMessage(err) if !shouldExposeResponsesUpstreamError(errMsg) { - if timelineLog != nil && errMsg != nil { + closed, _ := w.closeWithoutError() + if closed && timelineLog != nil && errMsg != nil { appendWebsocketTimelineDisconnect(timelineLog, errMsg.Error, time.Now()) } - _, _ = w.closeWithoutError() - return + return closed } payload, errBuild := buildResponsesWebsocketErrorPayload(errMsg) if errBuild != nil { - _, _ = w.closeWithoutError() - return + closed, _ := w.closeWithoutError() + return closed } wrote, errClose := w.closeWithPayload(payload) if wrote { @@ -240,6 +240,7 @@ func (w *responsesWebsocketWriter) closeForUpstreamDisconnect(err error, wsTimel if errClose != nil && !errors.Is(errClose, websocket.ErrCloseSent) { log.Debugf("responses websocket: upstream disconnect close failed: %v", errClose) } + return wrote } // isWebsocketConnectionClosedError reports whether the error only means the @@ -400,7 +401,8 @@ func (h *OpenAIResponsesAPIHandler) ResponsesWebsocket(c *gin.Context) { log.Infof("responses websocket: client connected id=%s remote=%s", passthroughSessionID, clientIP) wsDone := make(chan struct{}) - defer close(wsDone) + var disconnectWg sync.WaitGroup + var recordedDisconnectErr atomic.Pointer[interfaces.ErrorMessage] var codexDuplexStream atomic.Bool if h != nil && h.AuthManager != nil { @@ -415,7 +417,9 @@ func (h *OpenAIResponsesAPIHandler) ResponsesWebsocket(c *gin.Context) { if subscriber, ok := exec.(upstreamDisconnectSubscriber); ok && subscriber != nil { disconnectCh := subscriber.UpstreamDisconnectChan(passthroughSessionID) if disconnectCh != nil { + disconnectWg.Add(1) go func() { + defer disconnectWg.Done() select { case <-wsDone: return @@ -426,10 +430,9 @@ func (h *OpenAIResponsesAPIHandler) ResponsesWebsocket(c *gin.Context) { // OAuth-only steering still leaves API keys in normal mode. return } - if disconnectErr != nil { - h.LoggingAPIResponseError(context.WithValue(context.Background(), "gin", c), handlers.ExecutionErrorMessage(disconnectErr)) + if writer.closeForUpstreamDisconnect(disconnectErr, wsTimelineLog) && disconnectErr != nil { + recordedDisconnectErr.CompareAndSwap(nil, handlers.ExecutionErrorMessage(disconnectErr)) } - writer.closeForUpstreamDisconnect(disconnectErr, wsTimelineLog) } }() } @@ -450,10 +453,16 @@ func (h *OpenAIResponsesAPIHandler) ResponsesWebsocket(c *gin.Context) { h.AuthManager.CloseExecutionSession(passthroughSessionID) log.Infof("responses websocket: upstream execution session closed id=%s", passthroughSessionID) } + close(wsDone) cancelSocket(nil) + _, _ = writer.closeWithoutError() if errClose := conn.Close(); errClose != nil && !isWebsocketConnectionClosedError(errClose) { log.Warnf("responses websocket: close connection error: %v", errClose) } + disconnectWg.Wait() + if errMsg := recordedDisconnectErr.Load(); errMsg != nil { + h.LoggingAPIResponseError(c, errMsg) + } // Finish control-frame diagnostics before snapshotting the timeline. <-inputDone wsTimelineLog.SetContext(c)