diff --git a/backend/internal/transport/http/inference/voice_ws_handler.go b/backend/internal/transport/http/inference/voice_ws_handler.go index 7e7c5075..691a1038 100644 --- a/backend/internal/transport/http/inference/voice_ws_handler.go +++ b/backend/internal/transport/http/inference/voice_ws_handler.go @@ -116,25 +116,34 @@ func (h *Handler) proxyVoiceWebSocket(c *gin.Context, pathValue string) { first := <-errCh if !isNormalVoiceWSClose(first.result.err) { outcomeMu.Lock() - if (first.upstreamSide && !first.result.writeFailed) || (!first.upstreamSide && first.result.writeFailed) { - outcome.ErrorCode = "upstream_stream_interrupted" - outcome.UpstreamFailed = true - } else { - outcome.ErrorCode = "client_stream_interrupted" + // A request-context watcher may have closed both sockets first. Keep + // its timeout/cancellation outcome instead of replacing it with the + // close error produced by our own cleanup. + if outcome.ErrorCode == "" { + if (first.upstreamSide && !first.result.writeFailed) || (!first.upstreamSide && first.result.writeFailed) { + outcome.ErrorCode = "upstream_stream_interrupted" + outcome.UpstreamFailed = true + } else { + outcome.ErrorCode = "client_stream_interrupted" + } } outcomeMu.Unlock() } } -// watchRequestTimeout calls closeAll once ctx is done, recording errorCode as -// "request_timeout" unless some other outcome was already set. Extracted so -// the cancellation-triggers-cleanup behavior can be tested without a real -// WebSocket upgrade. +// watchRequestTimeout closes the session once its request context is done. +// Deadline cancellation is the configured request timeout; ordinary +// cancellation is kept distinct so client disconnects and server shutdowns +// are not reported as timeouts. Existing transport outcomes always win. func watchRequestTimeout(ctx context.Context, mu *sync.Mutex, errorCode *string, closeAll func()) { <-ctx.Done() mu.Lock() if *errorCode == "" { - *errorCode = "request_timeout" + if ctx.Err() == context.DeadlineExceeded { + *errorCode = "request_timeout" + } else { + *errorCode = "request_canceled" + } } mu.Unlock() closeAll() diff --git a/backend/internal/transport/http/inference/voice_ws_timeout_test.go b/backend/internal/transport/http/inference/voice_ws_timeout_test.go index 8c93d579..ce5651f3 100644 --- a/backend/internal/transport/http/inference/voice_ws_timeout_test.go +++ b/backend/internal/transport/http/inference/voice_ws_timeout_test.go @@ -59,3 +59,26 @@ func TestWatchRequestTimeoutDoesNotOverwriteAnExistingOutcome(t *testing.T) { t.Fatalf("errorCode = %q, want the pre-existing outcome preserved", got) } } + +func TestWatchRequestTimeoutDistinguishesRequestCancellation(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + + var mu sync.Mutex + errorCode := "" + closed := make(chan struct{}) + go watchRequestTimeout(ctx, &mu, &errorCode, func() { close(closed) }) + cancel() + + select { + case <-closed: + case <-time.After(time.Second): + t.Fatal("watchRequestTimeout did not close the canceled session") + } + + mu.Lock() + got := errorCode + mu.Unlock() + if got != "request_canceled" { + t.Fatalf("errorCode = %q, want request_canceled", got) + } +}