diff --git a/handlers/anthropic.go b/handlers/anthropic.go index 94b98fb..d2a4414 100644 --- a/handlers/anthropic.go +++ b/handlers/anthropic.go @@ -271,10 +271,10 @@ func handleStreaming( ) error { // Direct HTTP request to upstream (bypass proxy.Forward for streaming) ctx, cancel := context.WithTimeout(c.Context(), time.Duration(cfg.RequestTimeoutSeconds)*time.Second) - defer cancel() req, err := http.NewRequestWithContext(ctx, "POST", targetURL, bytes.NewReader(body)) if err != nil { + cancel() return c.Status(fiber.StatusInternalServerError).SendString("build request failed") } for key, vals := range headers { @@ -287,11 +287,13 @@ func handleStreaming( client := &http.Client{} resp, err := client.Do(req) if err != nil { + cancel() logger.Warn("[ANTHROPIC] Stream error [IP: %s]: %v", c.IP(), err) return c.Status(fiber.StatusBadGateway).SendString("upstream error") } if resp.StatusCode != 200 { + cancel() defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) logger.Warn("[ANTHROPIC] Stream upstream %d [IP: %s]: %s", resp.StatusCode, c.IP(), string(body)) @@ -308,6 +310,7 @@ func handleStreaming( c.Set("Connection", "keep-alive") c.Response().SetBodyStreamWriter(func(w *bufio.Writer) { + defer cancel() defer resp.Body.Close() scanner := bufio.NewScanner(resp.Body)