From e710160df1399dc193c304c8fb02418f46c6fdaa Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 5 Nov 2026 03:37:20 +0800 Subject: [PATCH] gateway: a request body a little over a doubling is not copied whole again for its last bytes (#5281) readSized grows its slice by doubling up to the declared length. A body just over a power of two was read into 1+2+...+512 MB and then copied whole into a final 516 MB slice for its last 4 MB: about 1.5 GB allocated and copied outside the body's wait, which counts toward the 30s unrouted cut. TestCodexGeneratedImagesPastOldLimit sends such a 516 MB turn and was answered 504 on main CI's Race step (34s). Now, once the declared length is within a quarter of the next doubling, the slice grows straight to it. The worst case over all lengths goes from 3x the body allocated to 2.6x (computed over every length up to 600 MB in 16 KB steps). A body shorter than it said is still capped by what it declared. Run on mote: - new TestRequestBodyNotCopiedForItsLastBytes (4 MB + 64 KB in 64 KB reads, at most twice its size allocated): red on the old code, 11600416 bytes for 4259840; green on the fix. - go test -race -run 'TestCodexGeneratedImagesPastOldLimit|TestRequestBody| TestDecodeJSON|TestJSONValid|Held|TestReadSized|LargeBody|OldLimit' ./internal/gateway: ok 114.9s, with this commit and the two after it. - go vet ./internal/gateway on darwin, GOOS=linux and GOOS=windows: ok. gofmt -l: clean. - The full suite and race shards are recorded in the last commit of this set. Not tried: a real Codex client sending a 516 MB turn. --- internal/gateway/request_bounds.go | 22 ++++++++++++++++------ internal/gateway/request_sized_test.go | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/internal/gateway/request_bounds.go b/internal/gateway/request_bounds.go index 740908ffc..32880a25a 100644 --- a/internal/gateway/request_bounds.go +++ b/internal/gateway/request_bounds.go @@ -280,11 +280,13 @@ const sizedReadFirst = 1 << 20 // copies them into a slice of their sum, two copies of every body, and an // agent sends its whole conversation, ~1 MB, on every turn (yg6888 on X: // ~1 GB). Content-Length is not trusted for the allocation: room grows by -// doubling, from sizedReadFirst, up to what was declared, so an honest body -// of 1 MB or less is read into one slice of its own size, a larger one into -// a final slice of its own size, and a body shorter than it said costs at -// most about twice what it sent. A body longer than it said reads on, as -// io.ReadAll would. The error is io.ReadAll's: nil at io.EOF. +// doubling, from sizedReadFirst, up to what was declared, and straight to +// it once that is within a quarter, so an honest body of 1 MB or less is +// read into one slice of its own size, and a larger one into a final slice +// of its own size, with at most 2.6 times the body allocated in all (a +// doubling that fell just short of it made that 3). A body shorter than it +// said is held in at most two and a half times what it sent. A body longer +// than it said reads on, as io.ReadAll would. The error is io.ReadAll's: nil at io.EOF. func readSized(r io.Reader, declared int64) ([]byte, error) { if declared <= 0 { return io.ReadAll(r) @@ -307,7 +309,15 @@ func readSized(r io.Reader, declared int64) ([]byte, error) { } continue } - b = append(make([]byte, 0, min(int64(cap(b))*2, declared)), b...) + next := min(int64(cap(b))*2, declared) + if declared-next < next/4 { + // within a quarter of what it said: a doubling that + // fell just short would copy the whole body again for + // its last few bytes (a 516 MB turn read into 512 MB, + // then all of it into 516 MB, #5281) + next = declared + } + b = append(make([]byte, 0, next), b...) } n, err := r.Read(b[len(b):cap(b)]) b = b[:len(b)+n] diff --git a/internal/gateway/request_sized_test.go b/internal/gateway/request_sized_test.go index 48887d13f..4239448c3 100644 --- a/internal/gateway/request_sized_test.go +++ b/internal/gateway/request_sized_test.go @@ -70,6 +70,24 @@ func TestRequestBodyReadOnceAtItsLength(t *testing.T) { } } +// A body a little over a doubling of the first slice is not copied whole +// again for its last bytes: a Codex turn of 516 MB of generated images was +// read into 1+2+…+512 MB and then all of it into 516 MB, 1.5 GB allocated +// and copied outside the body's wait, and under -race on a loaded CI +// machine the turn was answered 504 for magpie's own work (#5281). +func TestRequestBodyNotCopiedForItsLastBytes(t *testing.T) { + sent := bytes.Repeat([]byte("0123456789abcdef"), (4*sizedReadFirst+64<<10)/16) // 4 MB + 64 KB + var got []byte + var err error + read := allocated(func() { got, err = readSized(&chunked{sent, 64 << 10}, int64(len(sent))) }) + if err != nil || !bytes.Equal(got, sent) || cap(got) != len(got) { + t.Fatalf("read %d bytes (cap %d), %v", len(got), cap(got), err) + } + if limit := uint64(len(sent)) * 2; read > limit { + t.Fatalf("a %d-byte body allocated %d bytes to read, over twice its size", len(sent), read) + } +} + // Content-Length is the client's word: a body shorter than it said costs // no more than the first slice, one longer reads on, a failed read is // returned as io.ReadAll returns it, and no length is io.ReadAll.