Skip to content

fix(codex): stop sandbox processes surviving termination - #125908

Merged
steipete merged 1 commit into
mainfrom
steipete/fix-sandbox-terminate-reap
Aug 18, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/fix-sandbox-terminate-reap

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where terminating a Codex sandbox command could acknowledge only a single SIGTERM while a TERM-resistant local wrapper, remote sandbox descendant, or backend lease remained alive.

Before this change, process termination, socket loss, and exec-server release could return before descendants were force-killed, reaped, and finalized. After this change, those paths share one child owner that completes remote descendant cleanup, local TERM-to-KILL escalation, child reaping, and backend finalization before termination is acknowledged.

Why This Change Was Made

The sandbox exec server owns each launched process tree and backend lease. A shared SandboxChildOwner now carries that ownership through close and finalization for both process RPCs and streaming HTTP helpers. Socket/server cleanup uses the same owner, eliminating the previous split kill/finalize paths and failing closed when a process survives SIGKILL.

Production LOC: +300/-143 (net +157) | Tests: +327/-8

The production growth adds the missing cross-host lifecycle capability: unique remote ownership markers, bounded TERM/KILL/reap verification, one finalization path, and cleanup ownership at connection/server shutdown. Existing duplicated direct-child kill/finalize branches were removed.

User Impact

Codex sandbox termination no longer reports success while resistant descendants or leases remain. Normal cooperative exits still finish without unnecessary force killing; unreaped processes produce an actionable failure instead of a false success.

Evidence

Rebased onto 302d05b988780e70c471a726ff7f8c90e53d25f8. The only rebase conflict was mechanical: current main's isPidAlive export and this branch's killProcessTree export were both retained in process-runtime.

  • node scripts/run-vitest.mjs extensions/codex/src/app-server/sandbox-exec-server.fs-streaming.test.ts extensions/codex/src/app-server/sandbox-exec-server.fs.test.ts extensions/codex/src/app-server/sandbox-exec-server.http.test.ts extensions/codex/src/app-server/sandbox-exec-server.json-rpc.test.ts extensions/codex/src/app-server/sandbox-exec-server.lifecycle.test.ts extensions/codex/src/app-server/sandbox-exec-server.test.ts — all 78 tests in the current six-file sandbox exec-server inventory passed.
  • pnpm build — full build passed, including all 146 public Plugin SDK subpaths and the SDK surface budget.
  • node scripts/check-changed.mjs — core/extension typechecks, type-aware lint, SDK export/surface gates, dead-code, database-first, architecture, and import-cycle checks passed.
  • .agents/skills/autoreview/scripts/autoreview --mode branch --base origin/main --stream-engine-output — clean; no accepted/actionable findings.
  • Frozen environment proof retained from the validated wave: a real Docker run caught the pre-fix in-container survivor and verified the shared owner removes the remote descendant before acknowledging termination.

Direct Codex contract inspection

Personally inspected sibling openai/codex at 02e9bfaac7577a3ab77a40de6b0c3cc37b934576:

This change strengthens OpenClaw's implementation behind the existing Codex protocol; it does not change protocol fields or public RPC names.

@clawsweeper

clawsweeper Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@steipete
steipete marked this pull request as ready for review August 18, 2026 16:59
@openclaw-barnacle openclaw-barnacle Bot added size: L maintainer Maintainer-authored PR labels Aug 18, 2026
@steipete

steipete commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Landing blockers are resolved on exact head 4ec20e9420833e52983eb1b84cd8cfe37747fca0.

Failure A — QA PGID reuse:

Failure B — CLI child deadlock:

Rebased verification:

  • Exact-head CI: run 32166345043 is green with 0 pending checks.
  • Sandbox exec-server inventory: 78/78 passed under Node 24.19.0.
  • node scripts/check-changed.mjs: passed.
  • pnpm build: passed, including all 146 public Plugin SDK subpaths.
  • Fresh Codex autoreview: clean; no accepted/actionable findings.
  • Direct sibling Codex source contract inspection was repeated at 02e9bfaac7577a3ab77a40de6b0c3cc37b934576 for protocol termination, client cleanup awaiting, remote-process awaiting, unified-exec confirmed termination, and TERM/KILL/reap shutdown.

ClawSweeper's latest exact-head comment is still review started; it has published no findings or Rank-up moves: list to apply or skip. Proceeding through the native review/prepare/merge gates.

@steipete steipete self-assigned this Aug 18, 2026
@clawsweeper

clawsweeper Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Codex review: found issues before merge. Reviewed August 18, 2026, 1:06 PM ET / 17:06 UTC.

ClawSweeper review

What this changes

