Skip to content

fix(exec): complete durable exec when its root command exits - #205097

Merged
obviyus merged 1 commit into
mainfrom
fix/durable-exec-root-exit
Nov 25, 2026
Merged

obviyus merged 1 commit into
mainfrom
fix/durable-exec-root-exit

Conversation

@obviyus

@obviyus obviyus commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

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.

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]>
@clawsweeper

clawsweeper Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime agents Agent runtime and tooling size: M labels Nov 25, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. impact:session-state Session, memory, transcript, context, or agent state can drift or corrupt. labels Nov 25, 2026
@obviyus
obviyus merged commit e80be91 into main Nov 25, 2026
143 checks passed
@obviyus
obviyus deleted the fix/durable-exec-root-exit branch November 25, 2026 05:25
@obviyus

obviyus commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Landed. A durable exec now completes when its root command exits, like ordinary exec. Processes it left running (for example nohup … &) keep running as ordinary background sessions that the completion names. Proof: a test that fails on the base and passes here, a real macOS launchd E2E with a real model (wake 4.5 s after the turn ended, code 3, leftover listed and killed), and a real systemd 255 user-manager run.

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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation gateway Gateway runtime impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. impact:session-state Session, memory, transcript, context, or agent state can drift or corrupt. P1 High-priority user-facing bug, regression, or broken workflow. size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant