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.
152 lines
4.7 KiB
Go
152 lines
4.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/update"
|
|
)
|
|
|
|
// `magpie serve` follows an update put in where it runs from. An update
|
|
// is put in by another magpie: `magpie web` or the app updating itself
|
|
// from the same program, or `magpie update` in a terminal. None of them
|
|
// restarted the gateway `magpie serve` keeps, as the service `magpie
|
|
// daemon install` writes, a systemd user unit or a launch agent (acezan on
|
|
// Discord: `magpie web` as a system unit updated itself, and the user
|
|
// unit's serve ran on for good from "magpie.real (deleted)", the page
|
|
// saying an older magpie served the gateway; #4335 on a Mac, where the
|
|
// old bundle is deleted and TCC turns the process away from Desktop,
|
|
// Documents and Downloads). The updater can't reliably restart it: a
|
|
// system unit doesn't reach the user's systemctl, and the app may not be
|
|
// running at all. So serve looks itself, as the app and magpie web do
|
|
// (gui's followReplaced): once the program it started from is another
|
|
// file, it waits until the gateway is idle, then runs the new program in
|
|
// its own place, the same process under the same service manager or in
|
|
// the same terminal. A build of one's own (not a release) never does,
|
|
// since a rebuild in place would restart it.
|
|
|
|
// serveFollowTick is how often serve looks at its program on disk;
|
|
// serveIdleSettle is how long nothing has ended before it counts as idle
|
|
// (the gap of a tool chain, as gui's idleSettle); serveWaitMost is how
|
|
// long it waits for that before it restarts once nothing is in flight,
|
|
// however recent. Vars for the tests.
|
|
var (
|
|
serveFollowTick = 5 * time.Second
|
|
serveIdleSettle = 10 * time.Second
|
|
serveWaitMost = time.Hour
|
|
)
|
|
|
|
// serveReexec runs the new program in serve's place; a var for the tests.
|
|
var serveReexec = update.Reexec
|
|
|
|
// serveFollow watches exe, the program serve started from (started is its
|
|
// os.Stat then), and calls stop once it has been replaced and busy says
|
|
// the gateway is idle. Restart reports the program to run in this one's
|
|
// place once stop has been called for it, "" otherwise.
|
|
type serveFollow struct {
|
|
exe string
|
|
started os.FileInfo
|
|
busy func() gateway.Busy
|
|
released bool
|
|
stop func()
|
|
|
|
mu sync.Mutex
|
|
due time.Time // when the replaced program was first seen
|
|
stopped bool
|
|
}
|
|
|
|
// newServeFollow follows the program this process runs, for s; nil when
|
|
// it can't be told (no path, or no file there).
|
|
func newServeFollow(s *gateway.Server, stop func()) *serveFollow {
|
|
exe, err := update.Executable()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
st, err := os.Stat(exe)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return &serveFollow{exe: exe, started: st, busy: s.Busy, released: update.Released(version), stop: stop}
|
|
}
|
|
|
|
// run looks every serveFollowTick until ctx ends or serve is stopped.
|
|
func (f *serveFollow) run(ctx context.Context) {
|
|
if f == nil || !f.released {
|
|
return
|
|
}
|
|
t := time.NewTicker(serveFollowTick)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-t.C:
|
|
}
|
|
if f.check(time.Now()) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// check stops serve when the program was replaced and the gateway is idle
|
|
// at now; it reports whether it did.
|
|
func (f *serveFollow) check(now time.Time) bool {
|
|
if !update.Replaced(f.exe, f.started) {
|
|
return false
|
|
}
|
|
f.mu.Lock()
|
|
if f.due.IsZero() {
|
|
f.due = now
|
|
log.Printf("magpie serve: %s was updated; restarting into it once the gateway is idle", f.exe)
|
|
}
|
|
waited := now.Sub(f.due)
|
|
f.mu.Unlock()
|
|
b := f.busy()
|
|
if b.Any() || now.Sub(b.Last) < serveIdleSettle && waited < serveWaitMost {
|
|
return false
|
|
}
|
|
f.mu.Lock()
|
|
f.stopped = true
|
|
f.mu.Unlock()
|
|
f.stop()
|
|
return true
|
|
}
|
|
|
|
// restartTo is the program to run in serve's place, "" when serve wasn't
|
|
// stopped for an update.
|
|
func (f *serveFollow) restartTo() string {
|
|
if f == nil {
|
|
return ""
|
|
}
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if !f.stopped {
|
|
return ""
|
|
}
|
|
return f.exe
|
|
}
|
|
|
|
// answer is what `magpie update`'s ask (gateway.RestartPath) is told: serve
|
|
// restarts into what it put in, at once when the gateway is idle (after
|
|
// the answer is out), else once it is, as on its own next look; "current"
|
|
// when nothing was put in where serve runs from, "unsupported" for a build
|
|
// of one's own, restarted the way it was started.
|
|
func (f *serveFollow) answer(string) gateway.RestartReply {
|
|
switch {
|
|
case f == nil || !f.released:
|
|
return gateway.RestartReply{State: "unsupported"}
|
|
case !update.Replaced(f.exe, f.started):
|
|
return gateway.RestartReply{State: "current"}
|
|
}
|
|
b := f.busy()
|
|
if !b.Any() && time.Since(b.Last) >= serveIdleSettle {
|
|
go f.check(time.Now())
|
|
return gateway.RestartReply{State: "restarting"}
|
|
}
|
|
return gateway.RestartReply{State: "waiting", Busy: &b}
|
|
}
|