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.