Files
magpie/internal/gui/gateway_takeover.go
T
yetone 9d0c9b41ea serve: follow an update put in where it runs from; a replaced magpie.real is magpie
acezan on Discord (Linux 5.15, 0.1.2959 to 0.1.2960): `magpie web` as a
system unit updated /root/.local/bin/magpie.real, and the user unit's
`magpie serve` (`magpie daemon install`) ran on for good from "magpie.real
(deleted)". The panel said an older magpie served the gateway, and its
"close it here" then said the port holder "isn't magpie" and left it.

Why serve stayed old: the app and `magpie web` follow the program they
started from (gui's followReplaced) and restart into an update; plain
`magpie serve` had no such look, and answered `magpie update`'s restart
ask (gateway.RestartPath) "unsupported". Nothing else can restart it
reliably: a system unit doesn't reach the user's `systemctl --user`, and
on a Mac the launch agent's serve (#4335) is the same serve. So serve now
looks itself (internal/cli/serve_follow.go): every 5s it checks its
program with update.Replaced; once replaced, it waits until the gateway
is idle (nothing in flight, 10s since the last end, as gui's idle rule;
after an hour, nothing in flight is enough), ends the plugin hosts and
runs the new program in its own place (update.Reexec: exec in place on
Unix, same PID, so systemd and launchd keep tracking it; a new process
on Windows). The restart ask now answers restarting/waiting/current. A
build that isn't a release never does it.

Why "not magpie": proc.IsMagpie judged the file name, and "magpie.real"
(the program the installer's wrapper script runs) didn't match. It now
trims ".real". And proc.RunsMagpie, used by the takeover and the port
check, also reads Go's build info of the running program (on Linux
through /proc/<pid>/exe, which still reads after the file is replaced)
and calls it magpie when its main package is github.com/yetone/magpie.
A process that isn't magpie is still never ended (the existing
"other-server" case passes unchanged).

Tests, each red on the old code:
- cli TestServeRestartsIntoAnUpdate: a copy of the test binary named
  magpie.real runs `serve` on a free port; a script renamed over it is
  run in its place with the same pid and args, and the old port stops
  answering. Old code: "serve ran on as the old version" after 40s.
- cli TestServeFollowWaitsForIdle: current / waiting / tool-chain gap /
  waitMost with a request in flight / idle / non-release.
- gui TestGatewayFixQuitsAReplacedMagpieReal: a fake older gateway named
  magpie.real, its file removed after start, is quit and the gateway
  served here. Old code: {OK:false Reason:not-magpie}, acezan's case.
- proc TestIsMagpie magpie.real case; TestRunsMagpieByItsBuild: a copy
  named server.real, its file removed, is told by its build (Linux
  /proc/<pid>/exe), and is not magpie with the real main path.

Run: the targeted tests above on Linux (wsl-run, magpie-desktop, -tags
nogui) and macOS (mote-run), all pass. Broken by hand on Linux: dropped
the ".real" trim, made RunsMagpie only IsMagpie, and set serve's follow
to nil; all four new tests went red (output above), restored after.
gofmt -l clean; go vet (darwin, GOOS=linux -tags nogui, GOOS=windows)
and builds on mote.

Not tried: a real systemd user unit or launch agent with a real update;
the Windows restart path (Reexec starts a new process) beyond vet; the
macOS TCC part of #4335 (this restarts the launch agent's serve into the
new bundle path only if that path is where it started from). #4335 stays
open, and ggbdpq's daemon.RestartReplaced patch there is not applied.
2026-11-04 23:55:14 +08:00

213 lines
6.4 KiB
Go

