Repository navigation
fix(process): release exec child groups when their tool call settles - #150750
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review in progressClawSweeper is reviewing this revision. This supersedes any previous blocked status. |
|
Codex review: blocked before merge. Reviewed September 17, 2026, 6:36 AM ET / 10:36 UTC (Revision 4). ClawSweeper reviewWhat this changesThe PR releases retained host exec process groups before reporting command completion and treats permission-denied disappearance probes as continued presence within the existing deadline. Merge readiness⛔ Blocked before merge - 1 item remains This remains a useful, well-supported fix absent from inspected main. No introduced correctness defect was found. The earlier compatibility warning describes the lifecycle change already explicitly chosen and documented in the PR, rather than a new unresolved requirement. Priority: P1 Review scores
Verification
How this fits togetherOpenClaw’s exec tool starts commands through a process supervisor and retains results for later process-tool access. The supervisor’s relay owns descendant cleanup independently of the command’s exit result. flowchart TD
A[Exec command] --> B[Process supervisor]
B --> C[Command and descendants]
C --> D[Root result and output]
D --> E[Exec finalization]
E --> F[Release retained group]
F --> G[Confirm disappearance or report failure]
G --> H[Return result and retain output]
Before merge
Agent review detailsSecurityNone. PR surfaceSource +5, Tests +94, Docs +9, Other -1. Total +107 across 7 files. View PR surface stats
Review metricsNone. Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep the documented command-scoped cleanup decision, preserving supervised background commands until their root exits and leaving sandbox lifetimes with their backend. Do we have a high-confidence way to reproduce the issue? Yes: the real exec fixture supplies a concrete descendant-retention trigger, and inspected main lacks the finalization join. Contributor evidence records the failing and repaired behavior; this read-only review did not rerun it. Is this the best way to solve the issue? Yes: releasing the existing managed group at exec finalization repairs the missing ownership handoff without adding a competing reaper, timeout, or configuration path. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 62507cc516f7. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (3 earlier review cycles)
|
Release and join host exec groups after command settlement so shell-backgrounded descendants cannot retain relays and anchors across completed turns or session resets. Preserve registered background commands until their root finishes and retain sandbox backend lifetime ownership. Treat EPERM from the process-group disappearance probe as present within the existing cleanup deadline; only ESRCH confirms absence. Keep persistent groups and unexpected probe failures observable. Thanks to @php737, @terrencehinds99, @flowzito, and @michaeljgood71 for the reports and investigation. Related: #142965, #146620, #145092
1038f66 to
c18e6da
Compare
|
Merged via squash.
|
…penclaw#150750) Completed host exec commands could retain shell-backgrounded children and their relay/anchor processes until Gateway restart, exhausting process limits during repeated turns and heartbeats. Release and join the existing supervisor group before reporting completion. Preserve supervised background commands until their root finishes and leave sandbox lifetimes with their backend. Treat EPERM from the process-group disappearance probe as continued presence within the existing cleanup deadline; only ESRCH confirms absence. Persistent groups and unexpected probe failures remain observable. This intentionally stops shell-backgrounded descendants when their root exits; long-running workloads should use the existing supervised background mode. No configuration or migration is required. Validation: the original real exec regression left 15 live PIDs after five completed commands. The repaired implementation passed macOS/Linux stress proof with 100 exec calls per platform and zero survivors, plus isolated Gateway heartbeat/reset proof. The 63 owner/Doctor settlement tests and 12 focused background/sandbox/one-shot tests passed. The reviewed patch is unchanged and exact-head CI run 35208095745 is green. Thanks to @php737, @terrencehinds99, @flowzito, and @michaeljgood71 for the reports and investigation. Related: openclaw#142965, openclaw#146620, openclaw#145092
…99079) ## Problem When the agent starts a process that must keep running, OpenClaw stops it. - A completed host `exec` call cancels its whole process tree. This stops `cmd &` and `nohup cmd &`. On Linux, the child subreaper also adopts and stops processes that left with `setsid` or a double fork, such as `nginx` and `sshd`. - A one-shot `openclaw agent --local` run cancels every process in its exec scope when it exits. This also stops `background: true` services. - Result: on Linux, no start style survives the CLI. The agent reports success, and the service is gone before anything can use it. - `background: true` services also inherit the 1800 s exec timeout unless the model passes `timeoutSeconds: 0`. - The model-facing text was wrong. `background` said "Start an independent service now; survives request Stop", but the service did not survive the CLI. #150750 made the killing deliberate. Its real problem was that OpenClaw's own relay and owner processes stayed alive with the user's processes. It fixed that by stopping the user's processes too. ## Fix This is PR 1: stop killing, stay bounded. 1. **Hand off on a normal exit.** When the root command exits normally, the process-group owner (anchor) does not signal its group. It reads the process table, reports every live process the command left, stops observing lineage, and exits. On Linux it exits as the subreaper, so adopted processes go to init. If a released process still holds the command's stdout or stderr pipe, the anchor keeps draining that pipe until it closes, so the service never gets SIGPIPE or EPIPE when it logs. The cap bounds these drain anchors. 2. **Every survivor is accounted for.** The report has no size limit. It includes the owner's group members, the processes the Linux subreaper adopted (these are the owner's direct children, and they stay the command's), and all of their descendants, also those in a new session (for example the `sleep` under `setsid sh -c 'sleep 300 & wait'`). If the process table cannot be read, the anchor stops the tree like a cancellation instead of releasing processes it cannot name. 3. **Adopt survivors as sessions.** Each survivor becomes an ordinary background session in the process registry, under the session that ran the command. - The tool result names its PID and session ID. `process list` shows it, and `process kill` stops it. - It is pinned to its start time. Every signal re-checks that identity, so a reused PID is never signalled. - OpenClaw drops it within 5 s of its exit. Gateway stop stops it. 4. **A stop counts only when the exit is observed.** OpenClaw sends SIGTERM, then SIGKILL after the 5 s grace, then waits up to 2 s for the recorded identity to disappear. If a signal is denied (for example EPERM after a UID change) or the process does not disappear, the record stays, the session is marked as cleanup-uncertain, and the result says so. This applies to `process kill`, session reset and delete, Gateway stop and over-limit survivors. A later stop retries. 5. **Admission caps.** At most 16 services per session and 64 per Gateway. A service is a background session or an adopted survivor. - A `background: true` start reserves its slot before approval and startup and holds it until its session runs or startup fails, so concurrent starts cannot pass the limit. - Over the cap, a new `background: true` start fails with an error that names `process list` and `process kill`. - Over the cap, a foreground command's new survivors are stopped by identity, and the result lists their PIDs and the reason. A survivor whose stop is not confirmed stays tracked over the limit, so nothing escapes custody. - OpenClaw never stops an older process to make room. 6. **Stopping stays the same where it matters.** Timeout, no-output timeout, cancellation, request Stop, `process kill`, memory limit, host loss and launch failure still stop the whole tree, including Linux `setsid` and double-fork escapees. Scopes with `required-all` cleanup (the local TUI shell, managed workers, cron exit watchers, update repair, desktop helpers) keep strict cleanup on exit. 7. **Every reset path stops everything the session runs.** Chat `/new` and `/reset`, Gateway `sessions.reset` and session delete all call one shared step, `stopSessionResetProcesses`, after the session's subagents stop and before the replacement conversation is committed. It stops in-flight commands, `background: true` services and adopted survivors by identity. If a stop is not confirmed, the reset fails and asks to retry. Durable processes are not affected. 8. **One-shot exit hands services off.** At the end of `openclaw agent --local`: - `background: true` services and adopted survivors are handed off, and only in-flight work is cancelled. The order from #177940 stays: hand-off, then scope cleanup, then join. - The CLI prints what it left running (PID and command) to stderr. - If the CLI dies without cleanup, host loss still stops work that was not handed off. 9. **No built-in timeout for services.** `background: true` no longer inherits the 1800 s default. An explicit `timeoutSeconds` or a configured `tools.exec.timeoutSeconds` still applies. In a subagent or cron run that can wait for the command with `sessions_yield`, the default still bounds that wait, as current main requires. - `host=node` ignores `background` and runs synchronously through remote `system.run`, so it is not a service. It keeps the ordinary node timeouts (a 1800 s program timer and a longer Gateway invocation deadline, as designed in 6872be8). Without this, `exec {host:"node", background:true}` sent a zero program timer and a 10 s Gateway deadline, and the caller got an outcome-unknown failure before the command returned. 10. **The model sees the truth.** The tool result names every survivor and every stop, with PID and reason. The `background` description and the background-process docs state the real lifetime. The relay and the anchor still exit after every completed command, so #150750 does not regress. ## Proof "Base" is the behaviour without this change. "Previous head" is the head that review rated (`3549c2b`, then `bb2b295`). | # | Proof | Tier | Base / previous head | Head | |---|---|---|---|---| | 1 | 50 calls that each leave a survivor: 16 are adopted, 34 are stopped with the reason in the result, 0 relay or anchor processes remain (#150750); `process list` shows them; `process kill` stops one by identity; an exited one drops out within one reap interval; Gateway stop ends the rest | vitest-source, macOS | previous head: fails | pass | | 2 | One command leaves 20 survivors: 16 tracked, 4 stopped, every PID named, none alive and untracked; then `background: true` is refused and a new survivor is stopped | vitest-source, macOS | previous head: fails (only 16 reported) | pass | | 3 | Two concurrent `background: true` starts at 15 of 16: exactly one starts | vitest-source, macOS | previous head: fails (both start, 17 running) | pass | | 4 | A survivor's child in a new session is tracked; stopping the session ends both | vitest-source, macOS | previous head: fails (child untracked) | pass | | 5 | Real Linux run in `node:24-bookworm` (root, subreaper active) of `bash-tools.exec-group-cleanup.real.test.ts`, `subagent-reset.lifecycle.test.ts` and `worker.runtime.test.ts`: survivors, cap, reservation, descendant closure, `setsid sh -c 'sleep & wait'`, timeout of `setsid`/double-fork children, unconfirmed stops, chat/RPC reset | vitest-source, Linux container (fresh `pnpm install`, no skips) | `bb2b295`: 8 failed (every survivor test: the subreaper-adopted survivors were classed as helpers, so nothing was reported) | 96 of 96 passed, 0 skipped | | 6 | Signals to a survivor are denied (EPERM): reset fails, the session stays running with cleanup uncertainty in its output, the process is still tracked; after signals work, a retry stops it and settles the record | vitest-source, macOS | previous head: fails (reset reported success, record dropped) | pass | | 7 | Chat `/reset` (`initSessionState`) and Gateway `sessions.reset` each stop a running `background: true` service and a `cmd &` survivor before the new session is committed | vitest-source, macOS | previous head: chat path fails (both processes still alive); RPC path passes | pass, both paths | | 9 | Detached styles (`node &`, `nohup`, plain `&`, double fork) survive a completed call; a timeout still stops what the call started; one-shot cleanup hands off and prints services and stops in-flight work; handed-off servers that log every request answer 20 of 20 requests 10 s after the run | vitest-source, macOS | — | pass | | 10 | A timeout stops `setsid` and double-fork children | vitest-source, Linux container | — | pass (part of row 5) | | 11 | Supervisor, relay, anchor, exec, process tool, exec follow-up (`sessions_yield`), cron exit watchers (`required-all` extinction), session reset/delete, agent delete, TUI, desktop and chat session suites | vitest-source, macOS | — | see "Test results" below | | 12 | CLI lifecycle, real model: `openclaw agent --local` with five start styles; the server is probed 0, 3 and 10 s after the CLI exits, then sent 20 requests | source (`ocx src`), macOS | base (`669576890a1f`): 1 of 5 alive (only the double-fork daemon, which leaves the group); `background: true` answers 200 during the run, then dies at CLI exit | 5 of 5 alive, each answers 20 of 20 requests; the CLI prints every handed-off PID; 0 relay/anchor processes after the servers stop | | 13 | Gateway lifecycle, real model, one session: start `background: true`, `nohup &` and plain `&` servers; check them in the next turn; chat `/reset`; start again; RPC `sessions.reset`; start again; stop the Gateway | source (`ocx src -- gateway run`), macOS | base: `nohup &` and plain `&` die at the end of their own call; `background: true` survives chat `/reset` and RPC reset | all 3 survive across turns; chat `/reset` stops all 3; RPC reset stops all 3; Gateway stop stops all 3; 0 relay/anchor processes left | | 14 | Upgrade: latest stable `2026.11.3` runs on a state dir with `tools.exec.timeoutSeconds: 90`, then this head (`doctor --fix --non-interactive`, as update does) runs on the same state | stable: published npm package; head: source (`ocx src`), macOS | stable reads 90 | head still reads 90 after doctor; a `background: true` call gets a deadline 90 s after its start ("times out in 1m30s"); without a configured timeout, the same call on head has no deadline | | 15 | Linux: five styles plus a timeout case in `node:24-bookworm` as root, `openclaw agent --local`, Node runtime | dist-nodts, Linux (this head's `dist/` laid over the `2026.11.5-beta.3` install) | published `2026.11.5-beta.2`: 0 of 5 alive | 5 of 5 alive, each answers 20 of 20 requests 10 s after the CLI exits; the timeout case stops both the `setsid` server and the double-fork daemon | | 16 | `exec {host:"node", background:true}` through `createExecTool` to the node `system.run` invocation keeps the ordinary 1800 s program timer and 1805 s Gateway deadline, and completes | vitest-source, macOS | previous head: fails (program timer 0) | pass | <details> <summary>Redacted captures (CLI and Gateway, head and base)</summary> CLI, base `669576890a1f`, `nohup … &` (the agent's own check fails, then the probes): ```text exec: nohup python3 -m http.server 18972 --bind 127.0.0.1 --directory /tmp > /tmp/oc-srv-18972.log 2>&1 & result: completed 0, (no output) exec: sleep 2; curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:18972/ result: 000 (Command exited with code 7) reply: FAILED 18972 000 after_exit_plus=0s curl=000 · 3s curl=000 · 10s curl=000 · twenty_requests_after_10s=0/20 ``` CLI, head, `nohup … &`: ```text exec result: pid 70493 (…/Python -m http.server 18982 --bind 127.0.0.1 --directory /tmp) keeps running as background session mild-sage:2b822b901780: process list shows it and process kill mild-sage:2b822b901780 stops it. Its later output to this command's stdout/stderr is discarded; redirect it to a file to keep it. reply: STARTED 18982 200 stderr: [openclaw] Left running after this run (OpenClaw no longer tracks them; stop with kill <pid>): pid 70493 …/Python -m http.server 18982 --bind 127.0.0.1 --directory /tmp after_exit_plus=0s curl=200 · 3s curl=200 · 10s curl=200 · twenty_requests_after_10s=20/20 ``` Gateway, base: ```text [turn1] exit=0 reply=STARTED [after-start-1] port=18991 curl=200 [after-start-1] port=18992 curl=000 [after-start-1] port=18993 curl=000 [turn2] exit=0 reply=18991=200 18992=000 18993=000 [after-next-turn] port=18991 curl=200 [after-next-turn] port=18992 curl=000 [after-next-turn] port=18993 curl=000 [turn3] exit=0 reply=✅ Session reset. [after-chat-reset] port=18991 curl=200 [after-chat-reset] port=18992 curl=000 [after-chat-reset] port=18993 curl=000 [turn4] exit=0 reply=STARTED [after-start-2] port=18991 curl=200 [after-start-2] port=18992 curl=000 [after-start-2] port=18993 curl=000 [rpc-reset] ok=true [after-rpc-reset] port=18991 curl=200 [after-rpc-reset] port=18992 curl=000 [after-rpc-reset] port=18993 curl=000 [turn5] exit=0 reply=STARTED [after-start-3] port=18991 curl=200 [after-start-3] port=18992 curl=000 [after-start-3] port=18993 curl=000 [after-gateway-stop] port=18991 curl=000 [after-gateway-stop] port=18992 curl=000 [after-gateway-stop] port=18993 curl=000 helpers_left=0 ``` Gateway, head: ```text [turn1] exit=0 reply=STARTED [after-start-1] port=18991 curl=200 [after-start-1] port=18992 curl=200 [after-start-1] port=18993 curl=200 [turn2] exit=0 reply=18991=200 18992=200 18993=200 [after-next-turn] port=18991 curl=200 [after-next-turn] port=18992 curl=200 [after-next-turn] port=18993 curl=200 [turn3] exit=0 reply=✅ Session reset. [after-chat-reset] port=18991 curl=000 [after-chat-reset] port=18992 curl=000 [after-chat-reset] port=18993 curl=000 [turn4] exit=0 reply=STARTED [after-start-2] port=18991 curl=200 [after-start-2] port=18992 curl=200 [after-start-2] port=18993 curl=200 [rpc-reset] ok=true [after-rpc-reset] port=18991 curl=000 [after-rpc-reset] port=18992 curl=000 [after-rpc-reset] port=18993 curl=000 [turn5] exit=0 reply=STARTED [after-start-3] port=18991 curl=200 [after-start-3] port=18992 curl=200 [after-start-3] port=18993 curl=200 [after-gateway-stop] port=18991 curl=000 [after-gateway-stop] port=18992 curl=000 [after-gateway-stop] port=18993 curl=000 helpers_left=0 (the script counted 1, an unrelated test-runner process whose arguments name the helper files; a direct check found 0 relay/anchor processes) ``` </details> ## Test results (head) - `ocx test` on 139 files (supervisor, relay, anchor, exec, process tool, exec follow-up, cron exit watchers, session reset/delete, agent delete, chat session, subagent reset, TUI, desktop): 10 shards pass, 1456 tests passed, 0 failed (previous head). - After the CI fixes: exec, worker runtime, supervisor, relay, anchor, adapters, TUI shell, exec follow-up, process scope and subagent reset suites: 5 shards, 416 passed, 0 failed (macOS); the Linux container run in row 5. - Not in that run: `bash-tools.exec.approval-id.test.ts`. One of its checks fails in this worktree only because the worktree path contains the word "process"; it passes on CI paths. - `pnpm tsgo:core` clean; oxlint and oxfmt clean on the changed files. ## Limits - The `--dts` build and packed smoke did not run. `ocx build --dts` fails locally with `PLUGIN SDK DTS TOO LARGE` on clean main commits too; CI's `build-artifacts` job on main measured the declarations under the limit. The Linux proof uses the dist-nodts build laid over a published install. - On macOS, a process that leaves the command's process group (`setsid`, double fork) re-parents to launchd before the census unless a tracked parent still holds it. OpenClaw does not see it, so it is not adopted, not capped, and it keeps running after session reset and Gateway stop, as in every other harness compared below. On Linux the subreaper sees it. - A process that a tracked survivor forks after the hand-off is not tracked separately; stopping the survivor does not stop it. - Test lines exceed the 400-line allowance by 150; `config/test-lines-allocations/199079-exec-detached-process-ownership.txt` records them. Each added test is a real-process regression for one review finding. - Adopted services count as live background sessions, so, like `background: true` services, they block a safe Gateway restart until they exit or are stopped. ## How this compares | Harness | Process left by a completed command | Bound while running | Session reset / new | Host stop | One-shot CLI exit | |---|---|---|---|---|---| | Codex | Survives; only timeout or Ctrl-C kills the group | No | Session shutdown terminates all its unified-exec processes | Killed with the session | Survives | | Pi | Survives; only in-flight commands are tracked | No | Kills in-flight commands only | Kills in-flight commands only | Survives | | OMP | Survives; the shell is kept while background jobs run, reaped every 5 s | 15 running background jobs; over the limit, new jobs fail | `newSession()` cancels the session's own async jobs | Kept shells and their jobs die with the harness | Killed with the harness | | Hermes | Tracked in a session-scoped process registry | 64 tracked records; prunes only finished ones | Agent close and session end kill the session's processes, except explicitly persisted ones | — | — | | OpenClaw before | Killed at the end of the call (Linux: daemons too) | — | Killed earlier, at the end of the call | Killed | Killed | | OpenClaw after | Survives as a tracked background session, reaped every 5 s, named in the result and `process list` | 16 per session, 64 per Gateway; over the limit, new services are refused or stopped, old ones never evicted | Stops in-flight work, `background: true` services and survivors; durable processes stay | Stopped | Handed off and printed (as Codex and Pi) | In every harness, a process that daemonized itself and was never tracked keeps running. Sources: Codex `codex-rs/core/src/exec.rs`, `spawn.rs` and `core/src/session/handlers.rs` (`terminate_all_processes`); Pi `packages/coding-agent/src/core/tools/bash.ts`; OMP `packages/coding-agent/src/exec/bash-executor.ts` (`RETAIN_REAP_INTERVAL_MS`), `src/async/job-manager.ts` (`DEFAULT_MAX_RUNNING_JOBS = 15`) and `newSession()` (`#cancelOwnAsyncJobs`); Hermes `tools/process_registry.py` (`MAX_PROCESSES = 64`, `kill_all`). ## Follow-ups (PR 2 and later) - A strict process scope for cron and heartbeat runs, with a tool notice that everything ends with the run. - A durable PID ledger so services are still owned after a Gateway crash. - One ownership policy: remove the service-marker and platform predicates and the PTY rejection. - cgroup and RSS memory budgets for adopted services. - File-backed stdio for all exec, which removes the drain anchor. Co-authored-by: Ayaan Zaidi <[email protected]>
…enclaw#199079) ## Problem When the agent starts a process that must keep running, OpenClaw stops it. - A completed host `exec` call cancels its whole process tree. This stops `cmd &` and `nohup cmd &`. On Linux, the child subreaper also adopts and stops processes that left with `setsid` or a double fork, such as `nginx` and `sshd`. - A one-shot `openclaw agent --local` run cancels every process in its exec scope when it exits. This also stops `background: true` services. - Result: on Linux, no start style survives the CLI. The agent reports success, and the service is gone before anything can use it. - `background: true` services also inherit the 1800 s exec timeout unless the model passes `timeoutSeconds: 0`. - The model-facing text was wrong. `background` said "Start an independent service now; survives request Stop", but the service did not survive the CLI. openclaw#150750 made the killing deliberate. Its real problem was that OpenClaw's own relay and owner processes stayed alive with the user's processes. It fixed that by stopping the user's processes too. ## Fix This is PR 1: stop killing, stay bounded. 1. **Hand off on a normal exit.** When the root command exits normally, the process-group owner (anchor) does not signal its group. It reads the process table, reports every live process the command left, stops observing lineage, and exits. On Linux it exits as the subreaper, so adopted processes go to init. If a released process still holds the command's stdout or stderr pipe, the anchor keeps draining that pipe until it closes, so the service never gets SIGPIPE or EPIPE when it logs. The cap bounds these drain anchors. 2. **Every survivor is accounted for.** The report has no size limit. It includes the owner's group members, the processes the Linux subreaper adopted (these are the owner's direct children, and they stay the command's), and all of their descendants, also those in a new session (for example the `sleep` under `setsid sh -c 'sleep 300 & wait'`). If the process table cannot be read, the anchor stops the tree like a cancellation instead of releasing processes it cannot name. 3. **Adopt survivors as sessions.** Each survivor becomes an ordinary background session in the process registry, under the session that ran the command. - The tool result names its PID and session ID. `process list` shows it, and `process kill` stops it. - It is pinned to its start time. Every signal re-checks that identity, so a reused PID is never signalled. - OpenClaw drops it within 5 s of its exit. Gateway stop stops it. 4. **A stop counts only when the exit is observed.** OpenClaw sends SIGTERM, then SIGKILL after the 5 s grace, then waits up to 2 s for the recorded identity to disappear. If a signal is denied (for example EPERM after a UID change) or the process does not disappear, the record stays, the session is marked as cleanup-uncertain, and the result says so. This applies to `process kill`, session reset and delete, Gateway stop and over-limit survivors. A later stop retries. 5. **Admission caps.** At most 16 services per session and 64 per Gateway. A service is a background session or an adopted survivor. - A `background: true` start reserves its slot before approval and startup and holds it until its session runs or startup fails, so concurrent starts cannot pass the limit. - Over the cap, a new `background: true` start fails with an error that names `process list` and `process kill`. - Over the cap, a foreground command's new survivors are stopped by identity, and the result lists their PIDs and the reason. A survivor whose stop is not confirmed stays tracked over the limit, so nothing escapes custody. - OpenClaw never stops an older process to make room. 6. **Stopping stays the same where it matters.** Timeout, no-output timeout, cancellation, request Stop, `process kill`, memory limit, host loss and launch failure still stop the whole tree, including Linux `setsid` and double-fork escapees. Scopes with `required-all` cleanup (the local TUI shell, managed workers, cron exit watchers, update repair, desktop helpers) keep strict cleanup on exit. 7. **Every reset path stops everything the session runs.** Chat `/new` and `/reset`, Gateway `sessions.reset` and session delete all call one shared step, `stopSessionResetProcesses`, after the session's subagents stop and before the replacement conversation is committed. It stops in-flight commands, `background: true` services and adopted survivors by identity. If a stop is not confirmed, the reset fails and asks to retry. Durable processes are not affected. 8. **One-shot exit hands services off.** At the end of `openclaw agent --local`: - `background: true` services and adopted survivors are handed off, and only in-flight work is cancelled. The order from openclaw#177940 stays: hand-off, then scope cleanup, then join. - The CLI prints what it left running (PID and command) to stderr. - If the CLI dies without cleanup, host loss still stops work that was not handed off. 9. **No built-in timeout for services.** `background: true` no longer inherits the 1800 s default. An explicit `timeoutSeconds` or a configured `tools.exec.timeoutSeconds` still applies. In a subagent or cron run that can wait for the command with `sessions_yield`, the default still bounds that wait, as current main requires. - `host=node` ignores `background` and runs synchronously through remote `system.run`, so it is not a service. It keeps the ordinary node timeouts (a 1800 s program timer and a longer Gateway invocation deadline, as designed in 90a62a0). Without this, `exec {host:"node", background:true}` sent a zero program timer and a 10 s Gateway deadline, and the caller got an outcome-unknown failure before the command returned. 10. **The model sees the truth.** The tool result names every survivor and every stop, with PID and reason. The `background` description and the background-process docs state the real lifetime. The relay and the anchor still exit after every completed command, so openclaw#150750 does not regress. ## Proof "Base" is the behaviour without this change. "Previous head" is the head that review rated (`3549c2b`, then `bb2b295`). | # | Proof | Tier | Base / previous head | Head | |---|---|---|---|---| | 1 | 50 calls that each leave a survivor: 16 are adopted, 34 are stopped with the reason in the result, 0 relay or anchor processes remain (openclaw#150750); `process list` shows them; `process kill` stops one by identity; an exited one drops out within one reap interval; Gateway stop ends the rest | vitest-source, macOS | previous head: fails | pass | | 2 | One command leaves 20 survivors: 16 tracked, 4 stopped, every PID named, none alive and untracked; then `background: true` is refused and a new survivor is stopped | vitest-source, macOS | previous head: fails (only 16 reported) | pass | | 3 | Two concurrent `background: true` starts at 15 of 16: exactly one starts | vitest-source, macOS | previous head: fails (both start, 17 running) | pass | | 4 | A survivor's child in a new session is tracked; stopping the session ends both | vitest-source, macOS | previous head: fails (child untracked) | pass | | 5 | Real Linux run in `node:24-bookworm` (root, subreaper active) of `bash-tools.exec-group-cleanup.real.test.ts`, `subagent-reset.lifecycle.test.ts` and `worker.runtime.test.ts`: survivors, cap, reservation, descendant closure, `setsid sh -c 'sleep & wait'`, timeout of `setsid`/double-fork children, unconfirmed stops, chat/RPC reset | vitest-source, Linux container (fresh `pnpm install`, no skips) | `bb2b295`: 8 failed (every survivor test: the subreaper-adopted survivors were classed as helpers, so nothing was reported) | 96 of 96 passed, 0 skipped | | 6 | Signals to a survivor are denied (EPERM): reset fails, the session stays running with cleanup uncertainty in its output, the process is still tracked; after signals work, a retry stops it and settles the record | vitest-source, macOS | previous head: fails (reset reported success, record dropped) | pass | | 7 | Chat `/reset` (`initSessionState`) and Gateway `sessions.reset` each stop a running `background: true` service and a `cmd &` survivor before the new session is committed | vitest-source, macOS | previous head: chat path fails (both processes still alive); RPC path passes | pass, both paths | | 9 | Detached styles (`node &`, `nohup`, plain `&`, double fork) survive a completed call; a timeout still stops what the call started; one-shot cleanup hands off and prints services and stops in-flight work; handed-off servers that log every request answer 20 of 20 requests 10 s after the run | vitest-source, macOS | — | pass | | 10 | A timeout stops `setsid` and double-fork children | vitest-source, Linux container | — | pass (part of row 5) | | 11 | Supervisor, relay, anchor, exec, process tool, exec follow-up (`sessions_yield`), cron exit watchers (`required-all` extinction), session reset/delete, agent delete, TUI, desktop and chat session suites | vitest-source, macOS | — | see "Test results" below | | 12 | CLI lifecycle, real model: `openclaw agent --local` with five start styles; the server is probed 0, 3 and 10 s after the CLI exits, then sent 20 requests | source (`ocx src`), macOS | base (`0a8e0b7d9cd0`): 1 of 5 alive (only the double-fork daemon, which leaves the group); `background: true` answers 200 during the run, then dies at CLI exit | 5 of 5 alive, each answers 20 of 20 requests; the CLI prints every handed-off PID; 0 relay/anchor processes after the servers stop | | 13 | Gateway lifecycle, real model, one session: start `background: true`, `nohup &` and plain `&` servers; check them in the next turn; chat `/reset`; start again; RPC `sessions.reset`; start again; stop the Gateway | source (`ocx src -- gateway run`), macOS | base: `nohup &` and plain `&` die at the end of their own call; `background: true` survives chat `/reset` and RPC reset | all 3 survive across turns; chat `/reset` stops all 3; RPC reset stops all 3; Gateway stop stops all 3; 0 relay/anchor processes left | | 14 | Upgrade: latest stable `2026.11.3` runs on a state dir with `tools.exec.timeoutSeconds: 90`, then this head (`doctor --fix --non-interactive`, as update does) runs on the same state | stable: published npm package; head: source (`ocx src`), macOS | stable reads 90 | head still reads 90 after doctor; a `background: true` call gets a deadline 90 s after its start ("times out in 1m30s"); without a configured timeout, the same call on head has no deadline | | 15 | Linux: five styles plus a timeout case in `node:24-bookworm` as root, `openclaw agent --local`, Node runtime | dist-nodts, Linux (this head's `dist/` laid over the `2026.11.5-beta.3` install) | published `2026.11.5-beta.2`: 0 of 5 alive | 5 of 5 alive, each answers 20 of 20 requests 10 s after the CLI exits; the timeout case stops both the `setsid` server and the double-fork daemon | | 16 | `exec {host:"node", background:true}` through `createExecTool` to the node `system.run` invocation keeps the ordinary 1800 s program timer and 1805 s Gateway deadline, and completes | vitest-source, macOS | previous head: fails (program timer 0) | pass | <details> <summary>Redacted captures (CLI and Gateway, head and base)</summary> CLI, base `0a8e0b7d9cd0`, `nohup … &` (the agent's own check fails, then the probes): ```text exec: nohup python3 -m http.server 18972 --bind 127.0.0.1 --directory /tmp > /tmp/oc-srv-18972.log 2>&1 & result: completed 0, (no output) exec: sleep 2; curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:18972/ result: 000 (Command exited with code 7) reply: FAILED 18972 000 after_exit_plus=0s curl=000 · 3s curl=000 · 10s curl=000 · twenty_requests_after_10s=0/20 ``` CLI, head, `nohup … &`: ```text exec result: pid 70493 (…/Python -m http.server 18982 --bind 127.0.0.1 --directory /tmp) keeps running as background session mild-sage:2b822b901780: process list shows it and process kill mild-sage:2b822b901780 stops it. Its later output to this command's stdout/stderr is discarded; redirect it to a file to keep it. reply: STARTED 18982 200 stderr: [openclaw] Left running after this run (OpenClaw no longer tracks them; stop with kill <pid>): pid 70493 …/Python -m http.server 18982 --bind 127.0.0.1 --directory /tmp after_exit_plus=0s curl=200 · 3s curl=200 · 10s curl=200 · twenty_requests_after_10s=20/20 ``` Gateway, base: ```text [turn1] exit=0 reply=STARTED [after-start-1] port=18991 curl=200 [after-start-1] port=18992 curl=000 [after-start-1] port=18993 curl=000 [turn2] exit=0 reply=18991=200 18992=000 18993=000 [after-next-turn] port=18991 curl=200 [after-next-turn] port=18992 curl=000 [after-next-turn] port=18993 curl=000 [turn3] exit=0 reply=✅ Session reset. [after-chat-reset] port=18991 curl=200 [after-chat-reset] port=18992 curl=000 [after-chat-reset] port=18993 curl=000 [turn4] exit=0 reply=STARTED [after-start-2] port=18991 curl=200 [after-start-2] port=18992 curl=000 [after-start-2] port=18993 curl=000 [rpc-reset] ok=true [after-rpc-reset] port=18991 curl=200 [after-rpc-reset] port=18992 curl=000 [after-rpc-reset] port=18993 curl=000 [turn5] exit=0 reply=STARTED [after-start-3] port=18991 curl=200 [after-start-3] port=18992 curl=000 [after-start-3] port=18993 curl=000 [after-gateway-stop] port=18991 curl=000 [after-gateway-stop] port=18992 curl=000 [after-gateway-stop] port=18993 curl=000 helpers_left=0 ``` Gateway, head: ```text [turn1] exit=0 reply=STARTED [after-start-1] port=18991 curl=200 [after-start-1] port=18992 curl=200 [after-start-1] port=18993 curl=200 [turn2] exit=0 reply=18991=200 18992=200 18993=200 [after-next-turn] port=18991 curl=200 [after-next-turn] port=18992 curl=200 [after-next-turn] port=18993 curl=200 [turn3] exit=0 reply=✅ Session reset. [after-chat-reset] port=18991 curl=000 [after-chat-reset] port=18992 curl=000 [after-chat-reset] port=18993 curl=000 [turn4] exit=0 reply=STARTED [after-start-2] port=18991 curl=200 [after-start-2] port=18992 curl=200 [after-start-2] port=18993 curl=200 [rpc-reset] ok=true [after-rpc-reset] port=18991 curl=000 [after-rpc-reset] port=18992 curl=000 [after-rpc-reset] port=18993 curl=000 [turn5] exit=0 reply=STARTED [after-start-3] port=18991 curl=200 [after-start-3] port=18992 curl=200 [after-start-3] port=18993 curl=200 [after-gateway-stop] port=18991 curl=000 [after-gateway-stop] port=18992 curl=000 [after-gateway-stop] port=18993 curl=000 helpers_left=0 (the script counted 1, an unrelated test-runner process whose arguments name the helper files; a direct check found 0 relay/anchor processes) ``` </details> ## Test results (head) - `ocx test` on 139 files (supervisor, relay, anchor, exec, process tool, exec follow-up, cron exit watchers, session reset/delete, agent delete, chat session, subagent reset, TUI, desktop): 10 shards pass, 1456 tests passed, 0 failed (previous head). - After the CI fixes: exec, worker runtime, supervisor, relay, anchor, adapters, TUI shell, exec follow-up, process scope and subagent reset suites: 5 shards, 416 passed, 0 failed (macOS); the Linux container run in row 5. - Not in that run: `bash-tools.exec.approval-id.test.ts`. One of its checks fails in this worktree only because the worktree path contains the word "process"; it passes on CI paths. - `pnpm tsgo:core` clean; oxlint and oxfmt clean on the changed files. ## Limits - The `--dts` build and packed smoke did not run. `ocx build --dts` fails locally with `PLUGIN SDK DTS TOO LARGE` on clean main commits too; CI's `build-artifacts` job on main measured the declarations under the limit. The Linux proof uses the dist-nodts build laid over a published install. - On macOS, a process that leaves the command's process group (`setsid`, double fork) re-parents to launchd before the census unless a tracked parent still holds it. OpenClaw does not see it, so it is not adopted, not capped, and it keeps running after session reset and Gateway stop, as in every other harness compared below. On Linux the subreaper sees it. - A process that a tracked survivor forks after the hand-off is not tracked separately; stopping the survivor does not stop it. - Test lines exceed the 400-line allowance by 150; `config/test-lines-allocations/199079-exec-detached-process-ownership.txt` records them. Each added test is a real-process regression for one review finding. - Adopted services count as live background sessions, so, like `background: true` services, they block a safe Gateway restart until they exit or are stopped. ## How this compares | Harness | Process left by a completed command | Bound while running | Session reset / new | Host stop | One-shot CLI exit | |---|---|---|---|---|---| | Codex | Survives; only timeout or Ctrl-C kills the group | No | Session shutdown terminates all its unified-exec processes | Killed with the session | Survives | | Pi | Survives; only in-flight commands are tracked | No | Kills in-flight commands only | Kills in-flight commands only | Survives | | OMP | Survives; the shell is kept while background jobs run, reaped every 5 s | 15 running background jobs; over the limit, new jobs fail | `newSession()` cancels the session's own async jobs | Kept shells and their jobs die with the harness | Killed with the harness | | Hermes | Tracked in a session-scoped process registry | 64 tracked records; prunes only finished ones | Agent close and session end kill the session's processes, except explicitly persisted ones | — | — | | OpenClaw before | Killed at the end of the call (Linux: daemons too) | — | Killed earlier, at the end of the call | Killed | Killed | | OpenClaw after | Survives as a tracked background session, reaped every 5 s, named in the result and `process list` | 16 per session, 64 per Gateway; over the limit, new services are refused or stopped, old ones never evicted | Stops in-flight work, `background: true` services and survivors; durable processes stay | Stopped | Handed off and printed (as Codex and Pi) | In every harness, a process that daemonized itself and was never tracked keeps running. Sources: Codex `codex-rs/core/src/exec.rs`, `spawn.rs` and `core/src/session/handlers.rs` (`terminate_all_processes`); Pi `packages/coding-agent/src/core/tools/bash.ts`; OMP `packages/coding-agent/src/exec/bash-executor.ts` (`RETAIN_REAP_INTERVAL_MS`), `src/async/job-manager.ts` (`DEFAULT_MAX_RUNNING_JOBS = 15`) and `newSession()` (`#cancelOwnAsyncJobs`); Hermes `tools/process_registry.py` (`MAX_PROCESSES = 64`, `kill_all`). ## Follow-ups (PR 2 and later) - A strict process scope for cron and heartbeat runs, with a tool notice that everything ends with the run. - A durable PID ledger so services are still owned after a Gateway crash. - One ownership policy: remove the service-marker and platform predicates and the PTY rejection. - cgroup and RSS memory budgets for adopted services. - File-backed stdio for all exec, which removes the drain anchor. Co-authored-by: Ayaan Zaidi <[email protected]>
Related: #142965, #146620, #145092
What Problem This Solves
Completed host exec commands can leave shell-backgrounded children and their relay/anchor processes alive until Gateway restart, exhausting process limits during isolated heartbeats.
User Impact
Completed host exec commands release their retained process group before reporting completion. Long-running commands started with
background: trueremain available throughprocessuntil their root command finishes. Sandbox runtime lifetimes remain with their backend. No configuration or migration is required.Why This Change Was Made
Exec finalization now cancels and joins the existing supervisor group. The anchor already owns descendant cleanup; the missing step was its caller releasing that ownership after the command result settled.
Stress testing also exposed a separate interpretation error: the final disappearance probe treated
EPERMas immediate cleanup failure. It now treats that result as a present group within the original deadline; onlyESRCHproves absence. Persistent groups still produce the existing bounded cleanup-uncertainty outcome. Doctor already uses this interpretation, while Windows uses exact process/Job handles.Thanks to @php737, @terrencehinds99, @flowzito, and @michaeljgood71 for the reports and investigation.
Evidence
EPERMexpectations failed against old production; all 63 owner and Doctor-sibling settlement tests pass after the repair.The real macOS Gateway proof used isolated state and local synthetic model/MCP endpoints. Both inventories (2 and 31 configured stdio servers) produced the same before/after exec-pair counts:
Heartbeat configuration was hourly; the proof used three manual wakes per inventory. MCP server/relay/anchor PIDs returned to zero on session reset and isolated-run completion. All owned Gateways stopped with zero remaining recorded processes.
pnpm check:changedpassed completely on the reviewed implementation at1038f66aca6b8182d5389c437e66347076974550. Codex branch review (--mode branch --base origin/main --max-priority P1) was scoped-clean, and that tree matched the successful Linux stress proof. The rebase below preserves this patch and its review of record.LOC: production +13/−8; tests +119/−25; docs +9. The only baseline change removes the two assertions eliminated with redundant
undefinedinitializers.PR CI status
Rebased without patch changes onto main
9ee5c26f4080b4f63243897c904243802efbc3bf, which contains #150655 at5764fa493344c0a041e0bc8319c07585d649b18e. That main fix addresses the unrelated Doctor transport, Labeler target-list, and planner lint failures. The old red run was retained and was not rerun.git range-diffreports1038f66aca6 = c18e6da9a17; only the base moved.pnpm install --frozen-lockfilepassed. Current head:c18e6da9a17bc2ddd7cf7cd2fe8a95977336a855.A new
pull_requestrun is attached to this exact head: CI 35208095745. The native watcher completed GREEN on this head:github_rollup=SUCCESS,pending=0, exit 0. This PR remains ready for review and unmerged.Maintainer review decision
The ClawSweeper review of
c18e6da9a17bc2ddd7cf7cd2fe8a95977336a855reports no correctness findings and no rank-up moves. Its remaining compatibility concern is accepted: shell-backgrounded descendants intentionally stop when the root command completes. Keep long-running workloads in the existing supervisedbackground: truemode with the long-running command as the root; sandbox lifetimes remain backend-owned. Retaining the leaked groups would preserve the process-exhaustion defect. No code change is needed for this review concern.