Commit Graph
2 Commits
Author SHA1 Message Date
yetoneandggbdpq 4375805354 gateway tests: the long plugin reader's deadline follows the reply's progress, not the wall clock (#3286)
TestPluginStreamLongActiveReader went red under -race on macOS CI
(run 42449115269 at 691a10446, 75.54s): "SSE read after 38977536
content bytes: context deadline exceeded (Client.Timeout ...)". The
test's client carried a fixed 75s Client.Timeout over the whole
request, and the reply it reads is paced at 2.5MiB/s, so 40MB takes
16s on an idle machine and whatever a loaded -race runner makes of it
otherwise. The reader was still reading when the deadline cut it.

Change (test only):
- pluginReplyGateway's client has no total deadline. pluginReplyComplete
  arms a 60s idle guard (context.WithCancelCause) that progress resets:
  a frame the fake vendor wrote, an SSE event with content reaching the
  client, or, for a JSON reply, a read with more than whitespace in it.
  A reply that stops part way is red with "nothing moved for 1m0s"
  beside the read error.
- Wire bytes alone are not progress. ggbdpq's patch on the issue (the
  base of this change) reset the guard on any read with bytes; with that
  rule and the vendor's writes, broken by hand (credit never given back),
  the test was still waiting at 168s, because the gateway's ": keepalive"
  lines (every 15s, for up to 5 minutes) kept resetting it.
- TestPluginStreamLargeReplies runs through the same guard;
  TestPluginStreamStalledClientKeepsOtherRequestsMoving's second request
  keeps its own 3s context.
- The test's comment no longer speaks of a stall window: the host's
  credit window replaced it, and the test now says so.

Timing assumptions left: 60s with no progress anywhere is a stall
(progress arrives every few ms at the slowest pace here), and the
12s floor on elapsed is held by the reader's own pacing (16s minimum).

Run on mote (go1.26.8), base 1d5b5dc2d:
- Reproduced by capping the reader at 512KiB/s (1<<19) on the old
  code: red at 75.18s, "SSE event after 38723584 content bytes:
  unexpected end of JSON input" (the 75s deadline cut a line).
- The same cap on this change: ok, 81.45s, all 41943040 content bytes
  (vendor done after 72s).
- Broken by hand, host.credit a no-op: red at 65.43s, "SSE read after
  491520 content bytes: nothing moved for 1m0s".
- Broken by hand, the body given up 10s after its first read (the old
  stall window, ignoring progress): red at 10.19s, "SSE error after
  18219008 content bytes: ... the consumer stalled past the window".
- go test -race -count=1 ./internal/gateway: ok (1805s).
- gofmt -l internal/gateway: clean; GOOS=linux and GOOS=windows go vet
  ./internal/gateway: ok.
- Full suite: not run at push.

Fixes #3286

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-08 15:11:41 +08:00
Snowyy fa1e3947f9 plugin: isolate stalled streams and guard GUI refreshes (#664)
* plugin,gui: a stalled stream can't wedge the host, stale reads can't draw, and the browser suite runs in CI

internal/plugin: the host has one reader of the child's stdout, and it handed
each message to its call by sending on that call's channel. A caller that
stopped draining a streamed body (a slow HTTP client on a plugin-backed chat
stream) filled the channel; the reader then blocked, and every other call
through the host -- all plugin providers and accounts, moved built-ins
included -- stopped being dispatched until the stalled stream was drained. A
second call's answer arrived only after the first was drained.

Each call now queues its head and chunks in a bounded backlog (4 MiB of
chunks, 4096 of them) drained by its own reader, so the host's reader never
waits on one consumer; a call whose consumer stops reading is given up on
alone, with the limit as why. Aborts go through a goroutine too (abortLoop),
so a child that stopped reading its stdin can't make an abort wait either.
host_test.go covers the stalled consumer, the bounds and the abort path.

internal/gui: loadUsage() and refreshUsage() both read the Overview's
usage?period=, and neither checked after the await that its answer was still
the newest: a 24 h answer landing after a 7 d one drew the old period's
figures under the new period's name, and leaving the tab could still write the
Overview's cost into the shared header. Both now claim one shared read
generation before the request and draw only when it is still the newest and
the page, tab and period are unchanged. providersWhileFetching() also stopped
for good after one failed poll or reload; it now asks again.

usage-stale-answer.test.cjs and providers-fetching-retry.test.cjs lift the
real functions out of app.js and drive them with a faked api/page/timers, so
they need no browser; usage-refresh-flight.test.cjs gains the browser case for
the same race.

Makefile/.github: `make test-ui` runs every internal/gui/tests/*.test.cjs
through Node's test runner -- auto-discovered, UI_TEST_CONCURRENCY files at a
time (default 2) -- and a new `ui` job in Test runs it on ubuntu-latest with
Playwright's Chromium and WebKit. A missing browser fails the job; it never
skips to a pass. internal/gui/tests/README.md points at `make test-ui` instead
of a hand-kept command line.

Verified: node --test on the two new files 21/21; go test -count=1
./internal/plugin/ ok; go test -race -count=1 ./internal/plugin/ ok; go vet
./internal/plugin/ and go build ./... clean; node --check on the changed JS;
actionlint on the workflow.

Not verified: the full browser suite is not green yet. `make test-ui` is 1641
passed / 70 failed of 1711 across 225 files, and 12 of the 19 failing files
fail the same way on a pristine main (a known baseline: a fixture boots the
page in web mode while asserting the desktop window's narrow behaviour). The
`ui` job will be red until those baselines are dealt with -- nothing is
skipped or masked -- and the job has not yet run on a GitHub-hosted runner.

* plugin: distinguish stalled readers from large responses

* plugin: clarify best-effort abort delivery
2026-10-04 11:22:31 +08:00