gateway: the 64MB body tests get room before routing under -race only (#3957)

TestLargeRequestEndpoints and TestRequestTailAfterOldLimit went red on
main's Race jobs (macOS at ec6c2de52 and 56602426d, ubuntu at
874b86653): the Codex backend subtest's oldBodyLimit body was answered
magpie's own 504, "held this request for 30s without routing it".

Profiled on mote (M5 Pro): the subtest takes 0.89s without -race and
22.4s with it. 62% of the CPU is the race runtime itself and the rest is
the body's scans before routing (skipJSONSpace, decodeJSONIn, trimJSON,
bodyEffort, metadataOf), each instrumented per read. CI runners are
slower and share the shard, so 50 to 58s. A user's machine sees the
0.89s, far inside the 30s cut, so the cut is right and the test's
clock under -race is what's wrong.

largeBodyRoom lifts unroutedUnknown and unroutedMost to 10 minutes when
raceOn, for the three tests that serve such a body through the handler
(TestLargeRequestEndpoints, TestRequestTailAfterOldLimit,
TestCodexCompressedTailError). Without -race the 30s cut stays, so a
scan made 30x slower still fails them.

Run at this tree (origin/main 0dc4cec85..):
- Reproduced: with no room and a 10s cut under -race, the Codex subtest
  gives CI's message word for word ("held this request for 10s").
- With the room, -race: the three tests and TestUnrouted* ok (179s).
  Without -race: those three and TestCodexBodyReadsPastOldLimit ok (10s).
- go vet ./internal/gateway, with -tags race, GOOS=linux, GOOS=windows ok;
  gofmt -l clean.
Not run: the full ./... suite; the shard's 20m budget on CI (the next
Race run shows it).
This commit is contained in:
yetone
2026-10-28 21:19:08 +08:00
parent 012c584cc1
commit 29181eb78e
+17
View File
@@ -9,6 +9,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/klauspost/compress/zstd"
"github.com/yetone/magpie/internal/provider"
@@ -16,6 +17,19 @@ import (
const oldBodyLimit = 64 << 20
// largeBodyRoom lifts the cut on a request held before routing, under
// -race only, for a test that serves an oldBodyLimit body: the race
// detector makes the scans of its 64MB before routing take 20s on an M5
// Pro and near 60s on CI, where without it they take under a second, so
// the 30s cut answered 504 (#3957). Without -race the cut stays, and a
// slower scan still fails the test.
func largeBodyRoom(t *testing.T) {
if raceOn {
unknownLimit(t, 10*time.Minute)
unroutedLimits(t, unroutedShow, 10*time.Minute)
}
}
func encodedBody(t *testing.T, body []byte, encoding string) []byte {
t.Helper()
switch encoding {
@@ -63,6 +77,7 @@ func TestCodexBodyReadsPastOldLimit(t *testing.T) {
}
func TestLargeRequestEndpoints(t *testing.T) {
largeBodyRoom(t)
const payload = `{"model":"fake/m1","input":"END!","messages":[{"role":"user","content":"END!"}],"contents":[{"role":"user","parts":[{"text":"END!"}]}]}`
// Padding keeps the test about reading the whole body, not token limits.
body := append(bytes.Repeat([]byte(" "), oldBodyLimit), payload...)
@@ -119,6 +134,7 @@ type bodyReadError struct{}
func (bodyReadError) Read([]byte) (int, error) { return 0, errors.New("body read failed") }
func TestRequestTailAfterOldLimit(t *testing.T) {
largeBodyRoom(t)
const payload = `{"model":"fake/m1","input":"hi","messages":[],"contents":[]}`
body := append([]byte(payload), bytes.Repeat([]byte(" "), oldBodyLimit-len(payload))...)
for _, path := range []string{CodexPath + "/responses", "/v1/responses", "/v1/messages/count_tokens", "/v1beta/models/fake/m1:generateContent"} {
@@ -160,6 +176,7 @@ func TestRequestTailAfterOldLimit(t *testing.T) {
}
func TestCodexCompressedTailError(t *testing.T) {
largeBodyRoom(t)
const payload = `{"model":"fake/m1","input":"hi"}`
body := append([]byte(payload), bytes.Repeat([]byte(" "), oldBodyLimit-len(payload))...)
for _, encoding := range []string{"gzip", "zstd"} {