update: a version found is downloaded at once, not when Update is clicked

canlee886 on X (11-13, again 11-15): with downloads automatic, magpie
showed a new version and downloaded it only after a click on Update.
0.1.4051 made a click go into the copy downloaded and waiting, but the
clock's check (byClock) also kept that copy, so every version out after
it waited for the click: the pill said Update while the newest had never
been fetched. A failed download likewise waited, as a pill to click, for
the next 6-hour check, and a "found" version stayed found when downloads
were turned back on.

Every check that finds a newer version now downloads it with downloads
automatic: the clock's (5 s after start, then UpdateEvery), Check now,
the Version row's refresh and a page asking for notes in another
language (relang now starts the clock's check when the feed names a
newer version). A newer one replaces the one waiting. Only a restart's
check and the update dialog's (keep: recheck, freshen), a restart
waiting for the gateway to go idle (waiting) and downloads on a click
leave it, so a click on Update still restarts at once.

One download at a time stays (begin refuses a check meanwhile); such a
check now sets again (supersede), and the feed is asked again once the
download is in, so a newer version found then takes its place. The
minute loop fetches what was left (fetchDue): "found" once downloads are
automatic, and a failed download (error with retry) after 30 minutes.
docs/subsystems/gui-shell.md has the rule (When an update downloads).

Tests: TestUpdateClockLeavesWaitingDownload, which held the old rule, is
now TestUpdateClockFetchesNewerThanWaiting; new TestUpdateFetchDue,
TestUpdateCheckDuringDownloadTakesNewer (one download in flight at most)
and TestUpdateWaitingRestartKeepsItsDownload.

Broken by hand: putting u.byClock back in the keep-staged condition
turns TestUpdateClockFetchesNewerThanWaiting red (0.1.12 out, still
0.1.11, 1 download); supersede as a no-op turns
TestUpdateCheckDuringDownloadTakesNewer red (never took 0.1.12);
fetchDue false for found turns TestUpdateFetchDue red; dropping
u.waiting turns TestUpdateWaitingRestartKeepsItsDownload red (0.1.12, 2
downloads).

Run at this tree on
ec8125e68, on mote: go test -count=1 -run 'Update|Restart|Fresh|Download|Notes|
Recheck|Relang|Wait' ./internal/gui/ ok. On mica (mote's load was 24.5
over its 18 cores): go vet ./internal/gui/
./internal/update/, GOOS=linux go vet -tags nogui ./internal/gui/,
GOOS=windows go vet ./internal/gui/ ok; gofmt -l clean;
go test -count=1 -timeout 45m ./... (mica): 101 packages ok, no FAIL
(gateway 945s, agent 296s, gui 187s).

