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.
49 lines
1.7 KiB
Go
49 lines
1.7 KiB
Go
package gui
|
|
|
|
import (
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/update"
|
|
)
|
|
|
|
// restartBeside answers the gateway's RestartPath: `magpie update`, run in
|
|
// a terminal (gaubeebangeel on X: an agent this gateway serves, at 4am),
|
|
// put version in where this magpie runs from. Before, the app ran on as
|
|
// the old version until someone quit and reopened it, which cut what the
|
|
// agents had in flight. It now restarts into it as Settings' AutoRestart
|
|
// does: at once when the restart cuts nothing short (on a Mac or Linux it
|
|
// hands the gateway over, so the agent's own requests go on), else once
|
|
// that is done (waitIdle). A magpie that wasn't the one replaced (another
|
|
// copy) stays as it is.
|
|
func (u *updater) restartBeside(version string) gateway.RestartReply {
|
|
u.mu.Lock()
|
|
restart := u.autoRestart
|
|
in := u.replaced()
|
|
if in {
|
|
// what the terminal put in is the version restarted into, as if
|
|
// this one had downloaded it: the feed isn't asked first
|
|
if version != "" && (u.latest == nil || update.Newer(version, u.latest.Version)) {
|
|
u.latest, u.notesIn = &update.Release{Version: version}, ""
|
|
}
|
|
if u.state != "checking" && u.state != "downloading" {
|
|
u.state = "ready"
|
|
}
|
|
}
|
|
u.mu.Unlock()
|
|
switch {
|
|
case !in:
|
|
return gateway.RestartReply{State: "current"}
|
|
case restart == nil:
|
|
return gateway.RestartReply{State: "unsupported"}
|
|
}
|
|
reply := gateway.RestartReply{Handover: handsOver()}
|
|
if waiting, _ := u.waitingFor(); !waiting && u.idle() {
|
|
reply.State = "restarting"
|
|
go restart() // after the answer: the restart lets this request finish
|
|
return reply
|
|
}
|
|
u.waitIdle(restart, false)
|
|
b := restartBusy()
|
|
reply.State, reply.Busy = "waiting", &b
|
|
return reply
|
|
}
|