Files
magpie/internal/gui/update_beside.go
T
yetone 6673171595 update: the app and magpie web follow each other's update (visamzh on X)
visamzh on X: with the desktop app and magpie web both open, an update
from either side left the other still offering it ("↑ 更新") until it was
quit by hand.

Cause: the app and magpie web are two processes over one program, each
with its own updater (state, download, latest) in memory. One restarting
to update puts the new binary in; the other is told nothing. Its
replaced() (the binary on disk is no longer the one it started from) was
read only on a click of its own Update or on magpie update's gateway ask
(restartBeside), which reaches only the process serving the gateway. And
a browser's page reloaded (backAsNew) only after a restart it asked for
itself, so magpie web's tab kept the old page and its pill even once
the process under it was new.

Fix:
- followReplaced (update_beside.go): every updater stats its binary each
  5 s and, once another magpie replaced it, restarts into it through
  restartBeside, once: at once when idle, else after the gateway's
  requests (waitIdle), as for magpie update's ask. Not for what this
  process put in itself (installed, set by install, cleared by
  wontStart), not twice (followed), not for a build that isn't a release
  (make dev, go build rebuild in place). It stops with backendCtx, so a
  copy left draining after a handover doesn't look.
- waitFor (update_wait.go): a wait for the gateway restarts into what
  another magpie put in whatever the feed says now; before, a feed out
  of reach or a "latest" answer ended the wait with nothing restarted.
- boot.js names the version that served the page (bootPrefs.version);
  app.js's servedAnew reloads a browser's page (web) when /api/update's
  current names another, on the reads it already made (load, focus, back
  into view) and once a minute while shown. The app's own windows come
  back with their new process and are left alone.
- docs/subsystems/gui-shell.md describes it.

Run (mote, through mote-run, at 1e1068c86 + this change unless said):
- gofmt -l internal/gui: nothing.
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
  ./..., go build, GOOS=linux go build -tags nogui, GOOS=windows go
  build: all ok.
- go test -tags nogui -race -count=5 -run
  'Follow|AnotherMagpies|OwnUpdate|OwnBuild|UpdateClicked|Wait|Beside|
  Restart|Install|WhatsNew|Quiet' ./internal/gui/: ok (102.9s).
- go test -tags nogui -timeout 45m ./internal/gui/... ./internal/cli/...
  ./internal/update/... . : gui, update, update/launcher and magpie ok;
  internal/cli red in TestEveryRouteHasACommandOrIsNamed (GET
  /api/context/reply has no command), red the same on origin/main
  51fa4b7eb alone, filed as #5026 (from 0872bc2a0).
- node --test internal/gui/tests/update-served-anew.test.cjs: 34/34
  (Chromium and WebKit; en, zh, zh-TW, ja, de; 1100 and 420 wide; the
  minute poll under page.clock; the app's window not reloaded).

Broken by hand, each red, then put back:
- followReplaced never restarting: TestAnotherMagpiesUpdateRestartsThisOne
  and TestAnotherMagpiesUpdateWaitsForTheGatewayThenRestarts red.
- the installed guard dropped: TestOwnUpdateIsNotFollowedAgain and
  TestUpdateClickedOverAnothersIsNotFollowedAgain red (a second restart).
- waitFor's "in" case off: TestAnotherMagpiesUpdateWaitsForTheGateway
  ThenRestarts red (the wait ends with nothing restarted).
- boot.js without the version: TestBootNamesTheVersionThatServedThePage
  red.
- servedAnew not reloading: 15 of 16 subtests red in Chromium.
- the minute poll removed: the 5 "left on screen" subtests red in WebKit.

A first cut also refused a second install() once one was in; that made
TestRestartBySelfComesBackQuietly and TestWhatsNewQuietAfterRestartTo
Update red (they restart one updater several times), so the flag only
keeps followReplaced off.

Not tried: a real pair of the app and magpie web on two release builds
with one replacing the binary (only temp files and fakes); Windows
(InstallBinaryAside starts the staged file there, so the own-install
test is skipped and the click test covers the flag) and Linux runs;
the whole GUI suite: mote was never free (two other sessions' full GUI suites, load ~400), so it runs on the Linux box after this lands.
2026-11-04 06:59:29 +08:00

90 lines
3.1 KiB
Go

package gui
import (
"context"
"time"
"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 {
u.followed = true
// 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
}
// replacedTick is how often followReplaced looks at what is on disk; tests
// shorten it.
var replacedTick = 5 * time.Second
// followReplaced restarts this magpie into an update another magpie put in
// where it runs from, as restartBeside does for magpie update's ask
// (visamzh on X). The app and magpie web run from the same program (the
// terminal's magpie is the app's, linked), and each had its own updater:
// one restarting to update put the new version in, and the other ran on
// as the old one, its Update still offering the version already in, its
// page never reloaded. Neither tells the other, and the other may serve
// no gateway to be asked on, so each looks for itself, a stat every
// replacedTick, until ctx ends (the copy an update left draining looks no
// more, #4938). Only once for a change, so a wait the user called off
// isn't started again; never for what this one put in itself (installed),
// nor in a build of its own, which a rebuild in place would restart.
func (u *updater) followReplaced(ctx context.Context) {
t := time.NewTicker(replacedTick)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
}
if !update.Released(Version) {
continue
}
u.mu.Lock()
due := u.autoRestart != nil && !u.installed && !u.followed && u.replaced()
u.mu.Unlock()
if due {
u.restartBeside("")
}
}
}