magpie icon indicating copy to clipboard operation
magpie copied to clipboard

TestMaxRPM* fail on a busy machine: the window is 1.5s and the checks leave ~0.75s

Open yetone opened this issue 1 month ago • 6 comments

Seen on 2026-10-12 at load average 55-62 (other agents' test runs on the same Mac), in a full go test -tags nogui ./internal/gateway and alone with -run TestMaxRPM -count=5:

--- FAIL: TestMaxRPMWaitsForRoomInTheMinute (2.21s)
    rpm_test.go:135: the second waited 783.288416ms with room in the minute
--- FAIL: TestMaxRPMTooLongIs429WithRetryAfter (2.23s)
    rpm_test.go:160: second = 429 Retry-After "1" ... want a 429 saying the limit, Retry-After 2

Both pass 8 of 8 alone on origin/main 17c12621b and on my branch once the load fell to ~44, so they are timing, not a change in the RPM limiter. The cause: window := 1500 * time.Millisecond, and the checks allow window/2 for a request that should not wait (750ms), and expect Retry-After to round to exactly 2 — the first request taking more than 0.5s under load makes the remaining wait round to 1. A user on a busy machine meets the same: Retry-After is computed from the time left, so the exact-value check is the test's assumption, not a guarantee. Suggested fix: compare Retry-After against the time left measured in the test (a range, not "2"), and give the no-wait bound room independent of the window (or a longer window).

Not fixed here: found while running the suite for #1772; filing per LESSONS (a red under load is filed with its output).

yetone avatar Oct 12 '26 11:10 yetone

Seen again on 10-15 at load average 15-27, in a full go test -p 4 ./... on 707fbb249's parent tree, with a different shape: TestMaxRPMTooLongIs429WithRetryAfter got second = 200 Retry-After "". The first request took long enough under load that the 1.5s window had already passed. With -run TestMaxRPM -count=3 it passes alone. I'm not taking this; it's still unowned.

yetone avatar Oct 15 '26 02:10 yetone

Took this one (state was OPEN and unowned per your 10-15 comment; git log --grep=1791 shows no fix on main as of b1d02fa6). Since the repo doesn't take pull requests, the fix is on a branch of my fork for cherry-picking — one commit (aeaf6703) covers this, #1829, #1813 and #1812:

git fetch https://github.com/Niko-Wu/magpie fix/timing && git cherry-pick aeaf6703

(based on current b1d02fa6, single commit, test files only)

Root cause — exactly what the issue says: the window is 1.5s and the checks leave ~0.75s, and Retry-After is pinned to exactly "2".

  • internal/gateway/rpm_test.go (old): window := 1500ms in TestMaxRPMWaitsForRoomInTheMinute, TestMaxRPMTooLongIs429WithRetryAfter and TestMaxRPMCountsRetriesAndCounts; the no-wait checks bound at window/2 (750ms) — a loaded machine spent the half-second before the second request went out; and the 429 check pins Retry-After == "2", which assumes the first request took under 0.5s — a slow first request makes the remaining wait round to 1 (or, your 10-15 shape: the whole window passes at the gateway and the second goes 200 with no header).

The fix (the RPM window really is a time limit, so the bounds stay but get room on both sides):

  • The window in those three tests is now 4s (rpm_test.go:123, :152, :225). The no-wait bound is window/2 (2s), and a request that wrongly waits only reaches the vendor after the full span (window + slack), so a real over-wait is still red with room either way; the lower-bound check on the third request (at[2]-at[0] < window) is unchanged. Your 10-15 shape (second = 200, Retry-After "") now needs the first request to spend the whole 4s at the gateway — 2.7x the 1.5s it spent at load 15-27 — and if it still happens the failure names the 200 and the empty header rather than a wrong Retry-After.
  • Retry-After is no longer the literal "2": the test measures when the vendor stamped the first request, adds the window it set, and checks Retry-After is that time left rounded up, ±1 for the rounding, never below 1 (rpm_test.go:164-174). A wrong Retry-After is still caught; a slow first request is not.

Red → green (reproduced by injecting the delay, on origin/main 707fbb24, through the existing minuteVendor.late knob — the dial lag the slack is for):

  • TestMaxRPMWaitsForRoomInTheMinute: with a 900ms late first request, the second waited 918.9ms against window/2 → red at rpm_test.go:135, your "checks leave ~0.75s" shape.
  • TestMaxRPMTooLongIs429WithRetryAfter: same injection → Retry-After "1" against the pinned "2" → red at rpm_test.go:160, matching the issue. With the fix, both pass under the same injection.

Verification

  • The changed tests, go test -tags nogui -count=3 on internal/gateway (classifier + TestMaxRPM set): 21 passed.
  • Full package go test -tags nogui: internal/gateway 4192 passed on the final tree.
  • gofmt -l clean; go vet -tags nogui clean for GOOS=linux and GOOS=windows.
  • Not run: -race; CI's machines; a run at load 90+.

Niko-Wu avatar Oct 15 '26 03:10 Niko-Wu

Seen again at d0ccbbebc + 9a852ae9d (macOS, full suite -p 4, gateway package ran 1108s): rpm_test.go:246: a count with no room: 200 after 2.719805291s. Same shape as filed: the 1.5s window under a busy machine. Not taken by me — the netproxy fix in that run doesn't touch the gateway.

yetone avatar Oct 15 '26 03:10 yetone

Seen again 10-16 ~18:00 local in a full ./internal/gateway run at load ~205: rpm_test.go:160: second = 429 Retry-After "1" ... want a 429 saying the limit, Retry-After 2 (the second request came after the first had used ~0.5s of the 1.5s window, so Retry-After rounded to 1). Passes alone at load ~240 (52.5s with TestPluginStreamLargeReplies). Not taking it in this change (the /v1/models lookup fix); noting the occurrence only.

yetone avatar Oct 16 '26 10:10 yetone

Seen again 2026-10-19 on macOS, same run as the #2551 note (load average about 200):

--- FAIL: TestMaxRPMCountsRetriesAndCounts (3.27s)
    rpm_test.go:249: the vendor saw 4, want the count estimated

yetone avatar Oct 18 '26 17:10 yetone

Seen again at 197849ad4's parent (image-timeout change, rebased on origin/main), go test ./internal/gateway/ under a load of ~180:

--- FAIL: TestMaxRPMTooLongIs429WithRetryAfter (1.43s)
    rpm_test.go:160: second = 429 Retry-After "1" {"error":{..."message":"rl: 1 requests a minute is its limit, and the next could go in 1s, longer than a request waits (0s); try again then",...}}
        , want a 429 saying the limit, Retry-After 2

Taking it now: a session is fixing it at its cause (the Retry-After check assumes the second request lands in the first ~0.5s of the 1.5s window).

yetone avatar Oct 19 '26 13:10 yetone