Files
magpie/internal/plugin/host_heavy_test.go
T
yetone 7cce4b4d78 plugin: a host holding over 512 MiB is replaced by a new one
A headless magpie on a small Linux server had its shared plugin host at
783 MiB after 2h25m (main process 117 MiB), and the kernel OOM-killed it.
Four plugins went through it (cursor, kiro, qoder, workbuddy). Nothing
bounded the host: no heap cap, no collection when idle, no restart.

The host now checks its RSS every minute, from ten minutes after it
started. Past 512 MiB it runs a full collection (Bun.gc(true)), and if it
is still past, it says so ("heavy"). magpie then retires it as a plugin
update does (Restart): the calls it is answering finish on it, and the
next call starts a new host.

What was measured (host RSS; bench driver = host.js run as magpie runs
it, a fake vendor, 4 requests at a time, every 5th a JSON reply, every
7th cancelled mid-stream):
- fake plugin, 200KB bodies, 4000 requests: macOS 25 -> 115 MiB flat,
  Linux (WSL) 47 -> 55-77 MiB flat.
- through an HTTPS CONNECT proxy, 600KB CJK bodies, 3000 requests:
  macOS 138 -> 156 MiB, flat from ~1000.
- WorkBuddy's real fetch (gzip path) through the proxy, cursor, kiro,
  qoder and workbuddy loaded with models.dev's 5.9MB catalog, 3000
  600KB CJK requests: macOS 101 -> 218 MiB (creeping ~7KB a request),
  Linux 92 -> 131 MiB, 84 idle. --smol: macOS 236, Linux 92 idle: no
  better, so it is not used.
- Bun's node:http2 client (Cursor's transport), 1500 sessions opened
  and closed: 58-60 MiB flat.
- 2000 provider listings with all four plugins: 64 -> 130 MiB flat.
None of these reach 783 MiB, so the cause in the report was not found;
the bound leaves 2x room over the largest of them. A plugin made to keep
each request's body grew the old host to 1048 MiB with nothing said; the
new host said "heavy" at 512 MiB (Linux).

Tests: TestHeavyHostIsReplaced (a 1 MiB bound: the host is replaced and
a call after it is answered) is red on the old code (not replaced 30s
on) and green now; TestLightHostIsKept checks a host under the bound is
left alone through fifty checks.

Run at this tree: gofmt -l (nothing); go vet ./..., GOOS=linux go vet
-tags nogui ./..., GOOS=windows go vet ./..., go build for darwin,
linux (nogui) and windows on mote; go test ./internal/plugin/ on mote
(ok, 117s); the two new tests -count=3 on mote (-race), WSL and Windows
(ok). Not run before this push: the full suite (go test -tags nogui
-timeout 45m ./...), queued on mote behind five other sessions' suites
on its suite lock; pushed now, as the owner asked fixes not wait on it.

Not tried: a real signed-in Cursor, Kiro or Qoder account's requests and
background work (sign-in renewal, usage, model lists) under load; the
reporter's server itself.
2026-11-08 21:43:52 +08:00

94 lines
2.6 KiB
Go

package plugin
import (
"context"
"path/filepath"
"testing"
"time"
)
// heavyFor sets the host's memory bound for one test (heavyMB and its
// timing), checked every 20ms from the host's start.
func heavyFor(t *testing.T, mb int) {
t.Helper()
was, after, every := heavyMB, heavyAfter, heavyEvery
heavyMB, heavyAfter, heavyEvery = mb, 0, 20*time.Millisecond
t.Cleanup(func() { heavyMB, heavyAfter, heavyEvery = was, after, every })
}
func currentHost() *host {
hostMu.Lock()
defer hostMu.Unlock()
return current
}
// A host holding more than heavyMB after a full collection is replaced:
// it ends, and the next call is answered by a new host. A host that grew
// with what its plugins kept was otherwise left to grow until the system
// killed it (783 MiB after 2h25m, OOM-killed on a small Linux server).
// Any host holds more than 1 MiB.
func TestHeavyHostIsReplaced(t *testing.T) {
heavyFor(t, 1) // restored after sandbox's Settle has ended the hosts
sandbox(t)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/fake/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
was := recycles.Load()
if _, err := Providers(ctx); err != nil {
t.Fatal(err)
}
until := time.Now().Add(30 * time.Second)
for recycles.Load() == was {
if time.Now().After(until) {
t.Fatal("a host past its memory bound was not replaced 30s on")
}
time.Sleep(20 * time.Millisecond)
}
// the one replaced ends, and a new host answers (replaced in its turn,
// past the bound too)
until = time.Now().Add(30 * time.Second)
for {
hostMu.Lock()
n := len(retiring)
hostMu.Unlock()
if n == 0 {
break
}
if time.Now().After(until) {
t.Fatal("the replaced host was still finishing its calls 30s on")
}
time.Sleep(20 * time.Millisecond)
}
if err := Call(ctx, "providers", map[string]any{}, nil); err != nil {
t.Fatalf("a call after the heavy host was replaced: %v", err)
}
}
// A host under the bound is left alone, however often it checks.
func TestLightHostIsKept(t *testing.T) {
heavyFor(t, 1<<20)
sandbox(t)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/fake/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
was := recycles.Load()
if _, err := Providers(ctx); err != nil {
t.Fatal(err)
}
h := currentHost()
if h == nil {
t.Fatal("no host")
}
// fifty checks
time.Sleep(time.Second)
if recycles.Load() != was || currentHost() != h || !h.alive() {
t.Fatal("a host under its memory bound was replaced")
}
}