update: a restart whose new version couldn't be started leaves this magpie running instead of quitting (ShipInSand on X)

restartInto logged a relaunch error that was not a system refusal and still returned true, so the caller quit with the new version installed and nothing started; the handover's fallback did the same. Both now keep this magpie up, say why in the version row (Error), and let a later restart through.

Run, on mote/mica (macOS), at origin/main de58a3879 plus this commit: TestRestartThatCouldNotStartStaysUp is red on the old update.go and handover.go ("restartToUpdate said to quit, though the new version didn't start") and green with the fix; the Restart and Handover tests in internal/gui ok; internal/update ok; gofmt -l clean; go vet for darwin, linux with -tags nogui and windows clean. Windows: internal/update Relaunch|Blocked|Install|Aside ok; the new test skips there (a failed start on Windows is always a BlockedError). Full internal/gui run at d048939da+fix: red only in TestLaneLimitRoutes (#5150), TestSettingsSaveKeepsWhatItDoesNotSend (#7127) and TestAppReplacesItsOlderGatewayOnceIdle's TempDir cleanup (#6297); the first two are red the same on origin/main de58a3879, the third passes alone on both. TestUpdateThatWontStart is red on Windows on main (#6059).

Not tried: a real failed relaunch on a real install; Linux gui tests (wsl-run lacks gtk); the Windows AwaitPredecessor/lock hypothesis (commented on #5922), which this does not change; no GUI .cjs tests (no GUI file touched).
This commit is contained in:
yetone
2026-11-19 01:25:26 +08:00
parent de58a3879b
commit 1622484a86
3 changed files with 73 additions and 5 deletions
+28 -5
View File
@@ -141,10 +141,24 @@ func beginHandover(bundle, exe string, window bool, view string, quiet bool) boo
// failed gives the handover up before it began, and quits.
func (ho *handover) failed(err error) {
ho.fail(err)
if !ho.fail(err) {
ho.stay()
return
}
(<-ho.quit)()
}
// stay lets this magpie run on: the restart's new version couldn't be
// started either, so quitting would leave nothing running. A later
// restart is let through again.
func (ho *handover) stay() {
pending.Store(nil)
select {
case <-ho.quit: // the caller's quit, not made
default:
}
}
// leave quits once a handover under way is done, else at once.
func leave(quit func()) {
if ho := pending.Load(); ho != nil {
@@ -164,7 +178,10 @@ func (ho *handover) run() {
c, err := ho.offer.Accept(ctx)
cancel()
if err != nil {
ho.fail(err)
if !ho.fail(err) {
ho.stay()
return
}
quit()
return
}
@@ -196,7 +213,10 @@ func (ho *handover) run() {
cbLn.Close()
}
retakeInstance()
ho.fail(err)
if !ho.fail(err) {
ho.stay()
return
}
quit()
return
}
@@ -253,7 +273,7 @@ var fallbackWait = 15 * time.Minute
// fail gives the handover up: once the requests in flight have finished
// (the restart didn't wait for them), the new version opens once this one
// has exited, as a restart did before.
func (ho *handover) fail(err error) {
func (ho *handover) fail(err error) bool {
log.Println("update: the gateway wasn't handed over, restarting as before once its requests have finished:", err)
if ho.offer != nil {
ho.offer.Close()
@@ -262,8 +282,11 @@ func (ho *handover) fail(err error) {
time.Sleep(200 * time.Millisecond)
}
if ferr := ho.fallback(); ferr != nil {
log.Println("update:", ferr)
// nothing will start the new version: this one stays up
log.Println("update: the new version couldn't be started, this one runs on:", ferr)
return false
}
return true
}
// takeOver, in the new magpie, takes the gateway from the one that
+9
View File
@@ -1152,7 +1152,16 @@ func restartInto(web, window bool, view string, bySelf bool) bool {
return false
}
if err != nil {
// the new version is in place but nothing started it: quitting now
// would leave no magpie running at all (ShipInSand on X: it "often
// disappears by itself"). This one runs on and says so.
log.Println("update:", err)
os.Remove(filepath.Join(settings.Dir(), updatedInAppFile))
unmarkQuietRestart()
updates.mu.Lock()
updates.err = "the new version is installed but couldn't be started (" + err.Error() + "): quit magpie and open it again"
updates.mu.Unlock()
return false
}
return true
}
+36
View File
@@ -8,6 +8,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"sync/atomic"
"syscall"
"testing"
@@ -103,3 +104,38 @@ func TestUpdateThatWontStart(t *testing.T) {
t.Error("the note outlived the version")
}
}
// ShipInSand on X: magpie "often disappears by itself" after an update. A
// restart whose new version couldn't be started (not a refusal by the
// system: the program wouldn't run at all) still told the caller to quit,
// and nothing was left running. It stays up and says why.
func TestRestartThatCouldNotStartStaysUp(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("there a start that fails is always the system's refusal (BlockedError)")
}
exe := filepath.Join(t.TempDir(), "magpie")
os.WriteFile(exe, []byte("running magpie"), 0o755)
self, _ := os.Stat(exe)
staged := exe + ".new"
os.WriteFile(staged, []byte("new magpie"), 0o755)
o := updates
u := &updater{exe: exe, self: self, staged: staged, state: "ready", latest: &update.Release{Version: "0.1.11"}}
updates = u
defer func() { updates = o }()
wasRelaunch := relaunchBinary
relaunchBinary = func(string, string, bool, string) error {
return &os.PathError{Op: "fork/exec", Path: exe, Err: syscall.ENOENT}
}
defer func() { relaunchBinary = wasRelaunch }()
if restartToUpdate(false, true, "") {
t.Fatal("restartToUpdate said to quit, though the new version didn't start")
}
if j := u.json(); j.Error == "" {
t.Errorf("nothing says why: %+v", j)
}
if _, err := os.Stat(filepath.Join(settings.Dir(), updatedInAppFile)); !os.IsNotExist(err) {
t.Errorf("noted as updated in the app: %v", err)
}
}