update: the Mac relaunch tries open again while it fails
molvqingtai's system log (Discord): v0.1.1314 updated itself at 00:43:49, the old process exited 0, and ~160ms later `open` of the bundle failed with -600 (procNotFound): LaunchServices still sent to the process that had exited (the screen was locked). runAfterExit ran open once, nothing looked at its status, and magpie stayed closed until a manual launch at 09:45. Relaunch now runs relaunchCommand: the same open, tried again after pauses of 0.5 1 1 2 2 3 4 seconds (13.5s, 8 tries) while it exits non-zero, and not again once it succeeds. The detached own-session shell (#2362) is unchanged. The new instance is not confirmed running beyond open's exit 0. Sibling, not changed: RelaunchBinary (Windows, Linux) starts the new binary itself while this one is still up, with no open after exit; on Windows with the old binary moved aside, startChecked already waits 20s for the new one to say it is up and puts the old back otherwise. ayu's "restart to update needs several clicks" is not this shape (candidates: the install held by another program, or the busy-gateway wait) and needs its own look. Tests (internal/update/relaunch_retry_test.go): a fake `open` first on PATH refuses its first tries with the -600 text. - TestRelaunchOpenTriedAgain: relaunchCommand after a gone pid opens on the third try. Old code: FAIL "open was tried 1 times and the new version never opened" (30.04s). - TestRelaunchOpenGivesUp: a refusing open ends after len(waits)+1 tries; one that works on the second is not run again. Broken by hand (no `&& exit 0`): FAIL "open tried 4 times, want 2". - TestOpenWaitsTotal: waits add to 10-15s. Broken (0.5 1): FAIL "waits add to 1.5s". Real open: the same loop with /usr/bin/open on a scratch .app that appeared after 2s failed 3 times ("does not exist", exit 1) and opened it on the 4th, the app got --args tray. Record for this push: 13 commits on origin/mainca4a1cde0, head tree539c84e92. Go 1.26.8, sandbox HOME, GOMODCACHE/GOCACHE pinned. The full Go run and vet were at tree d6393dfc8, the same Go code as this head: only app.js comments (cut under the core script budget) and one ja string changed after it. - gofmt -l: nothing. go build ./...; go vet ./... (darwin), GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean. - go test -timeout 45m ./..., through tee, one suite on the machine: 68 packages ok. The one red was internal/gui's TestCoreScriptBudget: app.js 1,335,314 > 1,335,000, since main grew app.js under the stack. The autoscroll commit's own comments were cut (to 1,334,629) rather than the budget raised, and go test -count=1 ./internal/gui is ok at this head (113s). Packages the run took from Go's test cache had the same inputs. - GUI: the 18 files this stack touches or names, both engines, 202 of 204 pass (tree068acf128, this push less the last commit). The two reds: gui-ja, red on main sincec2636b7b5and fixed by this push's last commit (gui-ja and i18n-keys 6 of 6 there), and usage-fold's chromium "several accounts" (#2163, red on main the same way). - internal/update ok, with TestRelaunchOpenTriedAgain, TestRelaunchOpenGivesUp and TestOpenWaitsTotal. GOOS=linux and windows builds are covered by vet. The whole GUI suite ran at 1c1fc7183 (this stack's first 12 commits on0ddcf28a1): 11713 of 11769 pass. Each of the 21 red files was then run alone at the stack and at origin/main8278fd9b0. Eight are red the same way on main: plugin-card-update (#3079), quota-count-wrap and quota-name-translated (#2228), s3-sync (#2448), usage-agents-hide (#2276), usage-checkin (#3091), usage-fold's chromium "several accounts" (#2163), usage-ledger (#3104). The other 13 pass alone on both and were red only under that run's load (50 to 335): currency #3101, plugin-github #3218, provider-fetch-again #3292, rest-try-now #2564, routing-auto-reset #3293, routing-keys-fold #1612, routing-steady #1804, routing-wb-restricted #1905, sessions-folder-delete #2218, sessions-manage #3294, switch-only #3295, text-size #1866, usage-pack #3296. Not tried: a real update on a real Mac, or a real -600 from LaunchServices; nothing on Windows.
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
//go:build !windows
|
||||
|
||||
package update
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// fakeOpen puts an `open` first on PATH that fails its first fails tries
|
||||
// as LaunchServices does while it still holds the old magpie as running
|
||||
// ("-600 procNotFound"), then writes its arguments to dir/opened. Each try
|
||||
// is counted in dir/tries.
|
||||
func fakeOpen(t *testing.T, fails int) string {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
script := `#!/bin/sh
|
||||
d='` + dir + `'
|
||||
n=$(( $(cat "$d/tries" 2>/dev/null || echo 0) + 1 ))
|
||||
echo $n > "$d/tries"
|
||||
if [ $n -le ` + strconv.Itoa(fails) + ` ]; then
|
||||
echo "_LSOpenURLsWithCompletionHandler() failed for the application $1 with error -600." >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "$@" > "$d/opened"
|
||||
`
|
||||
if err := os.WriteFile(filepath.Join(dir, "open"), []byte(script), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
return dir
|
||||
}
|
||||
|
||||
// gonePid is the pid of a process that has exited, as magpie has when the
|
||||
// relaunch's open runs.
|
||||
func gonePid(t *testing.T) int {
|
||||
t.Helper()
|
||||
c := exec.Command("/bin/sh", "-c", "exit 0")
|
||||
if err := c.Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return c.Process.Pid
|
||||
}
|
||||
|
||||
func tries(dir string) int {
|
||||
b, _ := os.ReadFile(filepath.Join(dir, "tries"))
|
||||
n, _ := strconv.Atoi(strings.TrimSpace(string(b)))
|
||||
return n
|
||||
}
|
||||
|
||||
// The report: magpie updated itself and exited, and open of the new
|
||||
// bundle 160ms later failed with -600, LaunchServices still sending to
|
||||
// the process that had exited. open ran once, so magpie stayed closed
|
||||
// until the user started it nine hours later. It is tried again.
|
||||
func TestRelaunchOpenTriedAgain(t *testing.T) {
|
||||
dir := fakeOpen(t, 2)
|
||||
bundle := filepath.Join(t.TempDir(), "magpie.app")
|
||||
if err := runAfterExit(gonePid(t), relaunchCommand(bundle, false)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
opened := filepath.Join(dir, "opened")
|
||||
for end := time.Now().Add(30 * time.Second); time.Now().Before(end); time.Sleep(50 * time.Millisecond) {
|
||||
if b, err := os.ReadFile(opened); err == nil && len(b) > 0 {
|
||||
if got, want := strings.TrimSpace(string(b)), bundle+" --args tray"; got != want {
|
||||
t.Fatalf("opened %q, want %q", got, want)
|
||||
}
|
||||
if n := tries(dir); n != 3 {
|
||||
t.Fatalf("open tried %d times, want 3 (two refusals, then the open)", n)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatalf("open was tried %d times and the new version never opened", tries(dir))
|
||||
}
|
||||
|
||||
// runRetried runs retried's shell over the fake open, failing fails
|
||||
// times, to its end, and says how often open ran and how it exited.
|
||||
func runRetried(t *testing.T, fails int, waits []string) (int, error) {
|
||||
t.Helper()
|
||||
dir := fakeOpen(t, fails)
|
||||
c := exec.Command("/bin/sh", "-c", retried(`open "x.app"`, waits))
|
||||
if err := c.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- c.Wait() }()
|
||||
select {
|
||||
case err := <-done:
|
||||
return tries(dir), err
|
||||
case <-time.After(30 * time.Second):
|
||||
c.Process.Kill()
|
||||
<-done
|
||||
t.Fatal("the shell was still trying after 30s")
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// An open that never works is given up on after the last wait, not tried
|
||||
// forever by a shell nobody sees; one that works is not run again.
|
||||
func TestRelaunchOpenGivesUp(t *testing.T) {
|
||||
waits := []string{"0.1", "0.1", "0.1"}
|
||||
n, err := runRetried(t, 1000, waits)
|
||||
if err == nil {
|
||||
t.Error("never opened, and the shell said it did")
|
||||
}
|
||||
if n != len(waits)+1 {
|
||||
t.Errorf("a refusing open tried %d times, want %d", n, len(waits)+1)
|
||||
}
|
||||
n, err = runRetried(t, 1, waits)
|
||||
if err != nil {
|
||||
t.Errorf("opened on the second try, and the shell said %v", err)
|
||||
}
|
||||
if n != 2 {
|
||||
t.Errorf("open tried %d times, want 2: once refused, once opened, then no more", n)
|
||||
}
|
||||
}
|
||||
|
||||
// The waits add up to the 10 to 15 seconds a LaunchServices that hasn't
|
||||
// let go of the old process is given.
|
||||
func TestOpenWaitsTotal(t *testing.T) {
|
||||
var sum float64
|
||||
for _, w := range openWaits {
|
||||
f, err := strconv.ParseFloat(w, 64)
|
||||
if err != nil {
|
||||
t.Fatalf("wait %q: %v", w, err)
|
||||
}
|
||||
sum += f
|
||||
}
|
||||
if sum < 10 || sum > 15 {
|
||||
t.Fatalf("waits add to %.1fs, want 10 to 15", sum)
|
||||
}
|
||||
}
|
||||
@@ -353,7 +353,29 @@ func InstallAsAdmin(staged, bundle string) error {
|
||||
// window when window is set, else only its menu bar icon (`tray`, as a
|
||||
// login launch), which a restart by itself uses while no window was up.
|
||||
func Relaunch(bundle string, window bool) error {
|
||||
return runAfterExit(os.Getpid(), relaunchOpen(bundle, window))
|
||||
return runAfterExit(os.Getpid(), relaunchCommand(bundle, window))
|
||||
}
|
||||
|
||||
// openWaits are the pauses, in seconds, between tries of the relaunch's
|
||||
// open: about 13s in all. Just after magpie exits, LaunchServices can
|
||||
// still hold it as running and send the open to the process that is gone,
|
||||
// and open fails (-600, procNotFound; seen with the screen locked). Tried
|
||||
// once, nothing started magpie again until the user did, nine hours later.
|
||||
var openWaits = []string{"0.5", "1", "1", "2", "2", "3", "4"}
|
||||
|
||||
// relaunchCommand is the shell command Relaunch runs once magpie has
|
||||
// exited: open, tried again while it fails.
|
||||
func relaunchCommand(bundle string, window bool) string {
|
||||
return retried(relaunchOpen(bundle, window), openWaits)
|
||||
}
|
||||
|
||||
// retried is a shell command that runs command until it succeeds, sleeping
|
||||
// each of waits in turn after a failure; the last try's status is its own.
|
||||
func retried(command string, waits []string) string {
|
||||
if len(waits) == 0 {
|
||||
return command
|
||||
}
|
||||
return fmt.Sprintf("for w in %s; do %s && exit 0; sleep $w; done; %s", strings.Join(waits, " "), command, command)
|
||||
}
|
||||
|
||||
// runAfterExit starts a shell that runs command once pid has exited. The
|
||||
|
||||
Reference in New Issue
Block a user