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.
118 lines
3.4 KiB
Go
118 lines
3.4 KiB
Go
package proc
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// Output is a command's stdout and stderr, as pipes read apart from its
|
|
// Wait. cmd.StdoutPipe's end is closed by Wait, so a command whose output
|
|
// is read through it can only be waited for once that output has been
|
|
// read to the end, and the end is not the command's own: whatever it
|
|
// started kept a copy of its stdout (a program a plugin started, a daemon
|
|
// it left behind), so the reader never reached EOF after the command
|
|
// ended, the command was never waited for, and it stayed a <defunct>
|
|
// child of magpie until magpie quit (acezan on Discord: 57 magpie-bun
|
|
// after 47 hours). Output's command is waited for the moment it ends;
|
|
// its readers then have OutputGrace to read what it wrote last, and the
|
|
// pipes are closed, which ends their reads.
|
|
type Output struct {
|
|
// Out and Err are the ends magpie reads.
|
|
Out, Err *os.File
|
|
ws []*os.File
|
|
outDone chan struct{}
|
|
errDone chan struct{}
|
|
outOnce sync.Once
|
|
errOnce sync.Once
|
|
}
|
|
|
|
// OutputGrace is how long the readers of a command that has ended have to
|
|
// read what it wrote last, before its Output is closed under them.
|
|
var OutputGrace = 2 * time.Second
|
|
|
|
// NewOutput gives cmd's stdout and stderr each a pipe of an Output.
|
|
func NewOutput(cmd *exec.Cmd) (*Output, error) {
|
|
or, ow, err := os.Pipe()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
er, ew, err := os.Pipe()
|
|
if err != nil {
|
|
or.Close()
|
|
ow.Close()
|
|
return nil, err
|
|
}
|
|
cmd.Stdout, cmd.Stderr = ow, ew
|
|
return &Output{Out: or, Err: er, ws: []*os.File{ow, ew},
|
|
outDone: make(chan struct{}), errDone: make(chan struct{})}, nil
|
|
}
|
|
|
|
// OutRead says Out's reader has read to its end.
|
|
func (o *Output) OutRead() { o.outOnce.Do(func() { close(o.outDone) }) }
|
|
|
|
// ErrRead says Err's reader has read to its end.
|
|
func (o *Output) ErrRead() { o.errOnce.Do(func() { close(o.errDone) }) }
|
|
|
|
// Close closes the ends magpie reads, for an Output whose command
|
|
// isn't started.
|
|
func (o *Output) Close() {
|
|
for _, f := range append(o.ws, o.Out, o.Err) {
|
|
f.Close()
|
|
}
|
|
}
|
|
|
|
// Start starts cmd, whose output o is, and waits for it the moment it
|
|
// ends. wait gives what Wait said once the command has ended and its
|
|
// output has been read or closed; any number of goroutines can call it,
|
|
// any number of times. When cmd doesn't start, o is closed.
|
|
func (o *Output) Start(cmd *exec.Cmd) (wait func() error, err error) {
|
|
err = cmd.Start()
|
|
for _, w := range o.ws {
|
|
w.Close() // the command has its own copies
|
|
}
|
|
if err != nil {
|
|
o.Out.Close()
|
|
o.Err.Close()
|
|
return nil, err
|
|
}
|
|
ended := make(chan struct{})
|
|
var werr error
|
|
go func() {
|
|
werr = cmd.Wait()
|
|
grace := time.NewTimer(OutputGrace)
|
|
defer grace.Stop()
|
|
for _, s := range []struct {
|
|
done chan struct{}
|
|
f *os.File
|
|
}{{o.outDone, o.Out}, {o.errDone, o.Err}} {
|
|
select {
|
|
case <-s.done:
|
|
case <-grace.C:
|
|
// something the command started still holds this
|
|
// pipe: its reader is stopped rather than left waiting
|
|
grace.Reset(0)
|
|
}
|
|
s.f.Close()
|
|
}
|
|
close(ended)
|
|
}()
|
|
return func() error {
|
|
<-ended
|
|
return werr
|
|
}, nil
|
|
}
|
|
|
|
// StartReaped starts cmd, which magpie leaves running (it outlives magpie
|
|
// or ends on its own), and waits for it in the background, so it is
|
|
// reaped whenever it ends. Process.Release doesn't reap on Unix: a child
|
|
// released and ended stays <defunct> until magpie quits.
|
|
func StartReaped(cmd *exec.Cmd) error {
|
|
if err := cmd.Start(); err != nil {
|
|
return err
|
|
}
|
|
go cmd.Wait()
|
|
return nil
|
|
}
|