acezan on Discord (v0.1.2819, Linux x86_64, a systemd service): after 47 hours, 57 <defunct> magpie-bun children of magpie, growing toward the unit's TasksMax until a restart. One cause: the plugin host's only cmd.Wait ran in host.read after its stdout reached EOF. A program a plugin started that kept a copy of the host's stdout (Bun.spawn given Bun.stdout, which host.js's spawn guard doesn't catch, #5063; a daemon left behind) held that pipe open past the host's end, so EOF never came, Wait never ran, the host was never marked dead, and every host stopped (Restart, a moved proxy, hostStale) stayed a zombie until magpie quit. Reproduced on Linux (WSL magpie-desktop, Bun 1.4.2): one more Z-state bun under the test process per Restart, 1, 2, 3, 4 over four rounds. The other cause, magpie web's restart to update leaving the running host behind its exec (#3285), is the next commit. - proc.Output (new, internal/proc/output.go) gives a command's stdout and stderr pipes of magpie's own and waits for the command the moment it ends. Its readers then have OutputGrace (2s) to read the last lines; the pipes are closed after that, which ends a read held up by a grandchild. startOn uses it, and a reap goroutine fails the host when its child ends. host.read no longer calls Wait. The tests' fake hosts (no child) keep cmd.Wait. - Every other start that wasn't waited for now goes through proc.StartReaped (Start, then go cmd.Wait()). Process.Release doesn't reap on Unix: codexrelay.Start (a relay that can end while magpie runs), tui serveInBackground, uninstall's startAfter, update's runAfterExit and OpenBeside (no Wait at all), and Windows' Reexec. - TestEveryStartedCommandIsWaited (internal/proc) parses every non-test .go file. In each function, an x.Start() on a command needs an x.Wait(), or x handed on (a call's argument, a struct field), and .Process.Release() is refused. It found exactly the sites above. - docs/subsystems/provider-plugins.md says how the host is reaped. Checked and left as they are (each waits): the StartTree callers (provider/install, library mcp_run and mcp_check, gateway claude_warmup and claude_subscription), pii/serve, provider grok and claude_signin, claudetui child_unix and wslread, awake, every other bun start (bun.go, bunupdate, store, dist use Output/CombinedOutput). Run at 82460a2f9 (on origin/main40a287b21): - mote (macOS): gofmt -l clean; go vet on the touched packages, GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok. go test -count=1 -timeout 45m ./internal/plugin ./internal/proc ./internal/codexrelay ./internal/tui ./internal/uninstall ./internal/update: all ok. -race -count=3 of the new proc tests: ok. - WSL magpie-desktop (Linux): the same six packages ok, including TestHostReapedWhileItsChildHoldsStdout with a real Bun host; -race -count=3 of it and the Output tests: ok. - Windows (xiaomi-gpu), at the tree before the rebase (base5ee5274bd): proc and codexrelay ok. plugin and update had TestPiPackage, TestLoadConcurrentWithSavesAndEdits and TestPackagedByDpkg red; each fails the same at untouched origin/main40a287b21on that box, 3 of 3, and is filed (#4115, #1827, #4603). Broken by hand, each red: - host.go back to StdoutPipe with Wait only after EOF: TestHostReapedWhileItsChildHoldsStdout fails on Linux ("the host isn't marked ended 15s after Restart"). - Output.Start waiting for the readers before Wait: TestOutputWaitsPastWhatHoldsIt fails (not waited for in 10s). - codexrelay back to Process.Release: TestEveryStartedCommandIsWaited names relay.go:89 and :92. Not tried: a full go test ./... (mote and WSL were each running other sessions' package runs, load 39 and 110); a real magpie serve or web running for hours on Linux; the host test on macOS (no /proc; the Output test runs there). Not changed: killing what a host left behind (its process group) when it is stopped; an orphan like the sleep above stays in the service's cgroup until it ends on its own. Reported by acezan on Discord.
118 lines
3.0 KiB
Go
118 lines
3.0 KiB
Go
package plugin
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A plugin host is reaped when it ends, while a program the plugin started
|
|
// still holds its stdout. The host was waited for only once its stdout
|
|
// reached EOF, which such a program put off for its whole life, so every
|
|
// host stopped (a Restart, a proxy change) left a <defunct> magpie-bun
|
|
// under magpie until magpie quit (acezan on Discord: 57 after 47 hours as
|
|
// a systemd service on Linux).
|
|
func TestHostReapedWhileItsChildHoldsStdout(t *testing.T) {
|
|
sandbox(t)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
// a sleep of a length no other test uses, to find and end it after
|
|
const hold = "61.25"
|
|
t.Cleanup(func() { killSleeps(hold) })
|
|
|
|
b, err := os.ReadFile("testdata/fake/index.js")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
js := filepath.Join(t.TempDir(), "index.js")
|
|
start := `Bun.spawn({ cmd: ["sleep", "` + hold + `"], stdout: Bun.stdout })` + "\n"
|
|
if err := os.WriteFile(js, append([]byte(start), b...), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Add(ctx, js); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := range 3 {
|
|
if _, err := Providers(ctx); err != nil {
|
|
t.Fatalf("round %d: %v", i, err)
|
|
}
|
|
hostMu.Lock()
|
|
h := current
|
|
hostMu.Unlock()
|
|
if h == nil {
|
|
t.Fatalf("round %d: no host", i)
|
|
}
|
|
Restart()
|
|
select {
|
|
case <-h.dead:
|
|
case <-time.After(15 * time.Second):
|
|
t.Fatalf("round %d: the host isn't marked ended 15s after Restart", i)
|
|
}
|
|
// the host is waited for as soon as it ends, not when the sleep
|
|
// lets go of its stdout
|
|
until := time.Now().Add(15 * time.Second)
|
|
for {
|
|
z := defunctChildren()
|
|
if len(z) == 0 {
|
|
break
|
|
}
|
|
if time.Now().After(until) {
|
|
t.Fatalf("round %d: %d defunct children 15s after Restart: %v", i, len(z), z)
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
}
|
|
}
|
|
|
|
// defunctChildren lists this process's children that have ended and not
|
|
// been waited for.
|
|
func defunctChildren() []string {
|
|
me := strconv.Itoa(os.Getpid())
|
|
ents, _ := os.ReadDir("/proc")
|
|
var z []string
|
|
for _, e := range ents {
|
|
if _, err := strconv.Atoi(e.Name()); err != nil {
|
|
continue
|
|
}
|
|
b, err := os.ReadFile("/proc/" + e.Name() + "/stat")
|
|
if err != nil {
|
|
continue
|
|
}
|
|
s := string(b)
|
|
i := strings.LastIndexByte(s, ')')
|
|
if i < 0 || i+2 > len(s) {
|
|
continue
|
|
}
|
|
f := strings.Fields(s[i+2:])
|
|
if len(f) > 1 && f[0] == "Z" && f[1] == me {
|
|
z = append(z, e.Name()+" "+s[strings.IndexByte(s, '(')+1:i])
|
|
}
|
|
}
|
|
return z
|
|
}
|
|
|
|
// killSleeps ends every `sleep secs` running.
|
|
func killSleeps(secs string) {
|
|
ents, _ := os.ReadDir("/proc")
|
|
for _, e := range ents {
|
|
pid, err := strconv.Atoi(e.Name())
|
|
if err != nil {
|
|
continue
|
|
}
|
|
b, err := os.ReadFile("/proc/" + e.Name() + "/cmdline")
|
|
if err != nil {
|
|
continue
|
|
}
|
|
args := bytes.Split(bytes.TrimRight(b, "\x00"), []byte{0})
|
|
if len(args) == 2 && filepath.Base(string(args[0])) == "sleep" && string(args[1]) == secs {
|
|
_ = syscall.Kill(pid, syscall.SIGKILL)
|
|
}
|
|
}
|
|
}
|