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 <defunct> 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.
This commit is contained in:
yetone
2026-11-15 04:52:55 +08:00
parent b70c450489
commit 142994b21c
2 changed files with 48 additions and 0 deletions
+43
View File
@@ -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 <defunct> 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 <defunct>", cmd)
}
}
}
+5
View File
@@ -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 <defunct>, 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