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