diff --git a/handlers/anthropic.go b/handlers/anthropic.go index 7dc9711..2fc02a9 100644 --- a/handlers/anthropic.go +++ b/handlers/anthropic.go @@ -299,6 +299,9 @@ func handleStreaming( } // Stream the SSE response + // Capture values before SetBodyStreamWriter — the callback runs in a + // separate goroutine where fiber.Ctx is no longer valid. + clientIP := c.IP() transformer := transform.NewStreamTransformer("", "") c.Set("Content-Type", "text/event-stream") c.Set("Cache-Control", "no-cache") @@ -320,17 +323,17 @@ func handleStreaming( event := transformer.TransformChunk(chunk) if event != "" { if _, err := w.WriteString(event); err != nil { - logger.Warn("[ANTHROPIC] Stream write error [IP: %s]: %v", c.IP(), err) + logger.Warn("[ANTHROPIC] Stream write error [IP: %s]: %v", clientIP, err) return } if err := w.Flush(); err != nil { - logger.Warn("[ANTHROPIC] Stream flush error [IP: %s]: %v", c.IP(), err) + logger.Warn("[ANTHROPIC] Stream flush error [IP: %s]: %v", clientIP, err) return } } } if err := scanner.Err(); err != nil { - logger.Warn("[ANTHROPIC] Stream scan error [IP: %s]: %v", c.IP(), err) + logger.Warn("[ANTHROPIC] Stream scan error [IP: %s]: %v", clientIP, err) return } // Ensure final events are sent