Files
magpie/internal/update/relaunch_detach_test.go
T
yetone c68270dae7 update: a Mac opened from the Dock or Finder comes back after "Update and Restart", not quits for good (#2362, danbo610)
update.Relaunch left the shell that waits for magpie to exit and then
runs `open` in magpie's own process group. launchd ends a job's process
group when its process exits, so the shell died with magpie and the new
version never opened. ee16c657 fixed this for the Open at login agent
only (AbandonProcessGroup in the plist magpie writes). The job
LaunchServices makes for an app opened from the Dock or Finder carries
no such key, and on the reporter's macOS 13 (Intel, 0.1.1250) the
restart quit and nothing came back until a manual launch. The shell now
starts in a session of its own (detach, as RelaunchBinary's helper on
Windows and Linux already does). That holds for both job kinds, however
the previous version was started.

Tried on this Mac (macOS 26.2) with throwaway jobs, all removed after:
- A LaunchAgent without AbandonProcessGroup (a Go parent starting the
  same wait-then-run shell, then exiting): the shell in the parent's
  group never ran; one with Setsid did.
- A Cocoa .app opened with `open`, relaunching itself through the same
  shell: macOS 26 didn't kill the group, and both inherit and setsid
  relaunched. The application-job half is the reporter's macOS 13
  observation (their isolated A/B matches the agent case above). It
  was not reproduced on this OS.
- A setsid shell's `open` relaunched the .app fine (open rc 0, new pid).
Not tried: a real magpie update on macOS 13. No such machine here.

Siblings: RelaunchBinary (Windows, Linux) already detaches its child
through launch. Reexec (magpie web) runs in place. No other code
waits for magpie's pid and then opens it.

Tested: TestRelaunchOutlivesItsJobsGroup. A child of the test leads a
process group as a job's process does, starts the helper through
runAfterExit and exits. The test then kills the group as launchd does,
and the helper must still run its command. With detach(cmd) removed it
fails: "the relaunch helper died with the job's process group: the new
version was never opened" (15s). Restored, it passes (1.06s, 3 of 3).
go test -tags nogui -p 1 ./internal/update ok. gofmt -l clean. go vet
-tags nogui (darwin, GOOS=linux) and GOOS=windows go vet on
./internal/update ok.

Full suite: SUITE_LINE
2026-10-15 23:08:02 +08:00

56 lines
2.1 KiB
Go

//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")
}