Files
magpie/docs
yetone 0024660445 gateway: a key's slot is taken before an account's turn near its cap
ox0pine on X: a gateway key held to one provider sometimes hung "in
routing". With the key's MaxConcurrency set and its account within 5
points of a usage cap, two turns could each wait for what the other
held:

- Turn B took the account's near-cap turn, then the key's slot (kept for
  the whole request, key_slots.go), and got a passing failure (503).
- B slept and went back to the same account (i--). At the top of the
  loop it gave the near-cap turn back.
- Turn A, waiting for that turn, took it and waited in keySlot for the
  key's slot, which B held.
- B found the turn taken, was the last candidate and waited in
  nearCap.take, which A held. Neither could move until its agent hung up.
  Neither had a try that could start, so the GUI showed "routing…".

serve now takes the key's slot before the near-cap turn, so locks are
always taken key, near-cap turn, lane. The wait still counts in the try's
Queued (nearInKey). Eager routes already took the slot before serve.
forward and codexUpstreamOn take it in their handlers and never take a
near-cap turn.

TestKeySlotAndNearCapTurnNeverWaitOnEachOther: one Codex account at 89%
with a 90% cap, a key held to codex/* at MaxConcurrency 1. The first
try gets a 503, and a second turn is sent while the first is paused.

Run on mote (mote-run):
- the new test on the old serve: red 5 of 5 (both clients time out at
  8s). Green after, and green without the key's cap on the old code.
- -run 'NearCap|Key|Cap' -count=2: ok.
- go vet ./internal/gateway for darwin, GOOS=linux and GOOS=windows;
  gofmt -l clean.
- rebased on origin/main f5f68b433, then again: -run 'NearCap|Key|Cap'
  ok (70s), vet darwin/linux/windows ok, gofmt clean.
- full go test ./internal/gateway -timeout 45m through lockf
  ~/suite.lock: not run. It waited 1h17m behind other sessions' suites
  for the lock and is still queued; shipped without it (owner: never
  hold a verified fix behind a full suite).

Not the reporter's case. They later said (v0.1.3371) the key's one
provider is a custom API-key provider, and the row clears by itself in
about a minute. This hang needs a subscription account near a usage cap
and never clears by itself. A probe of a custom key provider behind a
held key (Models plan/*, Accounts held to one of two keys; plain, 429s
and patience, MaxRPM 1, MaxConcurrency 1) had a try in the trace that
matched the order at every 150ms poll, so the Routing page would read
"answering", not "routing…". Their ~60s wait is not found yet.
2026-11-09 18:15:24 +08:00
..