From d3aedbe0ecf39e25f34ab8a0ed84db93cf0f1d02 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Fri, 20 Nov 2026 20:51:49 -0500 Subject: [PATCH] fix(daemon): keep a launchd agent whose state uninstall cannot observe albedo daemon uninstall on macOS stops a managed daemon over HTTP before it boots the agent out, so the daemon drains and its kernels survive. It decides whether the daemon is managed from a classification taken after its first observation. When a launchctl print failed there, the classification was "manager unavailable", uninstall skipped the shutdown, the next observation succeeded, and the bootout then ended the ready daemon and its work. The contract says a failed shutdown ends the uninstall and forces nothing down. uninstall now stops when its classification is unknown, and keeps the registration and the plist. A retry, once launchd answers, stops the daemon first as usual. TestLaunchdUninstallKeepsADaemonItCouldNotClassify replays it against the fake launchd: install, then uninstall with its third print failing. It fails without the fix. It needs the UseLaunchctl seam and the fake from the preceding commit, since the launchd paths could not run on Linux before it. --- .../daemon_service_launchd_policy.go | 5 ++ .../entrypoint/daemon_service_launchd_test.go | 76 +++++++++++++++++++ docs/daemon-lifecycle.md | 20 ++--- 3 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 cli/internal/entrypoint/daemon_service_launchd_test.go diff --git a/cli/internal/entrypoint/daemon_service_launchd_policy.go b/cli/internal/entrypoint/daemon_service_launchd_policy.go index 20bda9851..924f6eea9 100644 --- a/cli/internal/entrypoint/daemon_service_launchd_policy.go +++ b/cli/internal/entrypoint/daemon_service_launchd_policy.go @@ -386,6 +386,11 @@ func (connector *connector) uninstallLaunchd(parent context.Context) (string, er return "", errors.New("service contract unavailable or unsupported; registration and file retained") } view, discoverErr := localdaemon.ObserveService(ctx, connector.options.HomeDir) + // Without a classification the CLI cannot tell whether launchd runs a + // ready daemon, and a bootout would end it without its shutdown. + if discoverErr == nil && view.Class == localdaemon.UnknownService { + return "", fmt.Errorf("service state unavailable; registration and file retained: %s", view.Problem) + } if discoverErr == nil && view.Class == localdaemon.ManagedService { if err = connector.stopLaunchd(ctx, view, false); err != nil { return "", err diff --git a/cli/internal/entrypoint/daemon_service_launchd_test.go b/cli/internal/entrypoint/daemon_service_launchd_test.go new file mode 100644 index 000000000..705e87ab3 --- /dev/null +++ b/cli/internal/entrypoint/daemon_service_launchd_test.go @@ -0,0 +1,76 @@ +//go:build linux + +// Regressions in the launchd reconciler, replayed on Linux against the fake +// launchd. The model in daemon_service_launchd_model_test.go finds these +// states among generated histories. +package entrypoint + +import ( + "bytes" + "context" + "os" + "path/filepath" + "testing" + + "albedo/cli/internal/loginservice" + "albedo/cli/internal/testwire" +) + +// fakeLaunchdHome installs a fake launchd for a fresh default home. +func fakeLaunchdHome(t *testing.T) (*fakeLaunchd, loginservice.DaemonUnit, string) { + t.Helper() + checker, err := testwire.NewChecker() + if err != nil { + t.Fatal(err) + } + root := t.TempDir() + home := filepath.Join(root, ".albedo") + launcher := filepath.Join(root, "albedo-daemon") + if err = os.MkdirAll(home, 0700); err != nil { + t.Fatal(err) + } + if err = os.WriteFile(launcher, []byte("#!/bin/sh\nexit 1\n"), 0700); err != nil { + t.Fatal(err) + } + for key, value := range map[string]string{"HOME": root, "ALBEDO_DAEMON": launcher, "ALBEDO_ROOT": "/opt/albedo", "ALBEDO_BUILD": "fake-build", "ALBEDO_STANDBY": "0", "ALBEDO_LAUNCHCTL": "", "ALBEDO_APPIMAGE_DAEMON": "", "APPIMAGE": ""} { + t.Setenv(key, value) + } + var manager *fakeLaunchd + t.Cleanup(loginservice.UseLaunchctl(func(ctx context.Context, args ...string) (string, string, error) { + return manager.run(ctx, args...) + })) + unit, err := loginservice.ForDaemon(home) + if err != nil { + t.Fatal(err) + } + manager = newFakeLaunchd(checker, home, map[string]string{unit.Name(): unit.Path()}) + t.Cleanup(manager.close) + return manager, unit, home +} + +// When one launchctl print failed between uninstall's first observation and +// its classification, uninstall saw an unknown service, skipped the HTTP +// shutdown, and then booted the agent out, ending its ready daemon and the +// work it was draining. +func TestLaunchdUninstallKeepsADaemonItCouldNotClassify(t *testing.T) { + manager, unit, home := fakeLaunchdHome(t) + if _, err := testConnector(home).Install(context.Background()); err != nil { + t.Fatal(err) + } + plist, _ := os.ReadFile(unit.Path()) + daemon := manager.job(unit.Name()).main + // The third print is the one that classifies the service. + manager.fault, manager.counts = &launchdFault{verb: "print", nth: 3, mode: launchdFaultBefore}, map[string]int{} + if _, err := testConnector(home).Uninstall(context.Background()); err == nil { + t.Fatal("uninstall succeeded without classifying the service") + } + if !manager.fired { + t.Fatal("the fault never fired") + } + if !daemon.isAlive() || manager.job(unit.Name()).loaded == nil { + t.Fatal("uninstall booted out the agent and its ready daemon") + } + if current, _ := os.ReadFile(unit.Path()); !bytes.Equal(current, plist) { + t.Fatal("uninstall removed the plist") + } +} diff --git a/docs/daemon-lifecycle.md b/docs/daemon-lifecycle.md index 3a3cb1fb6..53ee6945e 100644 --- a/docs/daemon-lifecycle.md +++ b/docs/daemon-lifecycle.md @@ -185,15 +185,17 @@ blocks install and start, and Albedo never changes it. Status prints the and leaves the override in place. `uninstall` asks a managed daemon to stop first, so kernels normally survive. If -that shutdown fails, the uninstall ends there and forces nothing down. Otherwise -it removes the registration with one label-level `bootout` at this home's label. -A replacement that arrives after the last check can be ended by that bootout, -work in progress included. The checks keep every replacement they can see, but -the last check and the removal are not atomic. A failed or cancelled change -leaves the service file in place. Albedo does not roll it back by deleting -state. The loaded-registration checks are not a guarantee against changes the -manager does not report. Native-manager checks are platform-specific, as -described under [verification](#implementation-and-verification). +that shutdown fails, or launchd cannot be observed well enough to tell whether +it runs a ready daemon, the uninstall ends there and forces nothing down. +Otherwise it removes the registration with one label-level `bootout` at this +home's label. A replacement that arrives after the last check can be ended by +that bootout, work in progress included. The checks keep every replacement they +can see, but the last check and the removal are not atomic. A failed or +cancelled change leaves the service file in place. Albedo does not roll it back +by deleting state. The loaded-registration checks are not a guarantee against +changes the manager does not report. Native-manager checks are +platform-specific, as described under +[verification](#implementation-and-verification). ## Authentication and defaults -- knotmirror 0.0.1