From 406107b053da313171212f956e5e8c05fd1b0384 Mon Sep 17 00:00:00 2001 From: sk-dev-ai Date: Wed, 14 Oct 2026 17:26:33 +0530 Subject: [PATCH] fix: report header-phase client disconnects as 499 client_canceled, not 502 A downstream disconnect during the upstream attempt loop is not an upstream failure, but the gateway answered it with 502 upstream_error on both the message endpoints and System One. Clients that retry on 502 then retried a request nobody cancelled, and telemetry counted a client behavior as a server failure. Check wire.ClientCanceled before the generic 502: set meta.Outcome so the request line reads client_canceled, and answer 499 with a client_canceled envelope. DeadlineExceeded keeps its 504. --- internal/gateway/gateway.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 497e669..4e47f93 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -196,6 +196,15 @@ func (g *Gateway) handleInference(external wire.Protocol) http.HandlerFunc { wire.WriteError(w, external, http.StatusGatewayTimeout, "upstream request timed out", "upstream_timeout", ids.Request) return } + // A downstream disconnect is not an upstream failure: report the + // real cause so telemetry and clients see a cancel, not a 502. + if wire.ClientCanceled(r.Context(), err) { + if meta != nil { + meta.Outcome = "client_canceled" + } + wire.WriteError(w, external, 499, "client closed request", "client_canceled", ids.Request) + return + } wire.WriteError(w, external, http.StatusBadGateway, "all upstream attempts failed", "upstream_error", ids.Request) return } @@ -354,6 +363,13 @@ func (g *Gateway) forwardSystemOne(w http.ResponseWriter, r *http.Request, body wire.WriteError(w, wire.SystemOne, http.StatusGatewayTimeout, "upstream request timed out", "upstream_timeout", ids.Request) return } + if wire.ClientCanceled(r.Context(), err) { + if meta != nil { + meta.Outcome = "client_canceled" + } + wire.WriteError(w, wire.SystemOne, 499, "client closed request", "client_canceled", ids.Request) + return + } wire.WriteError(w, wire.SystemOne, http.StatusBadGateway, "all upstream attempts failed", "upstream_error", ids.Request) return }