diff --git a/CHANGELOG.md b/CHANGELOG.md index eb819b9..fafd7f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ has a matching GitHub release whose body comes from its section below. ## [Unreleased] +### Fixed + +- A streamed request whose engine never got the card now ends with an error a client can read. The gateway sent + the error object and `[DONE]` as one server-sent event, so a client read the two lines together, failed to parse + them and reported "malformed server-sent event JSON" instead of the reason. They are now two events. (#346) + ## [v0.6.1] - 2026-11-15 ### Fixed diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 8f46b91..fb518a4 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -742,7 +742,9 @@ func ssePreamble(w http.ResponseWriter) { } // sseFailure answers a streaming request whose engine would not come up: -// the same OpenAI-shaped error the 503 path carries, as one SSE event. +// the same OpenAI-shaped error the 503 path carries, as one SSE event, then +// DONE as its own event. A client joins the data lines of one event, so the +// two on one event read as JSON followed by "[DONE]", which does not parse. func (g *Gateway) sseFailure(w http.ResponseWriter, engine, what string, err error) { code := "" var se *startError @@ -751,7 +753,7 @@ func (g *Gateway) sseFailure(w http.ResponseWriter, engine, what string, err err } g.log().Warn("gateway start failed", "engine", engine, "request", what, "code", code, "err", err) b, _ := json.Marshal(berthError(code, err.Error())) - fmt.Fprintf(w, "data: %s\ndata: [DONE]\n\n", b) + fmt.Fprintf(w, "data: %s\n\ndata: [DONE]\n\n", b) _ = http.NewResponseController(w).Flush() } diff --git a/internal/gateway/keepalive_test.go b/internal/gateway/keepalive_test.go index bdcec98..5a119f6 100644 --- a/internal/gateway/keepalive_test.go +++ b/internal/gateway/keepalive_test.go @@ -7,6 +7,7 @@ package gateway import ( "bufio" + "encoding/json" "io" "log/slog" "net/http" @@ -101,6 +102,23 @@ func TestKeepAliveFailureEmitsErrorEvent(t *testing.T) { if strings.Count(s, "data: [DONE]") != 1 { t.Fatalf("more than one DONE: %s", s) } + // An SSE client joins the data lines of one event with "\n" (the + // blank line ends the event), so the error object and DONE must be + // two events, or the client reads one event that is not JSON. + for ev := range strings.SplitSeq(strings.TrimSpace(s), "\n\n") { + var data []string + for line := range strings.SplitSeq(ev, "\n") { + if d, ok := strings.CutPrefix(line, "data: "); ok { + data = append(data, d) + } + } + if len(data) == 0 { + continue + } + if d := strings.Join(data, "\n"); d != "[DONE]" && !json.Valid([]byte(d)) { + t.Errorf("event data is neither JSON nor [DONE]: %q", d) + } + } } // /v1/berth/wait blocks until the engine answers, 404s a model nobody