What the reporter saw: generate_image (magpie mcp image) failed on 4K and
2048x1152 with "image 1: context deadline exceeded" and "no image: context
deadline exceeded", while the same request to /v1/images/generations came
back in ~40 s, and usage.jsonl showed the upstream's 200. Their relay
answers with a URL whatever response_format says.
Two causes, one per error:
- "image 1: context deadline exceeded" was generate_image's own download
of the URL (imagemcp.download): 2 minutes for the whole of it, on
http.DefaultTransport, which knows the global proxy but not a provider's
own (#237). A 10 MB PNG that was still coming was cut, and the error
dropped the URL of an image already drawn and paid for. Any earlier
image of n was saved but not said either.
- "no image: context deadline exceeded" (the usage log's 502s at
300002-300032 ms) was the gateway's drawTimeout (5 min) expiring while
it read a vendor's answer that had already begun: sendWith passed the
bare context error up. The vendor really took over 5 minutes there;
this commit doesn't change that limit, it says what happened.
What changed:
- gateway: an images caller that asks response_format "b64_json" gets the
image itself. One the vendor gave only a URL for is fetched by the
gateway's own client, through the provider's proxy (p.Via), after the
draw and outside drawTimeout. One that can't be fetched is answered by
its URL, as before, never dropped. A caller that asks no format still
gets the URL. Nothing is sent to the vendor differently.
- gateway.FetchImage: an image download gives up when nothing has come
for ImageStall (1 min) or it has taken ImageLongest (5 min), and says
which and how much had come. A slow download goes on while it moves.
The edits' fetchPicture uses it too.
- gateway: a vendor that began answering but hadn't finished in
drawTimeout is a 504 "<name> began answering after Ns (200 OK) but
hadn't sent all of its answer in 5m0s", not a 502 "context deadline
exceeded".
- imagemcp: asks for b64_json; downloads a URL the gateway still hands it
with FetchImage; an image it can't have is answered with the vendor's
URL to download it from, beside the ones saved (an error only when
none was). Its client to the gateway waits ToolTimeout (11 min), past
the draw and the fetch together, not 6 minutes.
What was run:
- Reproduced on the old code: the new TestFourKImageByURLIsSaved (a real
3840x2160 PNG, 9.8 MB, served by URL in 256 KB pieces over ~4 s) with
the old download's 2-minute cap scaled to 1 s fails with the
reporter's exact "image 1: context deadline exceeded"; the stalled
case fails the same way, with no URL in the error.
- Broke each part by hand and watched its test go red: no inline fetch,
fetch not through the provider's proxy, the bare deadline message, the
stall timer not reset (a total cap again), a lost image ending the
call, the MCP not asking b64_json.
- go test ./internal/imagemcp/ ./internal/gateway/ ; the full
go test -tags nogui ./... ; gofmt -l ; GOOS=linux and GOOS=windows
go vet on both packages.
- On the Windows box (Windows 11 x64): imagemcp.test.exe all passed, and
gateway.test.exe for the new tests and the images/ModelScope tests.
Not tried: the reporter's relay itself (never contacted), and a real
4K draw end to end. Why their URL took over 2 minutes from the MCP when
their own download took 7.5 s isn't known from here: a provider proxy
the MCP didn't use is one way it happens; a slow file host is another.
Both are covered now.