package gui
import (
"context"
"errors"
"net/http"
"os"
"slices"
"strconv"
"time"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/proc"
)
// The Gateway page's button (leslie_luo on Discord): with an older magpie
// keeping the gateway's port, it quits that one and this one serves the
// gateway at once; otherwise it restarts the gateway this one serves, or
// starts it when none does. The page said only "quit that magpie", and the
// user couldn't find which one it was. Only a process whose program is a
// magpie (proc.RunsMagpie, by the pid listening on the port and its program) is
// ever ended; anything else is said, never touched.
// gatewayFixJSON is how a take-over or a restart went: what stood in the
// way by a reason the page says in its language, and the process it was.
type gatewayFixJSON struct {
OK bool `json:"ok"`
// Reason: "not-magpie" (the port is another program's), "container"
// (a container's or a VM's: the magpie answering runs in it, which
// its own tools stop — docker stop, OrbStack's window), "unseen" (no
// process of this user's listens: another user's, an administrator's),
// "denied" (a magpie this user may not end), "stuck" (it didn't end),
// "respawned" (something started a magpie on the port again at once:
// a LaunchAgent, a systemd unit, a scheduled task), "taken" (the
// gateway couldn't listen: another program has the port), "other"
// (a magpie not older than this one serves; quit it there), "error"
Reason string `json:"reason,omitempty"`
PID int `json:"pid,omitempty"`
Path string `json:"path,omitempty"`
Port string `json:"port,omitempty"`
Version string `json:"version,omitempty"`
Error string `json:"error,omitempty"` // the system's own words
}
func gatewayFixRoutes(mux *http.ServeMux) {
mux.HandleFunc("POST /api/gateway/take-over", func(rw http.ResponseWriter, r *http.Request) {
writeJSON(rw, takeOverGateway())
})
mux.HandleFunc("POST /api/gateway/restart", func(rw http.ResponseWriter, r *http.Request) {
writeJSON(rw, restartGateway())
})
}
// How long a magpie asked to quit is given before it is ended at once,
// that again, and the gateway here to answer; vars for the tests.
var (
quitWait = 5 * time.Second
killWait = 3 * time.Second
startWait = 5 * time.Second
)
// takeOverGateway quits the magpie serving the gateway, when there is one,
// and serves it here.
func takeOverGateway() gatewayFixJSON {
gatewayMu.Lock()
defer gatewayMu.Unlock()
if served.Load() != nil {
return gatewayFixJSON{OK: true}
}
if o := gateway.ServedBy(); o.Running {
if out := quitMagpieOnPort(o.Version); !out.OK {
return out
}
}
return startHere()
}
// restartGateway stops the gateway this process serves and starts it
// again; with none served here and none answering, it starts it. A magpie
// serving it elsewhere is left alone: that is the take-over's to quit.
func restartGateway() gatewayFixJSON {
gatewayMu.Lock()
defer gatewayMu.Unlock()
if gw := served.Load(); gw != nil {
if servedRun.stop != nil {
servedRun.stop()
<-servedRun.done
}
served.CompareAndSwap(gw, nil)
} else if o := gateway.ServedBy(); o.Running {
return gatewayFixJSON{Reason: "other", Version: o.Version, Port: gateway.Port()}
}
return startHere()
}
// startHere serves the gateway here and waits until it answers, or says
// what has the port.
func startHere() gatewayFixJSON {
gw := serveGatewayLocked()
for deadline := time.Now().Add(startWait); gw != nil && served.Load() == gw; time.Sleep(50 * time.Millisecond) {
if gateway.Running() {
return gatewayFixJSON{OK: true}
}
if time.Now().After(deadline) {
break
}
}
// it didn't take the port: who has it
out := gatewayFixJSON{Reason: "taken", Port: gateway.Port()}
if o := gateway.ServedBy(); o.Running {
out.Version = o.Version
}
port, _ := strconv.Atoi(out.Port)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if pids, err := proc.ListeningOn(ctx, port); err == nil {
for _, pid := range pids {
if pid == os.Getpid() {
continue
}
out.PID = pid
out.Path, _ = proc.Executable(pid)
if proc.RunsMagpie(pid, out.Path) {
// a magpie took it back as soon as the other quit
out.Reason = "respawned"
} else if proc.IsForwarder(out.Path) {
out.Reason = "container"
}
break
}
}
return out
}
// quitMagpieOnPort ends the magpie (of version, as it says) listening on
// the gateway's port, once its program is checked to be magpie's, and
// waits until the port is let go.
func quitMagpieOnPort(version string) gatewayFixJSON {
port := gateway.Port()
n, _ := strconv.Atoi(port)
out := gatewayFixJSON{Port: port, Version: version}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
pids, err := proc.ListeningOn(ctx, n)
if err != nil {
out.Reason, out.Error = "error", err.Error()
return out
}
pids = slices.DeleteFunc(pids, func(p int) bool { return p == os.Getpid() })
if len(pids) == 0 {
out.Reason = "unseen"
return out
}
for _, pid := range pids {
path, err := proc.Executable(pid)
out.PID, out.Path = pid, path
if err != nil {
// a process this user can't look into isn't one it can end
out.Reason, out.Error = "unseen", err.Error()
return out
}
if !proc.RunsMagpie(pid, path) {
out.Reason = "not-magpie"
if proc.IsForwarder(path) {
out.Reason = "container"
}
return out
}
}
end := func(f func(int) error) *gatewayFixJSON {
for _, pid := range pids {
if err := f(pid); err != nil {
out.PID = pid
out.Path, _ = proc.Executable(pid)
if errors.Is(err, proc.ErrDenied) {
out.Reason = "denied"
} else {
out.Reason, out.Error = "error", err.Error()
}
return &out
}
}
return nil
}
if bad := end(proc.Terminate); bad != nil {
return *bad
}
if letGo(ctx, n, pids, quitWait) {
return gatewayFixJSON{OK: true}
}
if bad := end(proc.Kill); bad != nil {
return *bad
}
if letGo(ctx, n, pids, killWait) {
return gatewayFixJSON{OK: true}
}
out.Reason = "stuck"
return out
}
// letGo waits until none of pids listens on port.
func letGo(ctx context.Context, port int, pids []int, wait time.Duration) bool {
for deadline := time.Now().Add(wait); ; time.Sleep(100 * time.Millisecond) {
now, err := proc.ListeningOn(ctx, port)
if err == nil && !slices.ContainsFunc(now, func(p int) bool { return slices.Contains(pids, p) }) {
return true
}
if time.Now().After(deadline) {
return false
}
}
}