Commit Graph
2 Commits
Author SHA1 Message Date
yetoneandJoeyWu-tech 7fd77a9445 gateway tests: the 192 streams' dials stay within macOS's listen queue (#3946)
TestPluginStreamsPastTheBudgetWait (#3890) dialed all 192 connections
to its httptest gateway at once. macOS queues at most 128 for a
listener (kern.ipc.somaxconn) and resets the rest, so the test's own
client got "connection reset by peer" and the vendor saw fewer than
192 requests. A dial now holds one of 64 places until the gateway
accepts it (ConnState StateNew) or the dial fails; the 192 streams
still go out and stay open together, and every assertion is as it was.

Patch from Coffemon (JoeyWu-tech) on #3946, 862c6e54, reviewed line by
line. Changed: giving a place back never blocks (select with default),
on the accept side too. The accept runs in the server's accept loop
under httptest's lock, so a blocking receive there could hang the
listener and gw.Close if a connection were ever accepted without a
place (a dial that failed after its handshake).

What was run (mote, macOS, go1.26.8, at origin/main f02d92c7b):
- origin/main, -count=6 -run '^TestPluginStreamsPastTheBudgetWait$':
  red ("dial tcp ...: connect: connection reset by peer", "the vendor
  saw 156 requests, want 192").
- This change: -count=10 ok; -race -count=5 ok.
- Again at origin/main b01193ef9: red 4 of 6 ("the vendor saw 164
  requests, want 192"); with this change -count=10 ok, go vet
  ./internal/gateway/ clean, gofmt -l empty.
- Broken by hand: beginStream refusing at once when the 128 windows
  are taken, as begin does: red 2 of 2 (81 "502 ... too many plugin
  calls are in flight", every route's 502 try), so the test still
  guards #3890's wait.

Not tried: the Windows box; the full suite (test-only change in one
file, run in its package).

Co-authored-by: JoeyWu-tech <joey110551567@gmail.com>
2026-10-28 16:06:07 +08:00
yetone f8cd964aec plugin: a fetch past the host's 128 streams waits for one, not a local 502 (#3890)
The plugin host holds streamBudget/streamWindow = 128 streaming calls'
windows. begin(true) failed the 129th at once with errTooManyCalls, before
anything reached the plugin child, and the gateway counted it as a 502
try and sent the request again. The reporter's 192 concurrent MiniMax
streams through one plugin all came back 200, but over 298 local 502
tries that never reached MiniMax (65 requests retried).

Fetch now takes its window through beginStream: when all 128 are held
it waits for one to come free, outside h.mu (the transition that frees
one takes it), while its request's context lives, the host is up, and
for streamWait (1 minute) at most, past which it is errTooManyCalls as
before. RPCs keep their reserve and still never wait; begin(false) and
begin(true) are unchanged for their other callers.

Tests:
- TestHostFetchWaitsForAStreamWindow (internal/plugin): one window;
  a second fetch waits, runs once the first is released, gives up on
  a cancelled context and after streamWait, and leaves no window taken.
- TestPluginStreamsPastTheBudgetWait (internal/gateway, new file): 192
  concurrent streams through the real Bun plugin host to a local vendor
  that holds each open until 128 are live. Every request is 200 on one
  try, the vendor sees 192 requests and never more than 128 at once.

Run at this head (on ff7048dec):
- gofmt -l internal/plugin internal/gateway: clean
- go vet -tags nogui ./internal/plugin/ ./internal/gateway/ for GOOS
  linux, windows, darwin: ok
- go test -timeout 45m ./internal/plugin/: ok (99.0s, before the rebase
  over two usage/provider commits that don't touch plugin or gateway)
- go test -timeout 45m -run Plugin ./internal/gateway/: ok (52.0s, same)
- After the rebase: TestHostFetchWaitsForAStreamWindow,
  TestHostAdmission*, TestPluginStreamsPastTheBudgetWait: ok
- Broken by hand (beginStream returning errTooManyCalls at once, as
  begin did): TestHostFetchWaitsForAStreamWindow fails ("want it
  waiting"); TestPluginStreamsPastTheBudgetWait fails with 960 lines
  of "a try 502 too many plugin calls are in flight".
- Real account: a sandbox HOME with only the ZCode plugin and its
  account, this head's build on 127.0.0.1:3581, 8 concurrent streaming
  zcode/GLM-5.3-Flash requests: 8 of 8 200, each with its [DONE].

Not tried: 192 real concurrent streams against a vendor (the reporter's
MiniMax setup); the full gateway package (only its Plugin tests).
2026-10-28 00:58:50 +08:00