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.
303 lines
8.0 KiB
Go
303 lines
8.0 KiB
Go
// Package codexrelay keeps the Codex threads open when magpie quits
|
|
// working until magpie is opened again or they end.
|
|
//
|
|
// Codex signed in to ChatGPT sends its turns to magpie's gateway while
|
|
// magpie fails it over (agent.EndCodexFailover). At quit magpie takes that
|
|
// base URL out of Codex's config, but a thread already open, in the Codex
|
|
// app above all, keeps the URL it started with, and every turn after went
|
|
// to a closed port: "503 Service Unavailable: Unknown error" through a
|
|
// system proxy, "Connection refused" without one, until magpie was opened
|
|
// again (idkatbro on X). So at quit, with Codex running, magpie starts a
|
|
// small process of its own (`magpie codex-relay`) that takes the port up
|
|
// once the gateway lets go of it and passes those turns on to the ChatGPT
|
|
// backend on Codex's own sign-in (gateway.CodexRelay). It ends once every
|
|
// Codex process it was started for has, and leaves when the next magpie
|
|
// starts (Leave), finishing the turns it is streaming first: no running
|
|
// turn is cut.
|
|
package codexrelay
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"crypto/subtle"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"sync/atomic"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// Command is the magpie command a relay runs as.
|
|
const Command = "codex-relay"
|
|
|
|
// LeavePath is where the next magpie tells the relay to leave; with
|
|
// TokenHeader holding the token the relay wrote in its state file.
|
|
const (
|
|
LeavePath = "/.magpie-codex-relay/leave"
|
|
TokenHeader = "X-Magpie-Relay-Token"
|
|
)
|
|
|
|
// Timings, vars for tests.
|
|
var (
|
|
// bindFor is how long the relay waits for the gateway's port: the
|
|
// quitting magpie holds it until its own requests have finished.
|
|
bindFor = 60 * time.Second
|
|
// watchEvery is how often the relay looks for its Codex processes.
|
|
watchEvery = 15 * time.Second
|
|
// drainFor caps how long a leaving relay finishes its streams.
|
|
drainFor = 30 * time.Minute
|
|
// leaveWait is how long Leave waits for the relay to answer.
|
|
leaveWait = 2 * time.Second
|
|
)
|
|
|
|
// state is what a running relay says of itself, for Leave.
|
|
type state struct {
|
|
Pid int `json:"pid"`
|
|
Addr string `json:"addr"`
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
func statePath() string { return filepath.Join(settings.Dir(), "codex-relay.json") }
|
|
|
|
// Start runs a relay at addr for the Codex processes pids, apart from
|
|
// this process so it outlives it.
|
|
func Start(addr string, pids []int) error {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ids := make([]string, len(pids))
|
|
for i, p := range pids {
|
|
ids[i] = strconv.Itoa(p)
|
|
}
|
|
cmd := proc.Command(exe, Command, "--addr", addr, "--pids", strings.Join(ids, ","))
|
|
detach(cmd)
|
|
// reaped when it ends while magpie runs: a released one stayed
|
|
// <defunct> until magpie quit
|
|
return proc.StartReaped(cmd)
|
|
}
|
|
|
|
// Main is `magpie codex-relay --addr host:port --pids 1,2`, serving h.
|
|
func Main(args []string, h http.Handler) error {
|
|
var addr string
|
|
var pids []int
|
|
for i := 0; i < len(args); i++ {
|
|
switch args[i] {
|
|
case "--addr":
|
|
if i+1 < len(args) {
|
|
addr = args[i+1]
|
|
i++
|
|
}
|
|
case "--pids":
|
|
if i+1 < len(args) {
|
|
for _, s := range strings.Split(args[i+1], ",") {
|
|
if s == "" {
|
|
continue
|
|
}
|
|
n, err := strconv.Atoi(s)
|
|
if err != nil || n <= 0 {
|
|
return fmt.Errorf("codex-relay: bad pid %q", s)
|
|
}
|
|
pids = append(pids, n)
|
|
}
|
|
i++
|
|
}
|
|
default:
|
|
return fmt.Errorf("codex-relay: unknown argument %q (magpie starts this itself when it quits)", args[i])
|
|
}
|
|
}
|
|
if addr == "" || len(pids) == 0 {
|
|
return errors.New("usage: magpie codex-relay --addr host:port --pids pid,… (magpie starts this itself when it quits)")
|
|
}
|
|
sig := make(chan os.Signal, 1)
|
|
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
|
|
ctx, stop := context.WithCancel(context.Background())
|
|
go func() { <-sig; stop() }()
|
|
return Run(ctx, addr, pids, h)
|
|
}
|
|
|
|
// Run serves h at addr until ctx ends, the next magpie tells it to leave,
|
|
// or none of pids runs and nothing is in flight; then it finishes the
|
|
// requests it has, for up to drainFor.
|
|
func Run(ctx context.Context, addr string, pids []int, h http.Handler) error {
|
|
ln, err := bind(ctx, addr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
b := make([]byte, 16)
|
|
if _, err := rand.Read(b); err != nil {
|
|
ln.Close()
|
|
return err
|
|
}
|
|
me := state{Pid: os.Getpid(), Addr: ln.Addr().String(), Token: hex.EncodeToString(b)}
|
|
if err := writeState(me); err != nil {
|
|
ln.Close()
|
|
return err
|
|
}
|
|
defer dropState(me.Token)
|
|
|
|
var inFlight atomic.Int64
|
|
left := make(chan struct{})
|
|
var leaving atomic.Bool
|
|
srv := &http.Server{ReadHeaderTimeout: 30 * time.Second}
|
|
srv.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == LeavePath {
|
|
tok := r.Header.Get(TokenHeader)
|
|
if r.Method != http.MethodPost || r.Header.Get("Origin") != "" || subtle.ConstantTimeCompare([]byte(tok), []byte(me.Token)) != 1 {
|
|
http.Error(w, "forbidden", http.StatusForbidden)
|
|
return
|
|
}
|
|
// the port is free once this is answered: the next magpie
|
|
// binds it at once, while the streams here finish
|
|
srv.SetKeepAlivesEnabled(false)
|
|
ln.Close()
|
|
if leaving.CompareAndSwap(false, true) {
|
|
close(left)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
return
|
|
}
|
|
inFlight.Add(1)
|
|
defer inFlight.Add(-1)
|
|
h.ServeHTTP(w, r)
|
|
})
|
|
go srv.Serve(ln)
|
|
|
|
t := time.NewTicker(watchEvery)
|
|
defer t.Stop()
|
|
wait:
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
break wait
|
|
case <-left:
|
|
break wait
|
|
case <-t.C:
|
|
if inFlight.Load() == 0 && !anyAlive(pids) {
|
|
break wait
|
|
}
|
|
}
|
|
}
|
|
drain, cancel := context.WithTimeout(context.Background(), drainFor)
|
|
defer cancel()
|
|
srv.Shutdown(drain)
|
|
return nil
|
|
}
|
|
|
|
// bind listens at addr, waiting up to bindFor for it to be let go of.
|
|
func bind(ctx context.Context, addr string) (net.Listener, error) {
|
|
end := time.Now().Add(bindFor)
|
|
for {
|
|
ln, err := net.Listen("tcp", addr)
|
|
if err == nil {
|
|
return ln, nil
|
|
}
|
|
if time.Now().After(end) {
|
|
return nil, err
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
case <-time.After(200 * time.Millisecond):
|
|
}
|
|
}
|
|
}
|
|
|
|
func anyAlive(pids []int) bool {
|
|
for _, p := range pids {
|
|
if alive(p) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// writeState writes the relay's state where only this user reads it,
|
|
// whole or not at all.
|
|
func writeState(st state) error {
|
|
b, err := json.Marshal(st)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p := statePath()
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o700); err != nil {
|
|
return err
|
|
}
|
|
tmp, err := os.CreateTemp(filepath.Dir(p), ".codex-relay-*")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(tmp.Name())
|
|
if err := tmp.Chmod(0o600); err != nil && !errors.Is(err, errors.ErrUnsupported) {
|
|
tmp.Close()
|
|
return err
|
|
}
|
|
if _, err := tmp.Write(b); err != nil {
|
|
tmp.Close()
|
|
return err
|
|
}
|
|
if err := tmp.Sync(); err != nil {
|
|
tmp.Close()
|
|
return err
|
|
}
|
|
if err := tmp.Close(); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(tmp.Name(), p)
|
|
}
|
|
|
|
func readState() (state, bool) {
|
|
b, err := os.ReadFile(statePath())
|
|
if err != nil {
|
|
return state{}, false
|
|
}
|
|
var st state
|
|
if json.Unmarshal(b, &st) != nil || st.Pid <= 0 || st.Addr == "" || st.Token == "" {
|
|
return state{}, false
|
|
}
|
|
return st, true
|
|
}
|
|
|
|
// dropState removes the state file when it is still this relay's.
|
|
func dropState(token string) {
|
|
if st, ok := readState(); ok && st.Token == token {
|
|
os.Remove(statePath())
|
|
}
|
|
}
|
|
|
|
// Leave tells a relay serving at the gateway's port to let go of it,
|
|
// for the magpie starting to serve there. It returns once the relay has
|
|
// closed its listener (its streams go on finishing), or at once when
|
|
// there is none.
|
|
func Leave() {
|
|
st, ok := readState()
|
|
if !ok {
|
|
return
|
|
}
|
|
if !alive(st.Pid) {
|
|
os.Remove(statePath())
|
|
return
|
|
}
|
|
req, err := http.NewRequest(http.MethodPost, "http://"+st.Addr+LeavePath, nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
req.Header.Set(TokenHeader, st.Token)
|
|
c := &http.Client{Timeout: leaveWait, Transport: &http.Transport{Proxy: nil, DisableKeepAlives: true}}
|
|
if res, err := c.Do(req); err == nil {
|
|
res.Body.Close()
|
|
}
|
|
}
|