Files
magpie/internal/gateway/request_bounds_test.go
T
yetone 694f4a6d65 gateway: a Codex turn of its own reads past 512 MB, to a sixteenth of the machine's memory (dallascao)
dallascao on Discord: generating images in Codex and revising them again
and again, magpie answered 413 "request body is over magpie's gateway
limit of 512 MB (decoded); the provider was not asked" on
127.0.0.1:3425/backend-api/codex/responses, while Codex signed in to
OpenAI directly went on. The count was right: Codex keeps each image it
generated in the conversation (a function_call_output input_image, a
~3 MB PNG as ~4 MB base64; codex-rs ext/image-generation to_response_item,
and history keeps image_generation_call results), and over HTTP sends the
conversation whole every turn, so ~130 images is 512 MB decoded. To
OpenAI directly Codex uses its WebSocket and sends a delta.

- Codex's own path (codexBackend, the Codex relay, the Responses
  WebSocket's read limit, keyLimited on CodexPath) reads to
  codexBodyLimit: a sixteenth of the machine's memory (memLimit, else
  proc.MemTotal), 512 MB at least, 2 GB at most. 16 GB: 1024 MB,
  24 GB: 1536 MB, 32 GB+: 2048 MB, 8 GB or unknown: 512 MB as before.
  It is still the bomb guard: nothing decodes past it, and the zstd
  decoder keeps its window bound.
- A turn for a magpie model ("provider/model") on that path is held to
  the 512 MB of every other route once its model is read: it is
  translated, at several times its size. Its 413 says one of Codex's own
  models takes up to codexBodyLimit; writeError's /compact hint stays.
- provider.codexNamesBad read tools/input/tool_choice with
  GetManyBytes, which copied the whole input (the body's size: 300 MB of
  a 300 MB turn in the alloc profile). It reads in place now.

Cost at the largest input (mote, a relayed gpt-5.5 turn of random
~3 MB images, heap objects sampled every 5ms, load ~350): 300 MB took
4.7s, 600 MB 7.7s, 1000 MB 11.9s, peak heap 1.5 to 2.1 GB past the
body's own bytes. The rest of the cost is io.ReadAll growing the decoded
body and fillJSONIn/exchangeOf; bodyRoom still runs a body bigger than
its room alone.

