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.
104 lines
3.0 KiB
Go
104 lines
3.0 KiB
Go
//go:build !windows
|
|
|
|
package cli
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// `magpie serve` runs on as the new version once an update is put in where
|
|
// it runs from (acezan on Discord: the service's serve ran on from
|
|
// "magpie.real (deleted)" after `magpie web` updated the program they
|
|
// share; #4335 on a Mac). Here the test binary, copied to magpie.real, is
|
|
// a released serve on a port of the test's own; the update renames a new
|
|
// program over it, a script that notes its pid and arguments. serve runs
|
|
// it in its own place: the same pid, with serve's arguments.
|
|
func TestServeRestartsIntoAnUpdate(t *testing.T) {
|
|
self, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := os.ReadFile(self)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dir := t.TempDir()
|
|
bin := filepath.Join(dir, "magpie.real")
|
|
if err := os.WriteFile(bin, b, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
addr := ln.Addr().String()
|
|
ln.Close()
|
|
|
|
cmd := exec.Command(bin, "serve")
|
|
cmd.Env = append(os.Environ(), "MAGPIE_CLI_TEST_SERVE=1", "MAGPIE_ADDR="+addr)
|
|
if err := cmd.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ended := make(chan struct{})
|
|
go func() { cmd.Wait(); close(ended) }()
|
|
t.Cleanup(func() { cmd.Process.Kill(); <-ended })
|
|
c := &http.Client{Timeout: 2 * time.Second}
|
|
for deadline := time.Now().Add(30 * time.Second); ; time.Sleep(100 * time.Millisecond) {
|
|
if res, err := c.Get("http://" + addr + "/v1/models"); err == nil {
|
|
res.Body.Close()
|
|
break
|
|
}
|
|
select {
|
|
case <-ended:
|
|
t.Fatal("serve ended before it served")
|
|
default:
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("serve didn't answer")
|
|
}
|
|
}
|
|
|
|
// the update: a new program renamed over the old, as an update puts
|
|
// it in; the running serve's file is now gone
|
|
marker := filepath.Join(dir, "ran")
|
|
next := filepath.Join(dir, "magpie.real.new")
|
|
script := "#!/bin/sh\necho \"$$ $*\" > '" + marker + "'\nexec sleep 60\n"
|
|
if err := os.WriteFile(next, []byte(script), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Rename(next, bin); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for deadline := time.Now().Add(40 * time.Second); ; time.Sleep(200 * time.Millisecond) {
|
|
if got, err := os.ReadFile(marker); err == nil && strings.HasSuffix(string(got), "\n") {
|
|
want := strconv.Itoa(cmd.Process.Pid) + " serve\n"
|
|
if string(got) != want {
|
|
t.Fatalf("the new program ran as %q, want %q: serve's own process, with its arguments", got, want)
|
|
}
|
|
break
|
|
}
|
|
select {
|
|
case <-ended:
|
|
t.Fatal("serve ended instead of running the update in its place")
|
|
default:
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("serve ran on as the old version: the update put in where it runs from was never run")
|
|
}
|
|
}
|
|
// and it let the gateway's port go for the new one
|
|
if res, err := c.Get("http://" + addr + "/v1/models"); err == nil {
|
|
res.Body.Close()
|
|
t.Fatal("the old serve still answers on the gateway's port")
|
|
}
|
|
}
|