diff --git a/internal/update/relaunch_detach_test.go b/internal/update/relaunch_detach_test.go new file mode 100644 index 000000000..aa0d34303 --- /dev/null +++ b/internal/update/relaunch_detach_test.go @@ -0,0 +1,55 @@ +//go:build !windows + +package update + +import ( + "os" + "os/exec" + "path/filepath" + "syscall" + "testing" + "time" +) + +// jobMarkerEnv, when set, makes TestRelaunchOutlivesItsJobsGroup's binary +// the app being relaunched: it starts the helper and exits. +const jobMarkerEnv = "MAGPIE_TEST_RELAUNCH_MARKER" + +// The shell that opens the new version once magpie has quit outlives the +// end of the launchd job magpie ran in. launchd kills the job's process +// group when its process exits: the Open at login agent without +// AbandonProcessGroup, and on the reporter's macOS 13 the application job +// LaunchServices makes for an app opened from the Dock or Finder (#2362). +// The shell, left in magpie's group, went with it: magpie quit and never +// came back. Here the test's child stands for magpie, leading a group of +// its own as a job's process does; once it exits the group is killed as +// launchd kills it, and the helper must still run its command. +func TestRelaunchOutlivesItsJobsGroup(t *testing.T) { + if marker := os.Getenv(jobMarkerEnv); marker != "" { + // a moment's sleep first, so the group is killed while the helper + // is still waiting, as launchd's kill comes the moment magpie exits + if err := runAfterExit(os.Getpid(), "sleep 1; echo opened > '"+marker+"'"); err != nil { + os.Exit(3) + } + os.Exit(0) + } + marker := filepath.Join(t.TempDir(), "opened") + self, err := os.Executable() + if err != nil { + t.Fatal(err) + } + app := exec.Command(self, "-test.run=^TestRelaunchOutlivesItsJobsGroup$") + app.Env = append(os.Environ(), jobMarkerEnv+"="+marker) + app.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + if out, err := app.CombinedOutput(); err != nil { + t.Fatalf("the app: %v\n%s", err, out) + } + // launchd ending the job: every process left in its group goes + syscall.Kill(-app.Process.Pid, syscall.SIGKILL) + for end := time.Now().Add(15 * time.Second); time.Now().Before(end); time.Sleep(50 * time.Millisecond) { + if b, err := os.ReadFile(marker); err == nil && len(b) > 0 { + return + } + } + t.Fatal("the relaunch helper died with the job's process group: the new version was never opened") +} diff --git a/internal/update/update.go b/internal/update/update.go index 8d50e320b..0d4d64d1f 100644 --- a/internal/update/update.go +++ b/internal/update/update.go @@ -352,9 +352,21 @@ 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 { - script := fmt.Sprintf(`while kill -0 %d 2>/dev/null; do sleep 0.2; done; %s`, os.Getpid(), relaunchOpen(bundle, window)) + return runAfterExit(os.Getpid(), relaunchOpen(bundle, window)) +} + +// runAfterExit starts a shell that runs command once pid has exited. The +// shell is in a session of its own: launchd ends the process group of a +// job whose process exits, so one left in magpie's group was killed as +// magpie quit and the new version never opened. That is the launch agent +// of Open at login without AbandonProcessGroup (ee16c657), and, on macOS +// 13 at least, the job LaunchServices runs an app in when it is opened from +// the Dock or Finder, which magpie can't set that key on (#2362). +func runAfterExit(pid int, command string) error { + script := fmt.Sprintf(`while kill -0 %d 2>/dev/null; do sleep 0.2; done; %s`, pid, command) cmd := proc.Command("/bin/sh", "-c", script) cmd.Stdin, cmd.Stdout, cmd.Stderr = nil, nil, nil + detach(cmd) return cmd.Start() }