Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions internal/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}

Expand Down
18 changes: 18 additions & 0 deletions internal/gateway/keepalive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package gateway

import (
"bufio"
"encoding/json"
"io"
"log/slog"
"net/http"
Expand Down Expand Up @@ -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
Expand Down
Loading