From 142994b21c31d1b064eca9c27b80d87a03da4e9b Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 15 Nov 2026 04:52:51 +0800 Subject: [PATCH] web: `magpie web` reaps the processes it adopts, as `magpie serve` does (swang20201 on X) The Docker docs run `magpie web --addr 0.0.0.0:3430 --no-open`, so in a container without an init `magpie web` is PID 1 and adopts every orphan. 382c8c0bd made `serve` reap them (proc.KeepReaping) and left `web` out. A plugin's backend whose host had ended (magpie-gemini-web's aistudio2api) then stayed under magpie for good, and the plugin, asking kill(pid, 0), took it for still running and refused every restart. Run: - TestLongRunningCommandsReap (internal/cli): serveCmd and webCmd each call proc.KeepReaping. Red with origin/main's web_cli.go, green here. - Real use on Linux (WSL2 Ubuntu 24.04, `unshare -Urpf --mount-proc` so magpie is PID 1, sandbox HOME), an orphan left in the namespace that exits after 1s: on origin/main `web` still had 1 zombie after 12s, `serve` 0; with this change both have 0. - go test ./internal/cli -run TestLongRunningCommandsReap on mica. - Full suite (-timeout 45m) and GOOS=linux/windows go vet on mica: started at this tree before the push; result follows. Not tried: the reporter's container itself. The plugin's own check is fixed in BUKOWSKIREAL/magpie-gemini-web#59; the plugins repo's 18 siblings are magpie-community/plugins#1164. --- internal/cli/reap_test.go | 43 +++++++++++++++++++++++++++++++++++++++ internal/cli/web_cli.go | 5 +++++ 2 files changed, 48 insertions(+) create mode 100644 internal/cli/reap_test.go diff --git a/internal/cli/reap_test.go b/internal/cli/reap_test.go new file mode 100644 index 000000000..0a8e9ee42 --- /dev/null +++ b/internal/cli/reap_test.go @@ -0,0 +1,43 @@ +package cli + +import ( + "go/ast" + "go/parser" + "go/token" + "testing" +) + +// TestLongRunningCommandsReap: `magpie serve` and `magpie web` are what a +// container runs as its PID 1 (the Dockerfile's CMD and the Docker docs' +// `web --addr 0.0.0.0:3430 --no-open`), and PID 1 is left every orphan to +// reap. `web` didn't call proc.KeepReaping, so an aistudio2api whose +// plugin host had ended stayed under it, and the plugin waited +// on it for good (swang20201 on X). +func TestLongRunningCommandsReap(t *testing.T) { + f, err := parser.ParseFile(token.NewFileSet(), "web_cli.go", nil, 0) + if err != nil { + t.Fatal(err) + } + reaps := map[string]bool{} + for _, d := range f.Decls { + fn, ok := d.(*ast.FuncDecl) + if !ok || fn.Body == nil { + continue + } + ast.Inspect(fn.Body, func(n ast.Node) bool { + if c, ok := n.(*ast.CallExpr); ok { + if s, ok := c.Fun.(*ast.SelectorExpr); ok && s.Sel.Name == "KeepReaping" { + if x, ok := s.X.(*ast.Ident); ok && x.Name == "proc" { + reaps[fn.Name.Name] = true + } + } + } + return true + }) + } + for _, cmd := range []string{"serveCmd", "webCmd"} { + if !reaps[cmd] { + t.Errorf("%s doesn't call proc.KeepReaping: as a container's PID 1 it leaves its adopted orphans ", cmd) + } + } +} diff --git a/internal/cli/web_cli.go b/internal/cli/web_cli.go index 24281079b..17659acd7 100644 --- a/internal/cli/web_cli.go +++ b/internal/cli/web_cli.go @@ -78,6 +78,11 @@ func webCmd(args []string) error { // version manager put on PATH only in the shell's profile, as the app's // does: a service or autostart has none of it (#3803) proc.UserPath() + // a container's `magpie web` (the Docker docs' command) is its PID 1 + // as `magpie serve` is, and is left the processes its children leave + // behind: a plugin's backend whose host ended stayed , and + // the plugin took it for still running (swang20201 on X) + proc.KeepReaping(context.Background()) w, err := gui.StartWeb(addr, version) if err != nil { return err