Files
magpie/internal
yetone 0c17aac2d7 gateway: large request bodies wait their turn for a share of memory (#4004)
One request's limit (512 MB decoded) bounded no sum. Each body is held
while its request is served, with what is made from it (the model
rewritten, a translation to Chat, a plugin's base64 line), and on
jihuayu's 2 GiB server Codex turns of ~45 MiB side by side held the Go
heap at ~500 MiB and the plugin host at ~500 MiB, into swap.

bodyRoom now holds a request body of 1 MiB or more to a share of the
memory magpie may use: a thirty-second of memLimit on Linux (the
cgroup's limit, else MemTotal), of the computer's memory elsewhere, 64
MiB at least; no bound where memory isn't known. A request takes room on
its Content-Length before its body is read, so the ones waiting don't
hold theirs, and on the body read (a compressed one is bigger than it
said). It waits first come first served, a body bigger than the room
runs alone, the room is given back when the request ends, and a body
that waits 100s is answered 503 saying what is held and that the
provider was not asked. The wait is a known one to the pre-routing watch
(noteWait), which otherwise answered it 504 at 30s. Small bodies, an
agent's ordinary turns, pass without counting. On jihuayu's 1,670 MiB
server that is one ~45 MiB body at a time.

Measured on mote, 8 concurrent 44 MiB Responses turns to a Chat-only
fake provider (largeConversation, 1s upstream), peak Go HeapInuse
sampled every 5ms: no room 2,166-2,396 MiB, all 8 in ~2.1s; a 64 MiB
room 459 MiB, all 8 answered 200 in 13.5s. One such turn alone peaks at
337 MiB (the translation's own copies, next).

Run first at efb55b5c9 + this:
  mote: go vet ./internal/gateway/ (darwin, linux, windows): ok
  mote: go test -count=1 -timeout 45m ./internal/gateway/: ok 501s
  mote: go test -race -count=3 -run BodyRoom ./internal/gateway/: ok
  wsl-run (magpie-desktop) and win-run: -run BodyRoom: ok
Broken by hand: letIn ignoring the room turns all six queue tests and
the 503 test red.
Not tried: a real 2 GiB Linux host under Codex traffic; the peak was
measured with a fake provider, not a plugin's host.

Reported-by: jihuayu (#4004)

Rebased onto 874b86653 (7 commits, none in internal/gateway's body
path); again at 874b86653 + this on mote: go vet ./internal/gateway/
for darwin, linux and windows ok; go test -count=1 -timeout 45m
./internal/gateway/ ok 469s; -race -count=3 -run BodyRoom ok.
Rebased again onto 51e3169fb (library, GUI and the Qoder mover only);
at 51e3169fb + this on mote: go vet for the three systems ok, -race
-run "BodyRoom|RequestBody|BodyLimit" ./internal/gateway/ ok.
2026-10-28 19:31:56 +08:00
..
…
…
…
…
…
…