The PR centralizes Codex sandbox child-process termination, reaping, and backend finalization across process RPCs, streaming HTTP, socket close, and server release.

Regression provenance

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 6 items remain

Keep open: the new remote cleanup path breaks supported Windows MXC sandboxes by running POSIX-only commands, so termination now reports failure there. Likely related people: Dallin Romney (retained lifecycle history) and steipete (recent process-runtime work).

Priority: P1
Reviewed head: f993b4f50cbbba3b94283275bb577681ad47200c
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The shared-owner design and POSIX lifecycle tests are useful, but a supported backend regression blocks merge.
Proof confidence 🌊 off-meta tidepool Not applicable: This is maintainer-authored work, so the external-contributor proof gate does not apply; the claimed Docker evidence also does not cover the Windows MXC regression.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This is maintainer-authored work, so the external-contributor proof gate does not apply; the claimed Docker evidence also does not cover the Windows MXC regression.
Evidence reviewed 6 items Current-main failure path: Current main returns process termination after aborting and sending only SIGTERM; it does not await reaping or backend finalization.
Unconditional POSIX remote cleanup: The proposed owner always invokes a /proc, grep, kill, and sleep shell script through the generic backend shell API before completing termination.
MXC backend contract conflict: The generic backend contract provides runShellCommand, while MXC registers only on Windows and constructs cmd.exe commands; it cannot execute the proposed POSIX script.
Findings 1 actionable finding [P1] Gate remote cleanup by backend shell contract
Security None None.

Live Verification

Command: node scripts/run-vitest.mjs extensions/codex/src/app-server/sandbox-exec-server.test.ts

Result: PASS (completed)

node scripts/run-vitest.mjs extensions/codex/src/app-server/sandbox-exec-server.test.ts
runner@runnervmzvulz:/tmp/clawsweeper-live-proof-125908-NwTi9S/target$ node scripts/run-vitest.mjs extensions/codex/src/app-server/sandbox-exec-server.test.ts
node scripts/run-vitest.mjs extensions/codex/src/app-server/sandbox-exec-server.test.ts
[test] starting test/vitest/vitest.extension-codex.config.ts

 RUN  v4.1.10 /tmp/clawsweeper-live-proof-125908-NwTi9S/target

 ✓  extension-codex  extensions/codex/src/app-server/sandbox-exec-server.test.ts (29 tests) 3795ms
     ✓ reaps TERM-resistant process groups on socket loss and turn environment release  2315ms

 Test Files  1 passed (1)
      Tests  29 passed (29)
   Start at  17:07:30
   Duration  16.91s (transform 10.65s, setup 790ms, import 12.06s, tests 3.79s, environment 0ms)

[test] passed 1 Vitest shard in 21.32s
runner@runnervmzvulz:/tmp/clawsweeper-live-proof-125908-NwTi9S/target$ node scripts/run-vitest.mjs extensions/codex/src/app-server/sandbox-exec-server.test.ts


































Assertions:

  • PASS expect_output: Test Files

How this fits together

The Codex extension starts sandboxed commands through a local exec server. Termination must clean up the local launcher, remote sandbox descendants, and backend lease before the client receives completion.

flowchart LR
  A[Codex client] --> B[Sandbox exec server]
  B --> C[Child lifecycle owner]
  C --> D[Local process tree]
  C --> E[Sandbox backend]
  E --> F[Remote descendant cleanup]
  D --> G[Reap and finalize]
  F --> G
  G --> H[Termination response]
Loading

Decision needed

Question Recommendation
Should remote descendant reaping become an explicit sandbox-backend capability, with MXC implementing its Windows equivalent, before this lifecycle owner is merged? Make cleanup backend-owned: Add a backend-specific remote-termination strategy and cover MXC plus POSIX behavior before merging.

Why: The generic backend shell API has no POSIX-shell guarantee, and adding a public SDK export also chooses a lasting extension contract.

Before merge

  • Gate remote cleanup by backend shell contract (P1) - This unconditionally runs a POSIX /proc and kill script through the generic backend shell API. MXC is Windows-only and executes shell commands through cmd.exe, so it returns a nonzero result; terminate() then throws after local cleanup, making process termination and shutdown fail for MXC. Move remote reaping behind a backend-specific strategy (or gate it to a backend that guarantees this shell contract) and add MXC coverage.
  • Resolve merge risk (P1) - On Windows MXC, the new POSIX remote-cleanup script causes process termination and shutdown to fail even after local cleanup.
  • Resolve merge risk (P1) - Exporting killProcessTree from the public Plugin SDK creates a permanent third-party contract that needs explicit owner approval and aligned documentation/contract treatment.
  • Resolve merge risk (P1) - This review cannot provide the repository-required direct Codex protocol approval until the OpenAI Codex source is available locally.
  • Complete next step (P2) - Resolve the backend-cleanup and public SDK ownership decisions before a repair or merge proceeds.

