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.
139 lines
4.0 KiB
Go
139 lines
4.0 KiB
Go
package proc
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestEveryStartedCommandIsWaited: a command magpie starts is waited for,
|
|
// in the function that starts it: Wait is what reaps it on Unix, and
|
|
// one not waited for stays a <defunct> child of magpie until magpie quits
|
|
// (acezan on Discord: 57 magpie-bun after 47 hours). Process.Release
|
|
// doesn't reap on Unix, so it is no way out: a command magpie leaves
|
|
// running goes through StartReaped. The check is by name: in each
|
|
// function, every x.Start() needs an x.Wait() (or x handed to a waiter,
|
|
// as Output's Start is, or kept in a struct whose stop waits) beside it.
|
|
func TestEveryStartedCommandIsWaited(t *testing.T) {
|
|
root, _ := filepath.Abs(filepath.Join("..", ".."))
|
|
fset := token.NewFileSet()
|
|
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.IsDir() {
|
|
if path != root && (strings.HasPrefix(d.Name(), ".") || d.Name() == "node_modules" || d.Name() == "testdata") {
|
|
return filepath.SkipDir
|
|
}
|
|
return nil
|
|
}
|
|
if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
|
|
return nil
|
|
}
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !strings.Contains(string(b), ".Start()") && !strings.Contains(string(b), ".Release()") {
|
|
return nil
|
|
}
|
|
f, err := parser.ParseFile(fset, path, b, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rel, _ := filepath.Rel(root, path)
|
|
for _, decl := range f.Decls {
|
|
name := "?"
|
|
switch d := decl.(type) {
|
|
case *ast.FuncDecl:
|
|
name = d.Name.Name
|
|
case *ast.GenDecl:
|
|
// a var holding a func, as tests swap in a fake
|
|
if vs, ok := d.Specs[0].(*ast.ValueSpec); ok && len(vs.Names) > 0 {
|
|
name = vs.Names[0].Name
|
|
}
|
|
}
|
|
starts := map[string]token.Pos{}
|
|
waited := map[string]bool{}
|
|
ast.Inspect(decl, func(n ast.Node) bool {
|
|
// handed to what owns it from here: a struct's field (whose
|
|
// stop waits), a waiter's argument (Output's Start,
|
|
// StartReaped)
|
|
switch n := n.(type) {
|
|
case *ast.KeyValueExpr:
|
|
waited[exprName(n.Value)] = true
|
|
case *ast.AssignStmt:
|
|
for _, r := range n.Rhs {
|
|
waited[exprName(r)] = true
|
|
}
|
|
}
|
|
call, ok := n.(*ast.CallExpr)
|
|
if !ok {
|
|
return true
|
|
}
|
|
sel, ok := call.Fun.(*ast.SelectorExpr)
|
|
if !ok {
|
|
return true
|
|
}
|
|
recv := exprName(sel.X)
|
|
switch {
|
|
case sel.Sel.Name == "Start" && len(call.Args) == 0 && isCmd(recv):
|
|
if _, ok := starts[recv]; !ok {
|
|
starts[recv] = call.Pos()
|
|
}
|
|
case sel.Sel.Name == "Wait" && len(call.Args) == 0:
|
|
waited[recv] = true
|
|
case sel.Sel.Name == "Release" && len(call.Args) == 0 && strings.HasSuffix(recv, ".Process"):
|
|
t.Errorf("%s:%d: %s releases %s rather than waiting for it; use proc.StartReaped", rel, fset.Position(call.Pos()).Line, name, recv)
|
|
}
|
|
for _, a := range call.Args {
|
|
// handed to a waiter: Output's Start, StartReaped, a
|
|
// helper that owns the command from here
|
|
waited[exprName(a)] = true
|
|
}
|
|
return true
|
|
})
|
|
for recv, pos := range starts {
|
|
if !waited[recv] {
|
|
p := fset.Position(pos)
|
|
t.Errorf("%s:%d: %s starts %s and never waits for it", rel, p.Line, name, recv)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// exprName is an expression as written: a name, or a selector of names.
|
|
func exprName(e ast.Expr) string {
|
|
switch e := e.(type) {
|
|
case *ast.Ident:
|
|
return e.Name
|
|
case *ast.SelectorExpr:
|
|
return exprName(e.X) + "." + e.Sel.Name
|
|
case *ast.StarExpr:
|
|
return exprName(e.X)
|
|
case *ast.UnaryExpr:
|
|
return exprName(e.X)
|
|
case *ast.ParenExpr:
|
|
return exprName(e.X)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// isCmd says whether a receiver's name is a command's: Start is also a
|
|
// timer's, a server's and a span's, which this check isn't about.
|
|
func isCmd(recv string) bool {
|
|
last := recv[strings.LastIndex(recv, ".")+1:]
|
|
l := strings.ToLower(last)
|
|
return l == "cmd" || strings.HasSuffix(l, "cmd") || l == "c" || l == "proc" || l == "command"
|
|
}
|