gaubeebangeel on X has an agent run the magpie upgrade at 4am and had
to give that agent its own provider: magpie couldn't serve it through
the upgrade.
Cause: `magpie update` put the new version in and printed "quit and
reopen magpie to use it" (a .app) or "updated to X" (a binary). The
running app never restarted, so the agent (or the user) had to restart
it by hand, and a plain restart closes the port and drops streams.
Measured before, on mote, a sandbox HOME `magpie serve` with a fake
upstream streaming 61 chunks at 100ms, restarted by SIGTERM + start:
- the stream was cut at chunk 15 of 61 (unexpected EOF); SIGTERM to exit
took ~18ms, no drain;
- requests during the restart: 289 answered, 5 refused over 87ms.
Now `magpie update`, once the new version is in, POSTs it to the
running app's /v1/magpie/restart (gateway/restart.go). It is answered
only to a program on this computer over loopback; a LAN caller, a page
(Origin / Sec-Fetch) and another Host get 403. The updater's
restartBeside (gui/update_beside.go) restarts only when its own exe was
the one replaced (replaced() now also compares a .app's binary,
bundleExe), takes the version as ready without the feed, and runs the
same restart as Settings' AutoRestart: at once when it cuts nothing
short, else through waitIdle. On macOS and Linux that restart is the
existing SCM_RIGHTS handover, so the port never closes and in-flight
streams finish in the old process. The command prints what happened:
restarting (handing the gateway over), waiting for N requests, another
copy (current), magpie serve (unsupported), not running, or too old
(404: the first update into this version still needs a manual
restart).
Tests:
- TestUpdateInATerminalRestartsTheAppWithoutCuttingIt (darwin, linux):
a stream through the gateway, the exe rewritten, magpie update's ask
answered "restarting" with handover; the successor serves a request
while the stream runs, the stream ends whole, the old one quits only
after it. Red by hand with the route removed ("magpie update's ask:
404").
- TestUpdateOfAnotherCopyLeavesTheAppBe, TestRestartAskedOnlyFromThisComputer
(refused cases never reach the restarter), TestUpdateAsksTheRunningMagpieToRestart
(each reply, a 404 magpie, none running).
Run at this tree:
- rebased on bdc1617be, at this head on mote: gofmt -l clean;
GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet ./...;
go test -run 'TestUpdate|TestRestartHandsTheGatewayOver|TestHandoverNotMade'
./internal/gui ./internal/cli and -run 'TestRestartAsked|TestReadOnlyKey'
./internal/gateway: ok
- before the rebase (88ecae173), mote: go test -count=1 ./internal/gui ./internal/cli ./internal/gateway:
gateway ok (535s), cli ok; gui FAIL only on filed reds: the real-window AppKit
tests killed at their deadline on mote (#4125, #2645) and
TestSettingsSaveKeepsWhatItDoesNotSend (#4514), both red the same
run alone at 88ecae173, and TestColdTurnsTaken (#4510, a race in
full gui runs, not touched here)
- wsl-run (magpie-desktop, -tags nogui): TestRestartHandsTheGatewayOver,
TestUpdateInATerminal..., TestUpdateOfAnother...: ok
- mote: gofmt -l clean; go vet gui/gateway/cli; GOOS=linux go vet -tags
nogui ./...; GOOS=windows go vet ./...: ok
Not tried: the real GUI app end to end with a real `magpie update`
against a published release; Windows (no handover there: the restart
waits for an idle gateway and the port closes while the new version
starts, #4521); `magpie serve` / the daemon, which has no handover and
says to restart it the way it was started.
57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
)
|
|
|
|
// magpie update tells the magpie running what it put in, and says what
|
|
// came of it: a restart, a wait, magpie serve, an older magpie (404), or
|
|
// none running.
|
|
func TestUpdateAsksTheRunningMagpieToRestart(t *testing.T) {
|
|
var reply gateway.RestartReply
|
|
var asked string
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "POST" || r.URL.Path != gateway.RestartPath {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
b, _ := io.ReadAll(r.Body)
|
|
asked = string(b)
|
|
json.NewEncoder(w).Encode(reply)
|
|
}))
|
|
defer ts.Close()
|
|
for _, c := range []struct {
|
|
reply gateway.RestartReply
|
|
want string
|
|
}{
|
|
{gateway.RestartReply{State: "restarting", Version: "v0.1.1", Handover: true}, "restarts into v0.1.2 now, handing its gateway over"},
|
|
{gateway.RestartReply{State: "waiting", Version: "v0.1.1", Busy: &gateway.Busy{Requests: 2}}, "once the 2 request(s)"},
|
|
{gateway.RestartReply{State: "unsupported", Version: "v0.1.1"}, "magpie serve"},
|
|
{gateway.RestartReply{State: "current", Version: "v0.1.1"}, "another copy"},
|
|
} {
|
|
reply = c.reply
|
|
if got := restartRunning(ts.URL, "v0.1.2"); !strings.Contains(got, c.want) {
|
|
t.Errorf("%s: %q", c.reply.State, got)
|
|
}
|
|
if asked != `{"version":"v0.1.2"}` {
|
|
t.Errorf("asked %q", asked)
|
|
}
|
|
}
|
|
old := httptest.NewServer(http.NotFoundHandler())
|
|
defer old.Close()
|
|
if got := restartRunning(old.URL, "v0.1.2"); !strings.Contains(got, "quit and reopen") {
|
|
t.Errorf("an older magpie: %q", got)
|
|
}
|
|
old.Close()
|
|
if got := restartRunning(old.URL, "v0.1.2"); !strings.Contains(got, "isn't running") {
|
|
t.Errorf("none running: %q", got)
|
|
}
|
|
}
|