diff --git a/internal/gui/handover.go b/internal/gui/handover.go index 87184b78a..6342c62ba 100644 --- a/internal/gui/handover.go +++ b/internal/gui/handover.go @@ -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 diff --git a/internal/gui/update.go b/internal/gui/update.go index d1bf9920d..1c2cb1069 100644 --- a/internal/gui/update.go +++ b/internal/gui/update.go @@ -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 } diff --git a/internal/gui/update_blocked_test.go b/internal/gui/update_blocked_test.go index e8ab9ec96..cf0519d06 100644 --- a/internal/gui/update_blocked_test.go +++ b/internal/gui/update_blocked_test.go @@ -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) + } +}