Not tried: a real app updating across two published releases; no GUI
file changed, so no GUI run.
This commit is contained in:
yetone
2026-11-15 03:45:30 +08:00
parent ec8125e680
commit 1ac137e100
3 changed files with 273 additions and 35 deletions
+26
View File
@@ -939,6 +939,32 @@ for a plugin that does not update by itself. See
`TestDismissalHoldsForTheQuiet`, `TestUpdateQuietUntilBehindAWhile`,
`update-quiet.test.cjs` and `update-hide.test.cjs`.
### When an update downloads
With downloads automatic (`NoAutoDownload` off, the default), a version
magpie finds is downloaded at once, in the background, by whichever check
found it: the clock's (`checkByClock`, 5 s after start, then every
`UpdateEvery`), Check now, the Version row's refresh of a stale "latest"
(`refreshLatest`), or a page asking in another language (`relang` sees a
newer version and starts the clock's check). A newer version found while
one is downloaded and waiting replaces it. Only these leave the waiting
copy as it is: a restart's own check and the update dialog's (`keep`:
`recheck`, `freshen`), so a click on Update restarts at once into what it
names (canlee886 on X, 0.1.4051); a restart waiting for the gateway to go
idle (`waiting`); and downloads on a click. From 0.1.4051 to 0.1.4207 the
clock's check also kept the waiting copy, so every newer version waited
for a click (canlee886 on X, 11-15).
One download runs at a time (`begin` refuses a check while one is under
way). A check that comes meanwhile sets `again` (`supersede`), and once
the download is in the feed is asked again; a newer version then takes
its place. The minute loop also fetches what was left behind
(`fetchDue`): a `found` version once downloads are automatic again, and a
failed download (`error` with `retry`) after `retryAfter` (30 minutes),
not at the next `UpdateEvery`. See `TestUpdateClockFetchesNewerThanWaiting`,
`TestUpdateCheckDuringDownloadTakesNewer`,
`TestUpdateWaitingRestartKeepsItsDownload` and `TestUpdateFetchDue`.
### A restart to update hands the gateway over
On a Mac and on Linux, a restart into a downloaded update (the Update pill,
+81 -20
View File
@@ -66,9 +66,13 @@ type updater struct {
// downloads what it finds whatever Settings' NoAutoDownload says; with
// it on, any other check stops at found (#2296)
fetch bool
// keep: the check under way leaves a version downloaded and waiting
// as it is, as the clock's does (freshen)
keep bool
// keep: the check under way is a restart's, which leaves a version
// downloaded and waiting as it is (freshen)
keep bool
// again: a check came while a download was under way (one download
// at a time); once that one is in, the feed is asked again, and a
// newer version found then takes its place
again bool
asked time.Time // when the feed was last asked, by the clock or a click
done int64 // downloading: bytes so far, of total (0 when unknown)
total int64
@@ -196,10 +200,10 @@ func (u *updater) start() {
// restarts into, nothing (#4938)
for backendCtx.Err() == nil {
u.mu.Lock()
last := u.asked
last, st, retry := u.asked, u.state, u.retry
u.mu.Unlock()
s := settings.Load()
if updateDue(s, last, time.Now()) {
if now := time.Now(); updateDue(s, last, now) || fetchDue(s, st, retry, last, now) {
u.checkByClock()
}
u.autoTick(s)
@@ -208,6 +212,29 @@ func (u *updater) start() {
}()
}
// fetchDue is whether a version already found is fetched by itself now,
// before the clock's next check: with downloads automatic nothing found
// waits for a click (canlee886 on X). A version said found while
// downloads were off is downloaded once they are on, and one whose
// download failed is tried again retryAfter after the last try, not six
// hours later with the header's Update offering it meanwhile.
func fetchDue(s settings.Settings, state string, retry bool, last, now time.Time) bool {
if s.NoAutoUpdate || s.NoAutoDownload {
return false
}
switch state {
case "found":
return true
case "error":
return retry && now.Sub(last) >= retryAfter
}
return false
}
// retryAfter is how long a failed download waits before it is tried
// again by itself.
const retryAfter = 30 * time.Minute
// bundleOf and executable are update.Bundle and update.Executable; tests
// stand in for them.
var (
@@ -296,9 +323,24 @@ type packagedJSON struct {
func (u *updater) check() {
if u.begin() {
u.run()
} else {
u.supersede()
}
}
// supersede notes a check that came while a download is under way: one
// download runs at a time, and the one under way is finished rather than
// thrown away (a slow network would never finish one, with a release
// every few minutes); once it is in the feed is asked again, and a newer
// version out since is downloaded in its place.
func (u *updater) supersede() {
u.mu.Lock()
if u.state == "downloading" {
u.again = true
}
u.mu.Unlock()
}
// checkByClock is check, by the clock rather than a click: a version this
// computer wouldn't start is left as it is.
func (u *updater) checkByClock() {
@@ -307,6 +349,8 @@ func (u *updater) checkByClock() {
u.byClock = true
u.mu.Unlock()
u.run()
} else {
u.supersede()
}
}
@@ -351,9 +395,9 @@ func (u *updater) recheck(asked bool) {
// ask asks the feed and waits for it a little while (recheckTicks of
// recheckTick). fetch is a click's: what is found is downloaded whatever
// Settings' NoAutoDownload says. keep leaves a version downloaded and
// waiting as it is, as the clock's check does: a newer one found is not
// downloaded in its place.
// Settings' NoAutoDownload says. keep, a restart's, leaves a version
// downloaded and waiting as it is: a newer one found is not downloaded in
// its place.
func (u *updater) ask(fetch, keep bool) {
if u.begin() {
u.mu.Lock()
@@ -521,17 +565,22 @@ func (u *updater) run() {
rel, lang = u.latest, u.notesIn
}
}
// a version downloaded on a click stays the one restarted into while
// downloads wait for a click: a newer one out since isn't fetched.
// Nor is one by the clock's check: magpie releases many times a day,
// so each check found a newer one and downloaded it whole (40 to 95
// MB) in place of the one waiting, every check until a restart, on
// the user's proxy too (iiiiiimone on X). With downloads automatic a
// restart goes into the one waiting and the new version's first check
// fetches anything newer (recheck, keep, canlee886 on X), and so with
// them on a click (sakamotokurome on X). Quitting puts in the one
// waiting.
if u.staged != "" && u.latest != nil && (err != nil || !update.Newer(rel.Version, u.latest.Version) || hold || u.byClock || u.keep) {
// A version downloaded and waiting gives way to a newer one found, by
// the clock's check or any other: with downloads automatic, a version
// magpie knows is out is downloaded at once, not left for a click
// (canlee886 on X: "为何能检测到最新版本的更新,却不直接下载"). From
// 0.1.4051 to 0.1.4207 the clock's check left the one waiting, so the
// newer one was fetched only by the next version's first check. It
// stays when:
// - downloads wait for a click (hold): the one clicked for is the one
// restarted into, and nothing more is fetched (iiiiiimone on X,
// whose proxy is billed by traffic, turns them off);
// - the check is a restart's (keep, recheck): the restart goes into
// the one waiting at once, and the new version's first check
// fetches anything newer (canlee886, sakamotokurome on X);
// - a restart waits for the gateway to be idle into it (waiting).
// Quitting puts in the one waiting.
if u.staged != "" && u.latest != nil && (err != nil || !update.Newer(rel.Version, u.latest.Version) || hold || u.keep || u.waiting) {
u.state = "ready" // what was downloaded is still the latest
// the feed found it before it had its notes (the site, kept from
// GitHub's API, reads them from the releases feed, which can lag
@@ -607,6 +656,8 @@ func (u *updater) run() {
staged, err = update.StageBinary(ctx, rel)
}
u.mu.Lock()
again := u.again
u.again = false
if err != nil {
log.Println("update:", err)
u.state, u.err, u.retry, u.mirror, u.network = "error", err.Error(), true, update.MirrorOf(err), update.Network(err)
@@ -616,6 +667,9 @@ func (u *updater) run() {
if u.onReady != nil {
go u.onReady(rel.Version)
}
if again {
go u.checkByClock() // a check came meanwhile (supersede)
}
}
// install swaps the staged version in; it reports whether it did. Asked
@@ -829,10 +883,17 @@ func (u *updater) relang(version, lang string) {
return
}
u.mu.Lock()
defer u.mu.Unlock()
if u.latest == nil || u.latest.Version != version || rel.Version != version {
newer := u.latest != nil && update.Newer(rel.Version, u.latest.Version)
u.mu.Unlock()
if newer && !settings.Load().NoAutoUpdate {
// the feed names a newer version than the one held: it is
// checked for, and so downloaded, now (canlee886 on X)
u.checkByClock()
}
return // a check since has its own
}
defer u.mu.Unlock()
c := *u.latest // the download may still be reading the one held
c.Notes = rel.Notes
u.latest, u.notesIn = &c, lang
+166 -15
View File
@@ -88,13 +88,13 @@ func TestUpdateStagedGivesWayToNewer(t *testing.T) {
}
}
// The clock's check, with a version downloaded and waiting, doesn't
// download a newer one out since: magpie releases many times a day, and
// each check downloaded the whole app again on the user's proxy
// (iiiiiimone on X). Nor does the feed asked again before a restart, with
// downloads automatic (canlee886 on X). A check asked for still fetches
// the newest.
func TestUpdateClockLeavesWaitingDownload(t *testing.T) {
// The clock's check, with a version downloaded and waiting, downloads a
// newer one out since in its place: with downloads automatic, a version
// magpie knows is out is downloaded at once, not when Update is clicked
// (canlee886 on X). The feed asked again before a restart leaves the one
// waiting, which the restart goes into at once; with downloads on a click
// the clock fetches nothing more (iiiiiimone on X).
func TestUpdateClockFetchesNewerThanWaiting(t *testing.T) {
sandboxHome(t)
body := []byte("new magpie")
sum := sha256.Sum256(body)
@@ -129,23 +129,174 @@ func TestUpdateClockLeavesWaitingDownload(t *testing.T) {
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 {
t.Fatalf("%+v, %d downloads", j, downloads.Load())
}
// newer ones out, check after check: the one waiting stays
for _, v := range []string{"0.1.12", "0.1.13", "0.1.14"} {
// the same version again: nothing more is downloaded
u.checkByClock()
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 {
t.Fatalf("same version: %+v, %d downloads", j, downloads.Load())
}
// newer ones out, check after check: each is downloaded as it is found
for i, v := range []string{"0.1.12", "0.1.13"} {
latest.Store(v)
u.checkByClock()
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 || u.staged == "" {
if j := u.json(); j.State != "ready" || j.Latest != v || downloads.Load() != int32(i+2) || u.staged == "" {
t.Fatalf("%s out: %+v, %d downloads", v, j, downloads.Load())
}
}
// the restart's recheck: the one waiting is what it goes into
latest.Store("0.1.14")
u.recheck(true)
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 {
if j := u.json(); j.State != "ready" || j.Latest != "0.1.13" || downloads.Load() != 3 {
t.Fatalf("recheck: %+v, %d downloads", j, downloads.Load())
}
// a check asked for takes the newest
latest.Store("0.1.15")
// downloads on a click: the clock leaves the one waiting
downloadsOnClick(t)
u.checkByClock()
if j := u.json(); j.State != "ready" || j.Latest != "0.1.13" || downloads.Load() != 3 || u.staged == "" {
t.Fatalf("on a click: %+v, %d downloads", j, downloads.Load())
}
}
// A version found and not downloaded is fetched by the minute loop, not
// left for a click: once downloads are automatic again (found), and half an
// hour after a download failed (error with retry), not six hours later.
func TestUpdateFetchDue(t *testing.T) {
now := time.Now()
auto := settings.Settings{}
for _, c := range []struct {
name string
s settings.Settings
state string
retry bool
last time.Time
want bool
}{
{"found", auto, "found", false, now, true},
{"failed just now", auto, "error", true, now, false},
{"failed half an hour ago", auto, "error", true, now.Add(-retryAfter), true},
{"error with no retry", auto, "error", false, now.Add(-time.Hour), false},
{"ready", auto, "ready", false, now.Add(-time.Hour), false},
{"latest", auto, "latest", false, now.Add(-time.Hour), false},
{"downloads on a click", settings.Settings{NoAutoDownload: true}, "found", false, now, false},
{"never asks", settings.Settings{NoAutoUpdate: true}, "found", false, now, false},
} {
if got := fetchDue(c.s, c.state, c.retry, c.last, now); got != c.want {
t.Errorf("%s: %v, want %v", c.name, got, c.want)
}
}
}
// A check that comes while a download is under way doesn't start a second
// download beside it; once that one is in, the feed is asked again and a
// newer version found then is downloaded in its place.
func TestUpdateCheckDuringDownloadTakesNewer(t *testing.T) {
sandboxHome(t)
body := []byte("new magpie")
sum := sha256.Sum256(body)
var latest atomic.Value
latest.Store("0.1.11")
var downloads, inFlight, most atomic.Int32
hold := make(chan struct{})
started := make(chan struct{}, 4)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/asset" {
n := inFlight.Add(1)
defer inFlight.Add(-1)
for m := most.Load(); n > m && !most.CompareAndSwap(m, n); m = most.Load() {
}
if downloads.Add(1) == 1 {
started <- struct{}{}
<-hold
}
w.Write(body)
return
}
json.NewEncoder(w).Encode(update.Release{Version: latest.Load().(string), Assets: map[string]update.Asset{
update.BinaryAsset(): {URL: "http://" + r.Host + "/asset", SHA256: hex.EncodeToString(sum[:])},
}})
}))
defer srv.Close()
t.Setenv("MAGPIE_UPDATE_FEED", srv.URL)
old := Version
Version = "0.1.10"
defer func() { Version = old }()
exe, err := update.Executable()
if err != nil {
t.Fatal(err)
}
u := &updater{exe: exe}
u.self, _ = os.Stat(exe)
defer os.Remove(exe + ".new")
go u.checkByClock()
<-started
if j := u.json(); j.State != "downloading" {
t.Fatalf("first download: %+v", j)
}
// 0.1.12 comes out mid-download, and a check finds it
latest.Store("0.1.12")
u.check()
if j := u.json(); j.State != "ready" || j.Latest != "0.1.15" || downloads.Load() != 2 {
t.Fatalf("check: %+v, %d downloads", j, downloads.Load())
if j := u.json(); j.State != "downloading" || downloads.Load() != 1 {
t.Fatalf("check mid-download: %+v, %d downloads", j, downloads.Load())
}
close(hold)
deadline := time.Now().Add(10 * time.Second)
for {
if j := u.json(); j.State == "ready" && j.Latest == "0.1.12" {
break
}
if time.Now().After(deadline) {
t.Fatalf("never took 0.1.12: %+v, %d downloads", u.json(), downloads.Load())
}
time.Sleep(10 * time.Millisecond)
}
settled(t, u)
if downloads.Load() != 2 || most.Load() != 1 {
t.Fatalf("%d downloads, %d at once", downloads.Load(), most.Load())
}
}
// A restart waiting for the gateway to go idle goes into the copy it is
// waiting with: a check meanwhile doesn't swap it for a download.
func TestUpdateWaitingRestartKeepsItsDownload(t *testing.T) {
sandboxHome(t)
body := []byte("new magpie")
sum := sha256.Sum256(body)
var latest atomic.Value
latest.Store("0.1.11")
var downloads atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/asset" {
downloads.Add(1)
w.Write(body)
return
}
json.NewEncoder(w).Encode(update.Release{Version: latest.Load().(string), Assets: map[string]update.Asset{
update.BinaryAsset(): {URL: "http://" + r.Host + "/asset", SHA256: hex.EncodeToString(sum[:])},
}})
}))
defer srv.Close()
t.Setenv("MAGPIE_UPDATE_FEED", srv.URL)
old := Version
Version = "0.1.10"
defer func() { Version = old }()
exe, err := update.Executable()
if err != nil {
t.Fatal(err)
}
u := &updater{exe: exe}
u.self, _ = os.Stat(exe)
defer os.Remove(exe + ".new")
u.checkByClock()
if j := u.json(); j.State != "ready" || downloads.Load() != 1 {
t.Fatalf("%+v, %d downloads", j, downloads.Load())
}
u.mu.Lock()
u.waiting = true
u.mu.Unlock()
latest.Store("0.1.12")
u.checkByClock()
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 || u.staged == "" {
t.Fatalf("waiting: %+v, %d downloads", j, downloads.Load())
}
}