From 75d40ba48bcc4b215de044c78ce226ed47f4f510 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 7 Nov 2026 18:22:07 +0800 Subject: [PATCH] ci: race gateway tests run in chunks taken in turn; 64 MB bodies are 20 MB under -race The macOS -race gateway step split 3772 tests by sorted name into one fixed share per core (3 on macos-latest), and the shares ran out of their 20 minutes with no test hung: run 42354008450 at 191f3c0af ended at 12m, 16m and over 20m (shard 2 in TestMediaJSONPastOldBodyLimit/images), and 42350695944 ran all three at ~19m20s. Two causes: the shares were uneven, and a few tests scan tens of MB byte by byte, which the race detector makes 14 to 30 times as slow. Measured on mote (M5 Pro), -race, -test.v, old script, per test (plain run in brackets): TestLoopGuardLeavesOrdinaryRepliesAlone 104s (7.6s), TestLargeRequestEndpoints 104s (3.6s), TestRequestTailAfterOldLimit 33s (4s), TestCodexBodyReadsPastOldLimit 14.6s (0.55s), TestMediaJSONPastOldBodyLimit 10.7s (0.7s), TestCodexCompressedTailError 10.2s. Sums of test time per share: 487s, 641s, 761s. - build/race-shards.sh: the tests go in ~118 chunks of about 32 (test N in chunk N mod m, so TestCodex..., TestCompaction... spread out), and each of the N workers takes the next chunk when it is done (xargs -P). Workers end within a chunk of each other. Each chunk logs in its own ::group:: with its time; the five slowest are printed at the end. A chunk whose process didn't finish counts as failed. - oldBodyLimit (the pre-#118 64 MB) is 20 MB under -race (race_on_test.go), 64 MB in the plain run. 20 MB still passes every cut below 64 MB (bodyKept's 4 MB, the 8 MB and 16 MB reads); the plain Test step on ubuntu and macOS still sends the full 64 MB. - TestLoopGuardLeavesOrdinaryRepliesAlone is skipped under -race: one goroutine feeds one loopGuard (loop_guard.go starts none), so there is no race to find; the plain run reads the whole corpus. After, per test on mote under -race: TestLargeRequestEndpoints 40.7s, TestRequestTailAfterOldLimit 13.05s, TestCodexBodyReadsPastOldLimit 5.1s, TestMediaJSONPastOldBodyLimit 4.46s, TestCodexCompressedTailError 3.1s, TestLoopGuard skipped: ~210s less race time. A/B in one window on mote (same commit, binaries built from base and change, 3 workers, load 15 to 30): old script with old tests 12m59s wall (red: TestTranslatedStreamKeepsClientAlive, the load-timing test of #2711/#2737); new script with new tests 10m57s, green, slowest chunk 62s. On 3-core CI the wall should be about the total over three plus a chunk, instead of the slowest share. Broken by hand (wsl-run, Linux): defaultBodyLimit 16<<20 under -race turns TestCodexBodyReadsPastOldLimit, TestLargeRequestEndpoints and TestRequestTailAfterOldLimit red (body rejected / 413); defaultBodyLimit 48<<20 in the plain run turns the same three red. So the race run guards the read-whole path past 16 MB and the plain run still guards 64 MB. TestCodexCompressedTailError and TestMediaJSONPastOldBodyLimit stay green with either cap: they never guarded the limit (true before this change). Run: gofmt -l; on mote GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go vet ./..., go vet -race ./internal/gateway; the seven touched tests plain on mote (all pass, 21.9s); the whole gateway package under -race through the new script on mote twice (one red, TestBurstTellsNoSpeed/responses_from_chat_burst, the load timing of #2719; the second run green). Not tried: the race run with a 48 MB cap (it would pass: under -race nothing now covers 20 to 64 MB, the plain step does); a CI run before the push. Fixes #5361 Fixes #5320 --- .github/workflows/test.yml | 5 +- build/race-shards.sh | 73 +++++++++++++++++++-------- internal/gateway/loop_guard_test.go | 7 +++ internal/gateway/race_off_test.go | 4 ++ internal/gateway/race_on_test.go | 11 ++++ internal/gateway/request_body_test.go | 6 +-- internal/gateway/request_idle_test.go | 2 +- 7 files changed, 81 insertions(+), 27 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d852dd10a..a8571fb40 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -91,8 +91,9 @@ jobs: # The redaction tests reuse this binary in fresh processes to check # first-use races, so it must include the race detector. The gateway's - # tests can't call t.Parallel (t.Setenv), so its suite runs split over - # one process per core (#923); each process isolates its own home. + # tests can't call t.Parallel (t.Setenv), so its suite runs in chunks, + # one process per core at a time, each taking the next chunk when done + # (#923, #5361); each process isolates its own home. - name: Race if: runner.os != 'Windows' shell: bash diff --git a/build/race-shards.sh b/build/race-shards.sh index 398f92041..c1520bfb2 100755 --- a/build/race-shards.sh +++ b/build/race-shards.sh @@ -1,31 +1,64 @@ #!/usr/bin/env bash # race-shards.sh DIR BIN N [FLAGS]: runs the tests of BIN, a package's test -# binary (go test -c), from the package's DIR in N processes at once, the -# tests split round-robin by name, and fails if any process does. FLAGS go -# to every process. A package's tests run one at a time unless they call -# t.Parallel, which t.Setenv rules out, so the gateway's suite under the -# race detector keeps one core busy; N processes keep N busy. Each process -# isolates its own home (the package's TestMain). +# binary (go test -c), from the package's DIR in N processes at once, and +# fails if any test process does. FLAGS go to every process. A package's +# tests run one at a time unless they call t.Parallel, which t.Setenv rules +# out, so the gateway's suite under the race detector keeps one core busy; +# N processes keep N busy. Each process isolates its own home (the +# package's TestMain). +# +# The tests go in chunks of about RACE_CHUNK (default 32), and each of the +# N workers takes the next chunk when it is done with one. A +# fixed split of the tests into N shares left one share with the slow +# tests: on macOS CI the gateway's three took 12, 16 and over 20 minutes +# (#5361, #5320), while chunks taken in turn end within a chunk of each +# other. Each chunk is a process of its own, ~1s of start-up under -race. set -euo pipefail pkg=$1 bin=$2 n=$3 shift 3 +size=${RACE_CHUNK:-32} dir=$(mktemp -d) trap 'rm -rf "$dir"' EXIT (cd "$pkg" && "$bin" -test.list '.*') | grep -E '^(Test|Example|Fuzz)' | sort >"$dir/all" -echo "$(wc -l <"$dir/all" | tr -d ' ') tests in $n processes" -pids=() -for i in $(seq 0 $((n - 1))); do - awk -v n="$n" -v i="$i" 'NR % n == i' "$dir/all" >"$dir/s$i" - re="^($(paste -sd'|' "$dir/s$i"))\$" - (cd "$pkg" && "$bin" -test.run "$re" "$@" >"$dir/log$i" 2>&1) & - pids+=($!) -done +total=$(wc -l <"$dir/all" | tr -d ' ') +# the Nth test goes in chunk N mod m: tests named alike are often slow +# alike (TestCodex..., TestCompaction...), and spread they don't make one +# chunk that ends long after the rest +m=$(((total + size - 1) / size)) +awk -v m="$m" -v d="$dir" '{f = sprintf("%s/c.%04d", d, NR % m); print >> f; close(f)}' "$dir/all" +printf '%s\n' "$dir"/c.* >"$dir/chunks" +echo "$total tests in $(wc -l <"$dir/chunks" | tr -d ' ') chunks of $size, $n at a time" + +# run CHUNK DIR BIN FLAGS...: one chunk's tests; its log and its status and +# seconds go beside it. +cat >"$dir/run" <<'EOF' +#!/usr/bin/env bash +c=$1 pkg=$2 bin=$3 +shift 3 +re="^($(paste -sd'|' "$c"))\$" +start=$(date +%s) +if (cd "$pkg" && "$bin" -test.run "$re" "$@") >"$c.log" 2>&1; then st=ok; else st=FAIL; fi +echo "$st $(($(date +%s) - start))" >"$c.st" +EOF +chmod +x "$dir/run" + +start=$(date +%s) +# a chunk whose process didn't finish has no status, and fails below +xargs -P "$n" -I{} "$dir/run" {} "$pkg" "$bin" "$@" <"$dir/chunks" || true fail=0 -for i in "${!pids[@]}"; do - if wait "${pids[$i]}"; then st=ok; else st=FAIL fail=1; fi - echo "::group::shard $i: $st ($(wc -l <"$dir/s$i" | tr -d ' ') tests)" - cat "$dir/log$i" +while read -r c; do + st=FAIL secs=? + if [ -f "$c.st" ]; then read -r st secs <"$c.st"; fi + echo "::group::${c##*/} $st ($(wc -l <"$c" | tr -d ' ') tests, ${secs}s): $(head -1 "$c") .. $(tail -1 "$c")" + cat "$c.log" 2>/dev/null || true echo "::endgroup::" - if [ $st = FAIL ]; then grep -E '^(--- FAIL|FAIL|panic|WARNING: DATA RACE)' "$dir/log$i" || true; fi -done + if [ "$st" != ok ]; then + fail=1 + grep -E '^(--- FAIL|FAIL|panic|WARNING: DATA RACE)' "$c.log" 2>/dev/null || echo "${c##*/}: no status, its process didn't finish" + fi +done <"$dir/chunks" +echo "$total tests in $(($(date +%s) - start))s; the slowest chunks:" +while read -r c; do + if [ -f "$c.st" ]; then echo "$(cut -d' ' -f2 "$c.st")s ${c##*/}: $(head -1 "$c") .. $(tail -1 "$c")"; fi +done <"$dir/chunks" | sort -rn | head -5 || true exit $fail diff --git a/internal/gateway/loop_guard_test.go b/internal/gateway/loop_guard_test.go index 9a34bb6e7..30a63fefd 100644 --- a/internal/gateway/loop_guard_test.go +++ b/internal/gateway/loop_guard_test.go @@ -240,6 +240,13 @@ func TestLoopGuardReadsEveryProtocol(t *testing.T) { // docs and their tables, a log, a numbered list, a CSV, a hex table, // base64 and a run of rules and braces. func TestLoopGuardLeavesOrdinaryRepliesAlone(t *testing.T) { + if raceOn { + // one goroutine feeding one loopGuard, so the race detector has + // nothing to find, and it made this corpus take 104s of the + // gateway's race run on an M5 Pro, 7s without (#5361); the plain + // Test step reads all of it + t.Skip("one goroutine: no race to find, and 14 times as slow under -race") + } root := filepath.Join("..", "..") var texts = map[string]string{} for _, f := range []string{ diff --git a/internal/gateway/race_off_test.go b/internal/gateway/race_off_test.go index a5fbbb347..47f219300 100644 --- a/internal/gateway/race_off_test.go +++ b/internal/gateway/race_off_test.go @@ -3,3 +3,7 @@ package gateway const raceOn = false + +// oldBodyLimit is the request body limit magpie had before #118: a body +// past it is read whole (race_on_test.go says why -race sends less). +const oldBodyLimit = 64 << 20 diff --git a/internal/gateway/race_on_test.go b/internal/gateway/race_on_test.go index efeac588e..2d8f0188b 100644 --- a/internal/gateway/race_on_test.go +++ b/internal/gateway/race_on_test.go @@ -5,3 +5,14 @@ package gateway // raceOn says the tests run under -race, where sync.Pool lets a quarter of // what is put in it go, so a test of what a pool saves is skipped. const raceOn = true + +// oldBodyLimit is 64 MB, the request body limit magpie had before #118, +// in the plain run. Under -race it is 20 MB: the race detector makes a +// scan of the 64 MB bodies take 25 to 30 times as long, and the five +// tests that send one took ~170s of the gateway's race run on an M5 Pro +// and pushed a macOS CI shard past its 20 minutes (#5361, #5320). 20 MB +// still passes every cut the request path makes below 64 MB (bodyKept's +// 4 MB, the 8 MB and 16 MB reads), so the race run takes the same +// branches; the plain Test step sends the full 64 MB, one byte either +// side of it too. +const oldBodyLimit = 20 << 20 diff --git a/internal/gateway/request_body_test.go b/internal/gateway/request_body_test.go index 834c0b840..b6cb0574b 100644 --- a/internal/gateway/request_body_test.go +++ b/internal/gateway/request_body_test.go @@ -15,11 +15,9 @@ import ( "github.com/yetone/magpie/internal/provider" ) -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 +// -race only, for a test that serves a body of 20 MB or more: the race +// detector made the scans of a 64MB body 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. diff --git a/internal/gateway/request_idle_test.go b/internal/gateway/request_idle_test.go index 3742ed406..2897ae386 100644 --- a/internal/gateway/request_idle_test.go +++ b/internal/gateway/request_idle_test.go @@ -113,7 +113,7 @@ func TestMCPBodyTooLarge(t *testing.T) { } func TestMediaJSONPastOldBodyLimit(t *testing.T) { - payload := append(bytes.Repeat([]byte(" "), 64<<20), []byte(`{"model":"fake/m1","prompt":"tail"}`)...) + payload := append(bytes.Repeat([]byte(" "), oldBodyLimit), []byte(`{"model":"fake/m1","prompt":"tail"}`)...) for _, api := range []string{"images", "videos"} { t.Run(api, func(t *testing.T) { r := httptest.NewRequest("POST", "/", bytes.NewReader(payload))