Findings

  • [P1] Gate remote cleanup by backend shell contract — extensions/codex/src/app-server/sandbox-exec-server/sandbox-child.ts:122-126
Agent review details

Security

None.

PR surface

Source +157, Tests +319. Total +476 across 9 files.

View PR surface stats
Area Files Added Removed Net
Source 7 300 143 +157
Tests 2 327 8 +319
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 9 627 151 +476

Review metrics

Metric Value Why it matters
Production versus test LOC production +300/-143 (net +157); tests +327/-8 The substantial lifecycle-owner refactor is justified only if it preserves all supported sandbox backends, including MXC.

Merge-risk options

Maintainer options:

  1. Implement backend-specific reaping (recommended)
    Replace the unconditional POSIX shell call with a backend-owned cleanup capability and add a Windows MXC regression test.
  2. Pause for SDK-contract direction
    Defer landing until maintainers confirm whether the new public process-runtime export is the intended supported plugin contract.

Technical review

Best possible solution:

Make remote descendant cleanup a backend-owned termination capability, implement the MXC equivalent, and prove both MXC/Windows and POSIX sandbox cleanup before merge.

Do we have a high-confidence way to reproduce the issue?

Yes, from source: run a Codex sandbox process on Windows with the MXC backend and terminate it; the generic shell invocation receives POSIX-only syntax under cmd.exe and returns an error.

Is this the best way to solve the issue?

No. A unified child owner is the right boundary, but unconditional POSIX remote cleanup is not a cross-backend solution; cleanup belongs behind a backend capability.

Full review comments:

  • [P1] Gate remote cleanup by backend shell contract — extensions/codex/src/app-server/sandbox-exec-server/sandbox-child.ts:122-126
    This unconditionally runs a POSIX /proc and kill script through the generic backend shell API. MXC is Windows-only and executes shell commands through cmd.exe, so it returns a nonzero result; terminate() then throws after local cleanup, making process termination and shutdown fail for MXC. Move remote reaping behind a backend-specific strategy (or gate it to a backend that guarantees this shell contract) and add MXC coverage.
    Confidence: 0.97

Overall correctness: patch is incorrect
Overall confidence: 0.97

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against b9d0e13b5d08.

Labels

Label changes:

  • add P1: The patch makes a supported Windows sandbox's process-termination workflow fail.
  • add merge-risk: 🚨 compatibility: The PR adds a new export to a public Plugin SDK runtime subpath and therefore establishes a third-party contract.
  • add merge-risk: 🚨 availability: MXC process termination and shutdown can fail after merge because the added remote cleanup is not executable there.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🌊 off-meta tidepool and patch quality is 🦐 gold shrimp.

Label justifications:

  • P1: The patch makes a supported Windows sandbox's process-termination workflow fail.
  • merge-risk: 🚨 availability: MXC process termination and shutdown can fail after merge because the added remote cleanup is not executable there.
  • merge-risk: 🚨 compatibility: The PR adds a new export to a public Plugin SDK runtime subpath and therefore establishes a third-party contract.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🌊 off-meta tidepool and patch quality is 🦐 gold shrimp.

Evidence

What I checked:

Likely related people:

  • Dallin Romney: Retained blame covers the current-main termination path and shared process-tree helper, though the grafted history prevents stronger attribution. (role: retained lifecycle-history contributor; confidence: medium; commits: e1d4eac8f429; files: extensions/codex/src/app-server/sandbox-exec-server/processes.ts, packages/agent-core/src/harness/env/kill-tree.ts)
  • steipete: Recent main history includes process-runtime work, and this PR expands that same public SDK subpath for the lifecycle fix. (role: recent process-runtime contributor; confidence: medium; commits: b9fb56566b90, f993b4f50cbb; files: src/plugin-sdk/process-runtime.ts, extensions/codex/src/app-server/sandbox-exec-server/sandbox-child.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Route remote cleanup through a backend-owned strategy and add Windows MXC termination coverage.
  • Confirm the intended public Plugin SDK contract for the new process-runtime export.
  • Re-run the direct Codex contract check in an environment containing the required sibling source.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete
steipete force-pushed the steipete/fix-sandbox-terminate-reap branch from f993b4f to 4ec20e9 Compare August 18, 2026 17:34
@steipete
steipete merged commit fd8326c into main Aug 18, 2026
225 checks passed
@steipete
steipete deleted the steipete/fix-sandbox-terminate-reap branch August 18, 2026 17:48
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant