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.
87 lines
2.2 KiB
Go
87 lines
2.2 KiB
Go
//go:build !windows
|
|
|
|
package proc
|
|
|
|
import (
|
|
"io"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A command is waited for when it ends, while what it started still holds
|
|
// its output: read to EOF first, its Wait waited for the program it left
|
|
// behind, and it stayed <defunct> all that time.
|
|
func TestOutputWaitsPastWhatHoldsIt(t *testing.T) {
|
|
old := OutputGrace
|
|
OutputGrace = 200 * time.Millisecond
|
|
t.Cleanup(func() { OutputGrace = old })
|
|
|
|
cmd := Command("sh", "-c", "sleep 30 & sleep 30 >&2 & echo hi")
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
o, err := NewOutput(cmd)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wait, err := o.Start(cmd)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) })
|
|
got := make(chan string, 1)
|
|
go func() {
|
|
b, _ := io.ReadAll(o.Out)
|
|
o.OutRead()
|
|
got <- string(b)
|
|
}()
|
|
go func() {
|
|
_, _ = io.Copy(io.Discard, o.Err)
|
|
o.ErrRead()
|
|
}()
|
|
waited := make(chan error, 1)
|
|
go func() { waited <- wait() }()
|
|
select {
|
|
case err := <-waited:
|
|
if err != nil {
|
|
t.Fatalf("wait: %v", err)
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("the command wasn't waited for in 10s while what it started held its output")
|
|
}
|
|
// waited, so reaped: the process is gone, not a zombie
|
|
if err := syscall.Kill(cmd.Process.Pid, 0); err != syscall.ESRCH {
|
|
t.Fatalf("the shell is still there after wait: %v", err)
|
|
}
|
|
select {
|
|
case s := <-got:
|
|
if s != "hi\n" {
|
|
t.Fatalf("read %q, want what it wrote", s)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("the read didn't end once its pipe was closed")
|
|
}
|
|
}
|
|
|
|
// What a command writes as it ends is read whole before its pipe is closed.
|
|
func TestOutputKeepsTheLastLines(t *testing.T) {
|
|
cmd := Command("sh", "-c", "i=0; while [ $i -lt 2000 ]; do echo line $i; i=$((i+1)); done")
|
|
o, err := NewOutput(cmd)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wait, err := o.Start(cmd)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
go func() { _, _ = io.Copy(io.Discard, o.Err); o.ErrRead() }()
|
|
time.Sleep(200 * time.Millisecond) // it has ended, its output still unread
|
|
b, _ := io.ReadAll(o.Out)
|
|
o.OutRead()
|
|
if err := wait(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n := len(b); n < 2000*len("line 0\n") {
|
|
t.Fatalf("read %d bytes of what it wrote", n)
|
|
}
|
|
}
|