Run at the pushed head (mote): gofmt -l clean; GOOS=linux and
GOOS=windows go vet ./internal/gateway ./internal/provider ok;
go test -timeout 45m ./internal/provider ok, ./internal/gateway ok but
TestCompactionOverByteCapEveryTime, red in the full package run only
(#4862, filed before this; passes alone 3 of 3 here).
TestRequestBodyTooLarge/codex now sends codexBodyLimit+1.
- TestCodexGeneratedImagesPastOldLimit (16 GB machine fixture, a 516 MB
  turn of generated images in Codex's shape): reaches the fake ChatGPT
  backend whole in ~6s; the same turn for x/gpt-5 is 413 naming
  "one of Codex's own models takes up to 1024 MB".
- TestCodexBodyLimitFromMemory: 2/8/16/24/64 GiB and unknown memory.
Broken by hand: codexBackend reading with limit 0 (the old code) makes
TestCodexGeneratedImagesPastOldLimit red with the reporter's 413; the
magpie-model check turned off makes it red (404 from routing, not 413).

Not tried: the reporter's own session or a real Codex image-generation
run through magpie; a turn past 1 GB on a 16 GB machine; the WebSocket's
raised read limit has no test of its own.
2026-11-03 16:05:53 +08:00

267 lines
7.4 KiB
Go

package gateway
import (
"bufio"
"bytes"
"compress/gzip"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/klauspost/compress/zstd"
"github.com/yetone/magpie/internal/provider"
)
func TestRequestBodyTooLarge(t *testing.T) {
for _, api := range []string{"chat", "responses", "messages", "count", "gemini", "codex", "images", "edits", "videos"} {
t.Run(api, func(t *testing.T) {
s := New()
r := httptest.NewRequest("POST", "/", strings.NewReader("{}"))
r.ContentLength = defaultBodyLimit + 1
if api == "codex" { // Codex's own turns are read to more
r.ContentLength = codexBodyLimit() + 1
}
w := httptest.NewRecorder()
switch api {
case "chat":
s.handle(provider.Chat)(w, r)
case "responses":
s.handle(provider.Responses)(w, r)
case "messages":
s.handle(provider.Anthropic)(w, r)
case "count":
s.countTokens(w, r)
case "codex":
s.codexBackend(w, r)
case "images":
s.images(false)(w, r)
case "edits":
s.images(true)(w, r)
case "videos":
s.videosCreate(w, r)
case "gemini":
r.SetPathValue("call", "m:generateContent")
s.gemini(w, r)
}
if w.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("status = %d, want 413", w.Code)
}
})
}
}
func TestRequestBodySocketTimeout(t *testing.T) {
s := New()
s.requestLimits.readTimeout = 40 * time.Millisecond
gw := httptest.NewServer(s.handle(provider.Chat))
defer gw.Close()
conn, err := net.Dial("tcp", strings.TrimPrefix(gw.URL, "http://"))
if err != nil {
t.Fatal(err)
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(2 * time.Second))
fmt.Fprintf(conn, "POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 20\r\n\r\n{")
res, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 408 {
t.Fatalf("slow body: status %d", res.StatusCode)
}
}
func TestRequestBodyDeadlineCleared(t *testing.T) {
s := New()
s.requestLimits.readTimeout = 30 * time.Millisecond
var connections atomic.Int64
gw := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
deadlines := []time.Time{}
_, ok := s.requestBody(deadlineRecorder{w, &deadlines}, r, provider.Chat)
if len(deadlines) < 3 || !deadlines[len(deadlines)-1].IsZero() {
t.Errorf("body deadline was not cleared: %v", deadlines)
}
if !ok {
return
}
io.WriteString(w, "first\n")
w.(http.Flusher).Flush()
time.Sleep(100 * time.Millisecond)
// The read deadline must be cleared before the first response byte;
// otherwise the server closes keepalive input during a long stream.
io.WriteString(w, "last\n")
}))
gw.Config.ConnState = func(_ net.Conn, state http.ConnState) {
if state == http.StateNew {
connections.Add(1)
}
}
gw.Start()
defer gw.Close()
for i := 0; i < 2; i++ {
res, err := gw.Client().Post(gw.URL, "application/json", strings.NewReader("abc"))
if err != nil {
t.Fatal(err)
}
b, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil || string(b) != "first\nlast\n" {
t.Fatalf("long response = %q, %v", b, err)
}
}
if n := connections.Load(); n != 1 {
t.Fatalf("streaming deadline broke keepalive: %d connections", n)
}
}
func TestCodexExpandedBodyBound(t *testing.T) {
for _, enc := range []string{"gzip", "zstd"} {
t.Run(enc, func(t *testing.T) {
var compressed bytes.Buffer
if enc == "gzip" {
zw := gzip.NewWriter(&compressed)
zw.Write(bytes.Repeat([]byte("x"), 2048))
zw.Close()
} else {
zw, err := zstd.NewWriter(&compressed)
if err != nil {
t.Fatal(err)
}
zw.Write(bytes.Repeat([]byte("x"), 2048))
zw.Close()
}
s := New()
s.requestLimits.body = 1024
r := httptest.NewRequest("POST", CodexPath+"/responses", &compressed)
r.Header.Set("Content-Encoding", enc)
w := httptest.NewRecorder()
s.codexBackend(w, r)
if w.Code != 413 {
t.Fatalf("inflated %s: status %d", enc, w.Code)
}
})
}
}
func TestRequestBodyUnknownLength(t *testing.T) {
s := New()
s.requestLimits.body = 16
for _, size := range []int{16, 17} {
r := httptest.NewRequest("POST", "/", strings.NewReader(strings.Repeat("x", size)))
r.ContentLength = -1
w := httptest.NewRecorder()
body, ok := s.requestBody(w, r, provider.Chat)
if size == 16 && (!ok || len(body) != size) {
t.Fatalf("exact limit: %d, %v", len(body), ok)
}
if size == 17 && (ok || w.Code != 413) {
t.Fatalf("unknown length overflow: %d, %v", w.Code, ok)
}
}
}
func TestRequestBodyReadError(t *testing.T) {
s := New()
r := httptest.NewRequest("POST", "/", nil)
r.Body = io.NopCloser(io.MultiReader(strings.NewReader("abc"), failedBodyReader{}))
w := httptest.NewRecorder()
if _, ok := s.requestBody(w, r, provider.Chat); ok || w.Code != 400 {
t.Fatalf("read failure: status %d", w.Code)
}
}
type failedBodyReader struct{}
func (failedBodyReader) Read([]byte) (int, error) { return 0, io.ErrUnexpectedEOF }
// The read deadline ends with the body, before any generation/stream starts.
type deadlineRecorder struct {
http.ResponseWriter
deadlines *[]time.Time
}
func (w deadlineRecorder) Unwrap() http.ResponseWriter { return w.ResponseWriter }
func (w deadlineRecorder) SetReadDeadline(at time.Time) error {
*w.deadlines = append(*w.deadlines, at)
return http.NewResponseController(w.ResponseWriter).SetReadDeadline(at)
}
func TestRejectedBodySocketDoesNotDrain(t *testing.T) {
s := New()
s.requestLimits.body = 4
gw := httptest.NewServer(s.handle(provider.Chat))
defer gw.Close()
conn, err := net.Dial("tcp", strings.TrimPrefix(gw.URL, "http://"))
if err != nil {
t.Fatal(err)
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(2 * time.Second))
fmt.Fprintf(conn, "POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 20\r\n\r\n{")
res, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusRequestEntityTooLarge {
t.Fatalf("rejected slow input: status %d", res.StatusCode)
}
}
func TestRequestBodyLargeConversations(t *testing.T) {
s := New()
payload := bytes.Repeat([]byte("x"), 30<<20)
// The old retained 256 MiB budget rejected the ninth 30 MiB conversation.
for i := 0; i < 9; i++ {
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", bytes.NewReader(payload))
body, ok := s.requestBody(w, r, provider.Chat)
if !ok || len(body) != len(payload) {
t.Fatalf("conversation %d refused: status %d", i+1, w.Code)
}
}
}
func TestRequestBodiesDoNotLimitStreams(t *testing.T) {
s := New()
stop := make(chan struct{})
gw := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, ok := s.requestBody(w, r, provider.Chat); !ok {
return
}
io.WriteString(w, "started\n")
w.(http.Flusher).Flush()
select {
case <-stop:
case <-r.Context().Done():
}
}))
defer gw.Close()
defer close(stop)
client := &http.Client{Timeout: 10 * time.Second}
var responses []*http.Response
defer func() {
for _, r := range responses {
r.Body.Close()
}
}()
// The old budget refused stream 129 before routing. Each handler stays live.
for i := 0; i < 160; i++ {
res, err := client.Post(gw.URL, "application/json", strings.NewReader("{}"))
if err != nil {
t.Fatal(err)
}
responses = append(responses, res)
if res.StatusCode != http.StatusOK {
t.Fatalf("stream %d refused: %d", i+1, res.StatusCode)
}
}
}