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.
This commit is contained in:
sk-dev-ai committed 2026-10-14 17:26:33 +05:30
1 parent 61769a9b9f
commit 406107b053
1 file changed
+16
+16
View File
@@ -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
}