Repository navigation
fix(gateway): restart inside a marker-less LaunchAgent no longer kills its caller - #139628
Conversation
…s its caller PR #139393 removed the configured-label-only fallback from launchd in-service detection so the detached update helper's recovery CLI stops being misdetected as running inside the LaunchAgent. That left a hand-written plist that sets OPENCLAW_LAUNCHD_LABEL but neither the service marker nor launchd's own label variables undetected, so `openclaw gateway restart` from inside such a service ran `launchctl kickstart -k` synchronously and killed its caller. Add a process-ancestry fallback: isCurrentProcessInsideLaunchdService keeps the env check as the fast path, then asks launchd for the job's PID and treats the caller as in-service when that PID is in its ancestry. Restart, stop, maintenance park, install, and uninstall use it. The recovery CLI inherits the label but is reparented to launchd after the gateway exits, so it stays on the synchronous path. Related: #139393
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed September 5, 2026, 10:23 PM ET / September 6, 2026, 02:23 UTC. ClawSweeper reviewWhat this changesDetect LaunchAgent membership through process ancestry so macOS lifecycle commands safely hand off restarts or refuse operations that would kill their caller. Merge readiness✅ Ready for maintainer review The fix remains useful: inspected main still misses marker-less LaunchAgent ancestry. No actionable introduced defect was found, and the related merged recovery fix addresses a different side of the same detection boundary. Priority: P2 Review scores
Verification
How this fits togetherThe macOS service manager translates Gateway lifecycle commands into launchctl operations. Identifying commands running inside the managed service determines whether restart must use a detached helper and whether destructive operations must be refused. flowchart TD
A[Gateway lifecycle command] --> B[Check service environment]
B -->|No match| C[Read launchd PID and caller ancestry]
B -->|Match| D[Inside managed service]
C -->|Ancestor matches| D
C -->|No match| E[Normal service operation]
D --> F[Detached restart or mutation refusal]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +28, Tests +244, Docs +2. Total +274 across 8 files. View PR surface stats
Review metricsNone. Technical reviewBest possible solution: Keep one shared membership detector that protects in-service callers while preserving synchronous recovery for detached update helpers. Do we have a high-confidence way to reproduce the issue? Yes, from source: a marker-less descendant misses main's environment-only check and reaches synchronous kickstart. Native macOS reproduction was not executed in this review. Is this the best way to solve the issue? Yes. Reusing the existing launchd probe, ancestry walker, and detached helper repairs detection without restoring the configured-label false positive. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 41fba39c40aa. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
|
Merged via squash.
|
* origin/main: fix(ci): rebalance services test roots below the memory bound (openclaw#139645) fix(backup): exclude shared workspace files from state-only archives (openclaw#139598) fix(config): preserve plugin secret references in Settings saves (openclaw#139536) fix(ui): retain real-Gateway browser proof across reruns (openclaw#134274) fix(browser): preserve node filenames in proxy media (openclaw#139559) fix(qa): give hot-reload maintenance proofs their own budgets (openclaw#139580) fix(streaming): preserve assistant items and settled output (openclaw#139507) fix(android): reconnect secondary gateways after rapid lifecycle changes (openclaw#139319) fix(gateway): restart inside a marker-less LaunchAgent no longer kills its caller (openclaw#139628)
* commit 'ef5972108dc690725717613cb0c1bdaaf910c8de': fix(browser): preserve node filenames in proxy media (#139559) fix(qa): give hot-reload maintenance proofs their own budgets (#139580) fix(streaming): preserve assistant items and settled output (#139507) fix(android): reconnect secondary gateways after rapid lifecycle changes (#139319) fix(gateway): restart inside a marker-less LaunchAgent no longer kills its caller (#139628)
…s its caller (openclaw#139628) PR openclaw#139393 removed the configured-label-only fallback from launchd in-service detection so the detached update helper's recovery CLI stops being misdetected as running inside the LaunchAgent. That left a hand-written plist that sets OPENCLAW_LAUNCHD_LABEL but neither the service marker nor launchd's own label variables undetected, so `openclaw gateway restart` from inside such a service ran `launchctl kickstart -k` synchronously and killed its caller. Add a process-ancestry fallback: isCurrentProcessInsideLaunchdService keeps the env check as the fast path, then asks launchd for the job's PID and treats the caller as in-service when that PID is in its ancestry. Restart, stop, maintenance park, install, and uninstall use it. The recovery CLI inherits the label but is reparented to launchd after the gateway exits, so it stays on the synchronous path. Related: openclaw#139393
Related: #139393
What Problem This Solves
Fixes an issue where operators running
openclaw gateway restartfrom inside a macOS LaunchAgent defined by a hand-written plist (one that setsOPENCLAW_LAUNCHD_LABELbut neitherOPENCLAW_SERVICE_MARKER/OPENCLAW_SERVICE_KINDnor launchd'sLAUNCH_JOB_LABEL/LAUNCH_JOB_NAME/XPC_SERVICE_NAME), including from an agent's shell command, would have the CLI runlaunchctl kickstart -ksynchronously and kill its own caller; KeepAlive then relaunched the gateway with the restart command's outcome lost.gateway stop,install, anduninstallfrom the same position were likewise not refused. This is the tradeoff ClawSweeper flagged on #139393, which had removed the configured-label-only fallback because the detached update helper's recovery CLI inherited that label and was misdetected as in-service.Why This Change Was Made
In-service identity now has a second signal: the new async
isCurrentProcessInsideLaunchdServicekeeps the environment check as the fast path, then reads the PID launchd reports for the job (the samelaunchctl printprobereadLaunchAgentRuntimeis built on) and treats the caller as inside the service when that PID is ingetSelfAndAncestorPidsSync(), the way the update CLI's ancestry block already works. Restart (after the system-LaunchDaemon ownership assertion), stop, maintenance park, and the install/uninstall guard use it. The ancestry walk only runs once a running PID is known. The detached update helper's recovery command inheritsOPENCLAW_LAUNCHD_LABELbut is reparented to launchd once the gateway exits, so no running gateway PID is in its ancestry and it stays on the synchronous path. The update-job label check intentionally keeps the env-only rule. No new config, option, or retry; production delta is +28 lines.User Impact
Restart from inside any managed LaunchAgent, marker-less or not, hands off to the detached helper instead of killing itself; stop, install, and uninstall refuse with the existing "run this command from an external shell" guidance. Managed-update recovery after a failed update is unchanged and still restores the service synchronously with health proof. One extra read-only
launchctl printruns before these lifecycle commands.docs/cli/gateway.mddocuments the macOS behavior.Evidence
pnpm test src/daemon/launchd.test.ts src/daemon/launchd-current-service.test.ts src/infra/update-managed-service-handoff-lifecycle.test.ts: launchd 180 passed (8 new ancestry-guard cases: restart handoff vs synchronous, stop refusal with and without--disable, park, install/uninstall refusal, legacy-label candidate, KeepAlive-relaunched service during helper recovery), current-service 4 passed, lifecycle 137 passed in the worker run. Two later lifecycle runs each dropped one variant of "transfers update ownership before CLI disconnect" at its 1 svi.waitForunder host load averages of 150-250; the failing variant alternated (systemd, then launchd), that helper imports none of the changed modules, and a focused rerun of all four variants passed.recoveryInsideService: falsewithservice-restoreexit 0 ("restores launchd through a synchronous installed-CLI restart when the helper inherits only the configured label").node scripts/check-changed.mjs: 37 stages passed including lint and both typecheck lanes; oxfmt, docs format check, andgit diff --checkclean. Codex autoreview (P0/P1): scoped-clean.