gateway: an image answer that stalls says whether it was coming as a url or as b64_json, how much had come and how long nothing more had (#1576, david-zdw)

david-zdw found their relay answers the same size as a 416 B url on one
domain and as a 3 MB b64_json body taking 380 s on another. The 504 "began
answering after … but hadn't sent all of its answer in 5m0s" didn't say
which, and the two point at different fixes: a url answer stuck after its
start, or the image's own bytes coming too slowly.

Change: sendWith reads the vendor's body through a reader that notes when
its last bytes came. On the deadline the error goes on: "it was sending
the image itself as b64_json; 3.0 MB had come, and nothing more for the
last 40s" (or "it was answering with the image's url, not the image; …",
or that what came doesn't yet say which).

Run:
- TestStalledAnswerSaysUrlOrB64 (a b64_json body and a url body, each
  stopping mid-answer), TestSizeOf, and TestVendorThatStopsMidAnswerIsSaid
  updated for the 21 B it got; the draw/fetch tests pass.
- go test ./internal/gateway, gofmt, GOOS=linux/windows go vet -tags nogui.

Not done: b64_json is still read whole into memory, not decoded to disk as
it comes. The image is passed on as bytes all the way to the agent's
answer, so streaming it would change every drawer; left for its own change.
Not tried against the reporter's relay.
This commit is contained in:
yetone
2026-10-11 19:59:15 +08:00
parent e9ae318589
commit bac81385ab
2 changed files with 83 additions and 3 deletions
+44 -2
View File
@@ -755,12 +755,13 @@ func (s *Server) sendWith(ctx context.Context, p provider.Provider, method, url,
}
defer res.Body.Close()
answered := time.Since(asked)
b, err := io.ReadAll(io.LimitReader(res.Body, 256<<20))
rb := &tracked{r: res.Body, last: time.Now()}
b, err := io.ReadAll(io.LimitReader(rb, 256<<20))
if err != nil {
if ctx.Err() != nil {
// not a bare "context deadline exceeded" (#1576): the vendor
// began answering, and its answer is what ran out of time
return nil, 504, fmt.Errorf("%s began answering after %s (%d %s) but hadn't sent all of its answer in %s", p.Name, answered.Round(time.Second), res.StatusCode, http.StatusText(res.StatusCode), drawTimeout)
return nil, 504, fmt.Errorf("%s began answering after %s (%d %s) but hadn't sent all of its answer in %s: %s", p.Name, answered.Round(time.Second), res.StatusCode, http.StatusText(res.StatusCode), drawTimeout, stalledAt(b, time.Since(rb.last)))
}
return nil, 502, err
}
@@ -774,6 +775,47 @@ func (s *Server) sendWith(ctx context.Context, p provider.Provider, method, url,
return b, res.StatusCode, nil
}
// tracked is a body that notes when its last bytes came.
type tracked struct {
r io.Reader
last time.Time
}
func (t *tracked) Read(p []byte) (int, error) {
n, err := t.r.Read(p)
if n > 0 {
t.last = time.Now()
}
return n, err
}
// stalledAt says where an answer that didn't finish stopped (#1576): how
// much had come, how long nothing more had, and whether the image was
// coming as a link (so the answer itself stuck, after its start) or as
// b64_json (so it was the image's own bytes that came too slowly). A relay
// may answer one size with either, so the two point at different fixes.
func stalledAt(got []byte, quiet time.Duration) string {
where := fmt.Sprintf("%s had come, and nothing more for the last %s", sizeOf(len(got)), quiet.Round(time.Second))
switch {
case bytes.Contains(got, []byte(`"b64_json"`)):
return "it was sending the image itself as b64_json; " + where
case bytes.Contains(got, []byte(`"url"`)):
return "it was answering with the image's url, not the image; " + where
}
return "what came doesn't yet say whether the image was coming as a url or as b64_json; " + where
}
// sizeOf is n bytes for a reader: 386 B, 12.4 KB, 3.0 MB.
func sizeOf(n int) string {
switch {
case n < 1000:
return fmt.Sprintf("%d B", n)
case n < 1000*1000:
return fmt.Sprintf("%.1f KB", float64(n)/1000)
}
return fmt.Sprintf("%.1f MB", float64(n)/1000/1000)
}
// vendorMessage is the message of a vendor's error, or its body.
func vendorMessage(b []byte) string {
var e struct {
+39 -1
View File
@@ -155,7 +155,45 @@ func TestVendorThatStopsMidAnswerIsSaid(t *testing.T) {
}
})
code, _, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"relay/gpt-image-2.5-sunburst","prompt":"a 4K city"}`)
if code != 504 || !strings.Contains(raw, "Relay began answering after 0s (200 OK) but hadn't sent all of its answer in 300ms") {
if code != 504 || !strings.Contains(raw, "Relay began answering after 0s (200 OK) but hadn't sent all of its answer in 300ms: what came doesn't yet say whether the image was coming as a url or as b64_json; 21 B had come") {
t.Fatalf("%d %s", code, raw)
}
}
// david-zdw's relay (#1576) answered 2048x1152 on one domain with a 3 MB
// b64_json body that took 380 s, and on another with a 416 B url. Which of
// the two stalled is said, with how much had come.
func TestStalledAnswerSaysUrlOrB64(t *testing.T) {
was := drawTimeout
drawTimeout = 500 * time.Millisecond
t.Cleanup(func() { drawTimeout = was })
for _, c := range []struct {
name, sent, want string
}{
{"b64_json", `{"created":1,"data":[{"b64_json":"iVBORw0KGgo` + strings.Repeat("A", 3000) + `"`, "it was sending the image itself as b64_json; 3.0 KB had come, and nothing more for the last "},
{"url", `{"created":1,"data":[{"url":"https://files.example/a.png"}]`, "it was answering with the image's url, not the image; 59 B had come"},
} {
t.Run(c.name, func(t *testing.T) {
s, _ := relayed(t, func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, c.sent)
w.(http.Flusher).Flush()
select {
case <-r.Context().Done():
case <-time.After(5 * time.Second):
}
})
code, _, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"relay/gpt-image-2.5-sunburst","prompt":"a 2K city","size":"2048x1152"}`)
if code != 504 || !strings.Contains(raw, c.want) {
t.Fatalf("%d %s", code, raw)
}
})
}
}
func TestSizeOf(t *testing.T) {
for n, want := range map[int]string{386: "386 B", 12400: "12.4 KB", 3_000_000: "3.0 MB", 19_700_000: "19.7 MB"} {
if got := sizeOf(n); got != want {
t.Errorf("sizeOf(%d) = %q, want %q", n, got, want)
}
}
}