Repository navigation
fix(exec): complete durable exec when its root command exits - #205097
Merged
Merged
Conversation
A durable command that left a process in its group (nohup helper, cmd &) never completed: the macOS launchd wrapper and the Linux unit (ExitType=cgroup) recorded the exit only once the whole group was empty, so its exit never woke the session. - macOS wrapper records the exit (with the group and start time) when the command exits; stop and timeout still end the whole group. - Linux units drop ExitType=cgroup and count every exit as success so systemd keeps what the command left running; ExecMainCode/Status keep the real result. - Supervisors report those processes; the durable run hands them to the existing leftover-session owner like ordinary exec: named in the completion, process list/kill, session reset and Gateway stop end them. - A unit still holding them is released later (retention end or restart), never killed. Co-authored-by: obviyus <[email protected]>
Contributor
|
🦞👀 Pull request received. I will update this pull request when review starts. |
Collaborator
Author
|
Landed. A durable exec now completes when its root command exits, like ordinary exec. Processes it left running (for example |
github-actions Bot
pushed a commit
to Desicool/openclaw
that referenced
this pull request
Nov 26, 2026
…w#205097) ## Problem A durable background exec (`durable: true`) completed only when its whole process group was empty. A command that leaves anything running (`nohup helper &`, `cmd &`) exited, but no exit was recorded and the promised wake never came. In a real incident a durable check script exited with code 2; its nohup'd helper kept the group alive, and the owner found out 9 hours later. - macOS: the launchd wrapper's `drain()` waited for `kill(-pgid, 0)` to fail with ESRCH before it recorded the exit. - Linux: units ran with `ExitType=cgroup`, which has the same rule. Ordinary exec already completes when the root command exits and turns leftovers into tracked background sessions. Exec now has one definition of done. ## Solution - **macOS wrapper:** records the exit, the group id and the start time when the command itself exits. Stop and timeout still end the whole group (SIGTERM, then SIGKILL after the grace) before the wrapper records, as before. Verified on a real gui domain: launchd leaves the detached group alive at job exit and at `bootout`. - **Linux unit:** `ExitType=cgroup` is removed. `SuccessExitStatus` lists every exit code and standard signal, because systemd stops (and kills) a unit whose main process fails. `ExecMainCode`/`ExecMainStatus` still carry the real code or signal; `reason` already comes from them. In `active/exited`, `RuntimeMaxSec` no longer applies, which matches ordinary exec (the timeout ends with the root). - **Leftovers:** both supervisors report them (`survivors`): on Linux, the unit's `cgroup.procs`; on macOS, members of the recorded group that started before the recorded exit. A reused group id is never adopted. The durable `ManagedRun` now exposes `releasesOnRootExit` and `waitForExtinction`, so the existing `settleHostExecDescendants` → `adoptExecSurvivors` owner handles them as it does for ordinary exec: - the completion names `pid N (cmd) keeps running as background session …`; - `process list` shows them; - `process kill`, session reset and Gateway stop end them. Gateway-start adoption uses the same function. - **Unit release:** the release never kills leftovers. On Linux, `cleanup` keeps a unit whose cgroup still holds processes. It is released at the record's retention end or at the next Gateway start. The wake and the recorded result no longer wait for `cleanup`. - If the wrapper itself was killed, the registry now checks the root pid, not the group. A leftover no longer holds the "outcome unknown" report either. Removed: the wrapper's wait-for-empty-group drain on natural exit, `ExitType=cgroup`, and the registry's group-alive gating of the supervisor-lost report. No schema, protocol or dependency change. The macOS status file is wrapper-private; old wrappers still running keep their old behavior until they finish. ### What users see - A durable command's exit wakes its session within seconds with its real exit code, even when it left processes running. - Those processes keep running. The completion names them, and they behave like ordinary exec leftovers. - Accepted asymmetry: leftovers of a durable command, like ordinary leftovers, stop at Gateway stop. Only the root command survives a restart. Keeping them across restarts would need a second owner (exempting them in `stopReleasedProcesses` plus re-adoption at boot). - Accepted edge cases (named in the code): - On Linux, if leftovers outlive the 30-min completed record without a Gateway restart, the empty unit stays loaded (`active/exited`) until the user manager restarts. - A root killed by a real-time signal still fails the unit. - If leftovers cannot be listed, they keep running untracked. ## Evidence - **Red on base, green on head** (`src/agents/bash-process-launchd.test.ts`, real wrapper and real processes, `sh -c 'sleep 600 & …; exit 3'`): - base: no exit recorded; the test timed out at 120 s; - head: exit code 3 recorded in under 1 s, the leftover alive and reported by `read()`. - **Real E2E, macOS launchd** (scratch Gateway from `dist`, Node 24.22.1, real `openai/gpt-5.5`, isolated state). The agent started `sleep 600 & sleep 3; echo DURABLE_HEAD; exit 3` with `background: true, durable: true` and ended its turn. Results: - The wake arrived 4.5 s after `STARTED` (the command runs about 3 s): `Exec completed (salty-re, code 3, 5s): DURABLE_HEAD … pid 95391 (sleep 600) keeps running as background session fast-daisy:…`. - The agent replied `SEEN_DURABLE_HEAD 3`. - `process list` showed `fast-daisy:… running … sleep 600`, and `process kill` ended pid 95391. - No durable launchd job of the run remained. An earlier run also showed Gateway stop ending the leftover. - The live Gateway's launchd pid, the set of durable jobs and the live config hash are unchanged. - **Real systemd 255 `--user` manager** (arm64 Ubuntu 24.04 container, the real `systemdDurableSupervisor`): - exit code 3 recorded 1.1 s after launch (the command sleeps 1 s), with `survivors: [sleep 600]` and the leftover alive; - `cleanup` keeps the unit while the leftover lives and releases it once empty; - stop and `timeoutSeconds` while the root runs still end the whole tree; - a root killed by SIGKILL keeps its leftover. - Targeted tests on the touched files and every test that imports a changed source or `*.test-support.ts`: all pass except `bash-process-durable.test.ts` (ordinary-restart case). It fails with `FsSafeError: private Darwin creation requires native descriptor ACL inspection` and fails identically on the merge base `d266c9d13d81`. Test lines: +39 −15. Co-authored-by: Ayaan Zaidi <[email protected]>
tideclaw-bot
pushed a commit
that referenced
this pull request
Nov 28, 2026
A durable background exec (`durable: true`) completed only when its whole process group was empty. A command that leaves anything running (`nohup helper &`, `cmd &`) exited, but no exit was recorded and the promised wake never came. In a real incident a durable check script exited with code 2; its nohup'd helper kept the group alive, and the owner found out 9 hours later. - macOS: the launchd wrapper's `drain()` waited for `kill(-pgid, 0)` to fail with ESRCH before it recorded the exit. - Linux: units ran with `ExitType=cgroup`, which has the same rule. Ordinary exec already completes when the root command exits and turns leftovers into tracked background sessions. Exec now has one definition of done. - **macOS wrapper:** records the exit, the group id and the start time when the command itself exits. Stop and timeout still end the whole group (SIGTERM, then SIGKILL after the grace) before the wrapper records, as before. Verified on a real gui domain: launchd leaves the detached group alive at job exit and at `bootout`. - **Linux unit:** `ExitType=cgroup` is removed. `SuccessExitStatus` lists every exit code and standard signal, because systemd stops (and kills) a unit whose main process fails. `ExecMainCode`/`ExecMainStatus` still carry the real code or signal; `reason` already comes from them. In `active/exited`, `RuntimeMaxSec` no longer applies, which matches ordinary exec (the timeout ends with the root). - **Leftovers:** both supervisors report them (`survivors`): on Linux, the unit's `cgroup.procs`; on macOS, members of the recorded group that started before the recorded exit. A reused group id is never adopted. The durable `ManagedRun` now exposes `releasesOnRootExit` and `waitForExtinction`, so the existing `settleHostExecDescendants` → `adoptExecSurvivors` owner handles them as it does for ordinary exec: - the completion names `pid N (cmd) keeps running as background session …`; - `process list` shows them; - `process kill`, session reset and Gateway stop end them. Gateway-start adoption uses the same function. - **Unit release:** the release never kills leftovers. On Linux, `cleanup` keeps a unit whose cgroup still holds processes. It is released at the record's retention end or at the next Gateway start. The wake and the recorded result no longer wait for `cleanup`. - If the wrapper itself was killed, the registry now checks the root pid, not the group. A leftover no longer holds the "outcome unknown" report either. Removed: the wrapper's wait-for-empty-group drain on natural exit, `ExitType=cgroup`, and the registry's group-alive gating of the supervisor-lost report. No schema, protocol or dependency change. The macOS status file is wrapper-private; old wrappers still running keep their old behavior until they finish. - A durable command's exit wakes its session within seconds with its real exit code, even when it left processes running. - Those processes keep running. The completion names them, and they behave like ordinary exec leftovers. - Accepted asymmetry: leftovers of a durable command, like ordinary leftovers, stop at Gateway stop. Only the root command survives a restart. Keeping them across restarts would need a second owner (exempting them in `stopReleasedProcesses` plus re-adoption at boot). - Accepted edge cases (named in the code): - On Linux, if leftovers outlive the 30-min completed record without a Gateway restart, the empty unit stays loaded (`active/exited`) until the user manager restarts. - A root killed by a real-time signal still fails the unit. - If leftovers cannot be listed, they keep running untracked. - **Red on base, green on head** (`src/agents/bash-process-launchd.test.ts`, real wrapper and real processes, `sh -c 'sleep 600 & …; exit 3'`): - base: no exit recorded; the test timed out at 120 s; - head: exit code 3 recorded in under 1 s, the leftover alive and reported by `read()`. - **Real E2E, macOS launchd** (scratch Gateway from `dist`, Node 24.22.1, real `openai/gpt-5.5`, isolated state). The agent started `sleep 600 & sleep 3; echo DURABLE_HEAD; exit 3` with `background: true, durable: true` and ended its turn. Results: - The wake arrived 4.5 s after `STARTED` (the command runs about 3 s): `Exec completed (salty-re, code 3, 5s): DURABLE_HEAD … pid 95391 (sleep 600) keeps running as background session fast-daisy:…`. - The agent replied `SEEN_DURABLE_HEAD 3`. - `process list` showed `fast-daisy:… running … sleep 600`, and `process kill` ended pid 95391. - No durable launchd job of the run remained. An earlier run also showed Gateway stop ending the leftover. - The live Gateway's launchd pid, the set of durable jobs and the live config hash are unchanged. - **Real systemd 255 `--user` manager** (arm64 Ubuntu 24.04 container, the real `systemdDurableSupervisor`): - exit code 3 recorded 1.1 s after launch (the command sleeps 1 s), with `survivors: [sleep 600]` and the leftover alive; - `cleanup` keeps the unit while the leftover lives and releases it once empty; - stop and `timeoutSeconds` while the root runs still end the whole tree; - a root killed by SIGKILL keeps its leftover. - Targeted tests on the touched files and every test that imports a changed source or `*.test-support.ts`: all pass except `bash-process-durable.test.ts` (ordinary-restart case). It fails with `FsSafeError: private Darwin creation requires native descriptor ACL inspection` and fails identically on the merge base `5750fb88fe02`. Test lines: +39 −15. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit e80be91) Release adaptation: Reuse stable truncateCommand for the live-process census and retain the two-second ordinary-exec survivor settle. Add a systemd regression for already-removed unit cleanup. The wrapper status additions remain private and require no database change. Follow-ups: #205129 is a separate restored requester-authority refactor and is not required for root-exit completion. Co-authored-by: RomneyDa <[email protected]>
tideclaw-bot
pushed a commit
that referenced
this pull request
Nov 29, 2026
## Problem A durable background exec (`durable: true`) completed only when its whole process group was empty. A command that leaves anything running (`nohup helper &`, `cmd &`) exited, but no exit was recorded and the promised wake never came. In a real incident a durable check script exited with code 2; its nohup'd helper kept the group alive, and the owner found out 9 hours later. - macOS: the launchd wrapper's `drain()` waited for `kill(-pgid, 0)` to fail with ESRCH before it recorded the exit. - Linux: units ran with `ExitType=cgroup`, which has the same rule. Ordinary exec already completes when the root command exits and turns leftovers into tracked background sessions. Exec now has one definition of done. ## Solution - **macOS wrapper:** records the exit, the group id and the start time when the command itself exits. Stop and timeout still end the whole group (SIGTERM, then SIGKILL after the grace) before the wrapper records, as before. Verified on a real gui domain: launchd leaves the detached group alive at job exit and at `bootout`. - **Linux unit:** `ExitType=cgroup` is removed. `SuccessExitStatus` lists every exit code and standard signal, because systemd stops (and kills) a unit whose main process fails. `ExecMainCode`/`ExecMainStatus` still carry the real code or signal; `reason` already comes from them. In `active/exited`, `RuntimeMaxSec` no longer applies, which matches ordinary exec (the timeout ends with the root). - **Leftovers:** both supervisors report them (`survivors`): on Linux, the unit's `cgroup.procs`; on macOS, members of the recorded group that started before the recorded exit. A reused group id is never adopted. The durable `ManagedRun` now exposes `releasesOnRootExit` and `waitForExtinction`, so the existing `settleHostExecDescendants` → `adoptExecSurvivors` owner handles them as it does for ordinary exec: - the completion names `pid N (cmd) keeps running as background session …`; - `process list` shows them; - `process kill`, session reset and Gateway stop end them. Gateway-start adoption uses the same function. - **Unit release:** the release never kills leftovers. On Linux, `cleanup` keeps a unit whose cgroup still holds processes. It is released at the record's retention end or at the next Gateway start. The wake and the recorded result no longer wait for `cleanup`. - If the wrapper itself was killed, the registry now checks the root pid, not the group. A leftover no longer holds the "outcome unknown" report either. Removed: the wrapper's wait-for-empty-group drain on natural exit, `ExitType=cgroup`, and the registry's group-alive gating of the supervisor-lost report. No schema, protocol or dependency change. The macOS status file is wrapper-private; old wrappers still running keep their old behavior until they finish. ### What users see - A durable command's exit wakes its session within seconds with its real exit code, even when it left processes running. - Those processes keep running. The completion names them, and they behave like ordinary exec leftovers. - Accepted asymmetry: leftovers of a durable command, like ordinary leftovers, stop at Gateway stop. Only the root command survives a restart. Keeping them across restarts would need a second owner (exempting them in `stopReleasedProcesses` plus re-adoption at boot). - Accepted edge cases (named in the code): - On Linux, if leftovers outlive the 30-min completed record without a Gateway restart, the empty unit stays loaded (`active/exited`) until the user manager restarts. - A root killed by a real-time signal still fails the unit. - If leftovers cannot be listed, they keep running untracked. ## Evidence - **Red on base, green on head** (`src/agents/bash-process-launchd.test.ts`, real wrapper and real processes, `sh -c 'sleep 600 & …; exit 3'`): - base: no exit recorded; the test timed out at 120 s; - head: exit code 3 recorded in under 1 s, the leftover alive and reported by `read()`. - **Real E2E, macOS launchd** (scratch Gateway from `dist`, Node 24.22.1, real `openai/gpt-5.5`, isolated state). The agent started `sleep 600 & sleep 3; echo DURABLE_HEAD; exit 3` with `background: true, durable: true` and ended its turn. Results: - The wake arrived 4.5 s after `STARTED` (the command runs about 3 s): `Exec completed (salty-re, code 3, 5s): DURABLE_HEAD … pid 95391 (sleep 600) keeps running as background session fast-daisy:…`. - The agent replied `SEEN_DURABLE_HEAD 3`. - `process list` showed `fast-daisy:… running … sleep 600`, and `process kill` ended pid 95391. - No durable launchd job of the run remained. An earlier run also showed Gateway stop ending the leftover. - The live Gateway's launchd pid, the set of durable jobs and the live config hash are unchanged. - **Real systemd 255 `--user` manager** (arm64 Ubuntu 24.04 container, the real `systemdDurableSupervisor`): - exit code 3 recorded 1.1 s after launch (the command sleeps 1 s), with `survivors: [sleep 600]` and the leftover alive; - `cleanup` keeps the unit while the leftover lives and releases it once empty; - stop and `timeoutSeconds` while the root runs still end the whole tree; - a root killed by SIGKILL keeps its leftover. - Targeted tests on the touched files and every test that imports a changed source or `*.test-support.ts`: all pass except `bash-process-durable.test.ts` (ordinary-restart case). It fails with `FsSafeError: private Darwin creation requires native descriptor ACL inspection` and fails identically on the merge base `5750fb88fe02`. Test lines: +39 −15. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit e80be91) Release adaptation: Preserve beta.2 proportional-memory sampling documentation and update only the durable root lifetime description. Co-authored-by: RomneyDa <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A durable background exec (
durable: true) completed only when its whole process group was empty. A command that leaves anything running (nohup helper &,cmd &) exited, but no exit was recorded and the promised wake never came. In a real incident a durable check script exited with code 2; its nohup'd helper kept the group alive, and the owner found out 9 hours later.drain()waited forkill(-pgid, 0)to fail with ESRCH before it recorded the exit.ExitType=cgroup, which has the same rule.Ordinary exec already completes when the root command exits and turns leftovers into tracked background sessions. Exec now has one definition of done.
Solution
macOS wrapper: records the exit, the group id and the start time when the command itself exits. Stop and timeout still end the whole group (SIGTERM, then SIGKILL after the grace) before the wrapper records, as before. Verified on a real gui domain: launchd leaves the detached group alive at job exit and at
bootout.Linux unit:
ExitType=cgroupis removed.SuccessExitStatuslists every exit code and standard signal, because systemd stops (and kills) a unit whose main process fails.ExecMainCode/ExecMainStatusstill carry the real code or signal;reasonalready comes from them. Inactive/exited,RuntimeMaxSecno longer applies, which matches ordinary exec (the timeout ends with the root).Leftovers: both supervisors report them (
survivors): on Linux, the unit'scgroup.procs; on macOS, members of the recorded group that started before the recorded exit. A reused group id is never adopted. The durableManagedRunnow exposesreleasesOnRootExitandwaitForExtinction, so the existingsettleHostExecDescendants→adoptExecSurvivorsowner handles them as it does for ordinary exec:pid N (cmd) keeps running as background session …;process listshows them;process kill, session reset and Gateway stop end them.Gateway-start adoption uses the same function.
Unit release: the release never kills leftovers. On Linux,
cleanupkeeps a unit whose cgroup still holds processes. It is released at the record's retention end or at the next Gateway start. The wake and the recorded result no longer wait forcleanup.If the wrapper itself was killed, the registry now checks the root pid, not the group. A leftover no longer holds the "outcome unknown" report either.
Removed: the wrapper's wait-for-empty-group drain on natural exit,
ExitType=cgroup, and the registry's group-alive gating of the supervisor-lost report. No schema, protocol or dependency change. The macOS status file is wrapper-private; old wrappers still running keep their old behavior until they finish.What users see
stopReleasedProcessesplus re-adoption at boot).active/exited) until the user manager restarts.Evidence
src/agents/bash-process-launchd.test.ts, real wrapper and real processes,sh -c 'sleep 600 & …; exit 3'):read().dist, Node 24.22.1, realopenai/gpt-5.5, isolated state). The agent startedsleep 600 & sleep 3; echo DURABLE_HEAD; exit 3withbackground: true, durable: trueand ended its turn. Results:STARTED(the command runs about 3 s):Exec completed (salty-re, code 3, 5s): DURABLE_HEAD … pid 95391 (sleep 600) keeps running as background session fast-daisy:….SEEN_DURABLE_HEAD 3.process listshowedfast-daisy:… running … sleep 600, andprocess killended pid 95391.--usermanager (arm64 Ubuntu 24.04 container, the realsystemdDurableSupervisor):survivors: [sleep 600]and the leftover alive;cleanupkeeps the unit while the leftover lives and releases it once empty;timeoutSecondswhile the root runs still end the whole tree;*.test-support.ts: all pass exceptbash-process-durable.test.ts(ordinary-restart case). It fails withFsSafeError: private Darwin creation requires native descriptor ACL inspectionand fails identically on the merge base5750fb88fe02.Test lines: +39 −15.