From 27a8255fd793ec1b3cb078681f80d3a0ba216d77 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 24 Oct 2026 02:31:52 +0800 Subject: [PATCH] update: the Mac relaunch opens a new instance (open -n), not a reopen of the gone one (Linka on Discord) Linka's log (Discord, macOS 27.0.1, v0.1.1308) shows the cause of the "magpie stays closed after updating itself" report that molvqingtai also hit with the screen locked. After the old process's "Termination complete", open found LaunchServices still holding the gone app as running ("Specified target process 41267 does not exist"), so it sent that process a reopen Apple event (aevt/rapp) instead of launching the bundle, and failed with -600 procNotFound ("LAUNCH: Launch failed in CSUI"). The retries from fb23bca5c asked for the same reopen each time. Fix (internal/update/update.go): relaunchOpen is `open -n `, with `--args tray` when no window was up. -n asks LaunchServices for a new instance, so it launches the bundle rather than look for a running one. The retries and f3eb92924's direct start of the bundle's program stay as the backup, with relaunch.log. A magpie that really is up still has one instance: the new process takes internal/gui's instance lock (a flock on app-.lock, keyed by the program's path), finds it held and hands over (runningAlready): a tray start ends, a window start asks the running one to show its window. Other opens, checked: OpenBeside (a95f0b83f, the handover restart) uses open -n already, and when it never starts the new version the handover falls back to Relaunch after helloWait. The Codex app restart (673651e8d, internal/agent/codex_app.go) opens another app and is unchanged, as are the link and `open -b` handovers in instance_darwin.go. Tests: TestRelaunchOpen wants -n in both commands, and TestRelaunchOpenTriedAgain's fake open records its arguments and wants "-n --args tray". fakeOpen names the .app argument in its -600 text rather than $1. Red by hand: with -n dropped from relaunchOpen, TestRelaunchOpen failed for both commands and TestRelaunchOpenTriedAgain failed with opened " --args tray"; green with it. Real run: a scratch Lock.app (LSUIElement, ad-hoc signed) whose program takes the same flock as holdInstance and logs its args. Real `open -n Lock.app --args tray` twice, 1.5s apart: the first got args=[tray] and held the lock, the second got args=[tray], found the lock held and handed over. With one instance up, a plain `open Lock.app --args tray` exited 0 and started nothing (routed to the running one), the shape of Linka's reopen. The scratch app was unregistered from LaunchServices afterwards. Run at 17a2b00ef plus this change: gofmt -l internal/ (clean); go vet ./... (darwin); GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet ./...; go test -count=1 ./internal/update (ok). Rebased onto f4640c0ae (sessions GUI only); go test -count=1 ./internal/update ok again at the pushed tree. Not tried: a real update with LaunchServices holding a stale registration (Linka's case) or with the screen locked; the real magpie binary under open -n (the handover's OpenBeside has run that way since a95f0b83f). --- internal/update/relaunch_open_test.go | 8 +++++--- internal/update/relaunch_retry_test.go | 11 +++++++---- internal/update/update.go | 15 ++++++++++++--- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/internal/update/relaunch_open_test.go b/internal/update/relaunch_open_test.go index 5d34851d8..115882532 100644 --- a/internal/update/relaunch_open_test.go +++ b/internal/update/relaunch_open_test.go @@ -6,12 +6,14 @@ import ( ) // A Mac restart by itself with no window up opens the app as a login launch -// does, its menu bar icon only (#2096); one with the window opens it. +// does, its menu bar icon only (#2096); one with the window opens it. Both +// ask for a new instance (-n), so LaunchServices launches the bundle rather +// than send a reopen to the magpie that has just exited (Linka's -600). func TestRelaunchOpen(t *testing.T) { - if got, want := relaunchOpen("/Applications/magpie.app", true), `open "/Applications/magpie.app"`; got != want { + if got, want := relaunchOpen("/Applications/magpie.app", true), `open -n "/Applications/magpie.app"`; got != want { t.Errorf("window: %s, want %s", got, want) } - if got, want := relaunchOpen("/Applications/magpie.app", false), `open "/Applications/magpie.app" --args tray`; got != want { + if got, want := relaunchOpen("/Applications/magpie.app", false), `open -n "/Applications/magpie.app" --args tray`; got != want { t.Errorf("no window: %s, want %s", got, want) } } diff --git a/internal/update/relaunch_retry_test.go b/internal/update/relaunch_retry_test.go index 05fe35c76..07e7784c1 100644 --- a/internal/update/relaunch_retry_test.go +++ b/internal/update/relaunch_retry_test.go @@ -14,8 +14,8 @@ import ( // 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. +// ("-600 procNotFound"), then writes its arguments to dir/opened, -n +// included. Each try is counted in dir/tries. func fakeOpen(t *testing.T, fails int) string { t.Helper() dir := t.TempDir() @@ -23,8 +23,10 @@ func fakeOpen(t *testing.T, fails int) string { d='` + dir + `' n=$(( $(cat "$d/tries" 2>/dev/null || echo 0) + 1 )) echo $n > "$d/tries" +app= +for a in "$@"; do case "$a" in *.app) app=$a;; esac; done if [ $n -le ` + strconv.Itoa(fails) + ` ]; then - echo "_LSOpenURLsWithCompletionHandler() failed for the application $1 with error -600." >&2 + echo "_LSOpenURLsWithCompletionHandler() failed for the application $app with error -600." >&2 exit 1 fi echo "$@" > "$d/opened" @@ -66,7 +68,8 @@ func TestRelaunchOpenTriedAgain(t *testing.T) { 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 { + // -n: a new instance, not a reopen sent to the gone one + if got, want := strings.TrimSpace(string(b)), "-n "+bundle+" --args tray"; got != want { t.Fatalf("opened %q, want %q", got, want) } if n := tries(dir); n != 3 { diff --git a/internal/update/update.go b/internal/update/update.go index e72007075..040d0235b 100644 --- a/internal/update/update.go +++ b/internal/update/update.go @@ -469,12 +469,21 @@ func runAfterExit(pid int, command string) error { return cmd.Start() } -// relaunchOpen is the open command Relaunch runs for bundle. +// relaunchOpen is the open command Relaunch runs for bundle. -n asks +// LaunchServices for a new instance: without it, open looks the app up +// among those running first, and just after magpie exits LaunchServices +// can still have the gone process registered as magpie. open then sends +// it a reopen Apple event (aevt/rapp) rather than launching the bundle, +// and fails with -600, procNotFound ("Specified target process … does +// not exist", "LAUNCH: Launch failed in CSUI"; Linka's log on Discord, +// macOS 27.0.1, v0.1.1308), as often as it is tried. A magpie that is in +// fact up still has the one instance: the new process finds the instance +// lock held and hands over to it (runningAlready in internal/gui). func relaunchOpen(bundle string, window bool) string { if window { - return fmt.Sprintf("open %q", bundle) + return fmt.Sprintf("open -n %q", bundle) } - return fmt.Sprintf("open %q --args tray", bundle) + return fmt.Sprintf("open -n %q --args tray", bundle) } // ReplaceBinary puts the release's build for this binary where the