Skip to content

fix: extension api.exec leaves child processes after timeout - #98340

Merged
vincentkoc merged 1 commit into
openclaw:mainfrom
ooiuuii:fix/session-exec-kill-tree
Jul 1, 2026
Merged

vincentkoc merged 1 commit into
openclaw:mainfrom
ooiuuii:fix/session-exec-kill-tree

Conversation

@ooiuuii

@ooiuuii ooiuuii commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #98335

What Problem This Solves

Fixes an issue where session extensions using api.exec() could leave descendant processes running when a command timed out, was aborted, or exceeded the default output cap. The API reported the command as killed, but the old termination path only signaled the direct child process.

Why This Change Was Made

api.exec() now uses the shared OpenClaw process-tree termination helper for timeout, abort, and output-limit cleanup, with the same detached Unix process-group setup used by the built-in shell execution paths. The direct proc.kill() fallback is retained only for the rare case where Node does not expose a child PID.

The runtime entry point is ExtensionAPI.exec in src/agents/sessions/extensions/loader.ts, which delegates extension commands to execCommand(). This PR keeps the api.exec() result shape unchanged and only changes how cleanup is performed after the command must be stopped.

User Impact

Extension authors and operators can expect api.exec() cleanup to stop wrapper commands and their descendants instead of leaving background child processes behind after cancellation or timeout. The command output/result contract is unchanged.

Evidence

  • Source-level before evidence on current main: src/agents/sessions/exec.ts previously called proc.kill(SIGTERM) and later proc.kill(SIGKILL) in killProcess(), so timeout/abort/output-limit cleanup targeted only the direct child process.
  • After-patch source proof: execCommand() now spawns with detached: process.platform !== "win32" and calls killProcessTree(proc.pid, { detached: process.platform !== "win32", graceMs: 5000 }) when cleanup is required.
  • Live Windows proof through the extension API boundary: I loaded an inline extension with loadExtensionFromFactory(), registered a command whose handler calls api.exec(process.execPath, ["-e", parentScript], { timeout: 500 }), and made that parent spawn a 30-second descendant Node process before idling. After the timeout, the descendant PID was no longer alive:
platform=win32
entrypoint=ExtensionAPI.exec via registered command
timeout_ms=500
elapsed_ms=5952
result.killed=true
result.code=1
stdout="descendant_pid=3128"
descendant_pid=3128
descendant_alive_after_timeout=false
  • Focused tests: node scripts/run-vitest.mjs src/agents/sessions/exec.test.ts passed, 1 file / 5 tests.
  • Lint/static checks: .\node_modules\.bin\oxlint.cmd src/agents/sessions/exec.ts src/agents/sessions/exec.test.ts passed.
  • Whitespace check: git diff --check passed.
  • Autoreview: python .agents\skills\autoreview\scripts\autoreview --mode local returned autoreview clean: no accepted/actionable findings reported.
  • Proof limitation: I did not run a full packaged third-party extension install. The live proof exercises the same ExtensionAPI.exec method that loaded extensions receive and the same backend cleanup path this PR changes.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Jul 1, 2026
@clawsweeper

clawsweeper Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 30, 2026, 10:56 PM ET / 02:56 UTC.

Summary
The PR changes session extension api.exec() cleanup to spawn with process-tree-safe options and call the shared tree killer on timeout, abort, or output-limit cleanup.

PR surface: Source +10, Tests +30. Total +40 across 2 files.

Reproducibility: yes. at source level: current main and v2026.6.11 still call direct proc.kill() in execCommand(), while the sibling bash path uses process-tree termination. I did not run a live current-main orphan-process repro in this read-only review.

Review metrics: none identified.

Stored data model
Persistent data-model change detected: serialized state: src/agents/sessions/exec.test.ts, serialized state: src/agents/sessions/exec.ts, unknown-data-model-change: src/agents/sessions/exec.test.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster
Relationship: canonical
Canonical: #98340
Summary: This PR is a viable canonical implementation candidate for the linked api.exec descendant-process cleanup bug, while sibling PRs are competing or superseded attempts for the same root cause.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🐚 platinum hermit
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • none.

Risk before merge

  • [P1] Multiple open PRs target the same linked issue, so maintainers should choose one canonical branch before merging.
  • [P2] This PR has direct ExtensionAPI.exec proof, while the sibling branch has broader focused tests for abort and no-PID fallback; maintainers may want to preserve both signals in the final landing path.

Maintainer options:

  1. Decide the mitigation before merge
    Pick one canonical implementation for the linked issue, ideally preserving this PR's direct ExtensionAPI.exec proof and the sibling branch's broader focused test coverage.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • [P2] Maintainers need to choose among duplicate open fix branches; this PR has no narrow code defect for automated repair.

Security
Cleared: No concrete security or supply-chain regression was found; the diff stays within local process cleanup and focused tests.

Review details

Best possible solution:

Pick one canonical implementation for the linked issue, ideally preserving this PR's direct ExtensionAPI.exec proof and the sibling branch's broader focused test coverage.

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

Yes at source level: current main and v2026.6.11 still call direct proc.kill() in execCommand(), while the sibling bash path uses process-tree termination. I did not run a live current-main orphan-process repro in this read-only review.

Is this the best way to solve the issue?

Yes for the implementation shape: reusing killProcessTree() and a Unix process group is the narrow owner-boundary fix without changing the public result shape. The unresolved question is branch selection among duplicate PRs, not whether this repair belongs in the extension exec backend.

AGENTS.md: found and applied where relevant.

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

Label changes

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR body and follow-up comments include live ExtensionAPI.exec proof showing a timed-out command's descendant process was no longer alive after cleanup.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body and follow-up comments include live ExtensionAPI.exec proof showing a timed-out command's descendant process was no longer alive after cleanup.
  • remove rating: 🧂 unranked krab: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: 📣 needs proof: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P2: This is a focused bugfix PR for a shipped extension exec cleanup issue with limited blast radius.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body and follow-up comments include live ExtensionAPI.exec proof showing a timed-out command's descendant process was no longer alive after cleanup.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body and follow-up comments include live ExtensionAPI.exec proof showing a timed-out command's descendant process was no longer alive after cleanup.
Evidence reviewed

PR surface:

Source +10, Tests +30. Total +40 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 17 7 +10
Tests 1 42 12 +30
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 59 19 +40

What I checked:

  • Root policy read and applied: Root AGENTS.md was read fully; its whole-path review and plugin/session runtime guidance applies because this PR changes a session extension execution boundary. (AGENTS.md:17, 4ac5cf863673)
  • Scoped agents policy read: The scoped agents guide was read fully; it affects test-context review for agent/session code but does not add a blocking exception. (src/agents/AGENTS.md:1, 4ac5cf863673)
  • Current main still has the bug: Current execCommand() spawns without a Unix process group and kills only the direct child with SIGTERM then SIGKILL, so descendants are not intentionally covered. (src/agents/sessions/exec.ts:82, 4ac5cf863673)
  • Extension API reaches the patched backend: ExtensionAPI.exec() delegates directly to execCommand(command, args, options?.cwd ?? cwd, options), so the changed function owns the reported extension API path. (src/agents/sessions/extensions/loader.ts:312, 4ac5cf863673)
  • Sibling shell path already uses tree cleanup: The built-in bash execution path spawns detached on Unix and calls killProcessTree(child.pid) for timeout and abort cleanup, matching the invariant this PR extends to extension exec. (src/agents/sessions/tools/bash.ts:64, 4ac5cf863673)
  • Shared helper contract supports the fix: The lower-level helper uses Windows taskkill /T and Unix process-group signaling with a grace-period SIGKILL escalation unless disabled. (packages/agent-core/src/harness/env/kill-tree.ts:23, 4ac5cf863673)

Likely related people:

  • Vincent Koc: Blame and log history point the current session exec backend, extension exec delegation, sibling bash tree-kill path, and kill-tree helper to commit 3e2646a. (role: introduced behavior and adjacent owner; confidence: high; commits: 3e2646a7864c; files: src/agents/sessions/exec.ts, src/agents/sessions/extensions/loader.ts, src/agents/sessions/tools/bash.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. labels Jul 1, 2026
@Alex-Alaniz

Copy link
Copy Markdown
Contributor

I ran the missing real behavior proof against this PR head (54f1410d992812a11cddc13a85e21cb3e3ee894c).

Setup: local checkout of the PR head, using loadExtensionFromFactory() to invoke the actual ExtensionAPI.exec() surface. The extension command ran a wrapper process that spawned a long-lived descendant; the wrapper exits on SIGTERM, while the descendant installs a SIGTERM handler and keeps running. The api.exec() call used timeout: 100, then I waited for the process-tree grace kill and checked the descendant PID.

Observed result:

{"prHead":"54f1410d992812a11cddc13a85e21cb3e3ee894c","surface":"ExtensionAPI.exec","killed":true,"code":0,"grandchildAlive":false}

Focused test also passed on the same PR checkout:

node scripts/run-vitest.mjs src/agents/sessions/exec.test.ts
[test] passed 1 Vitest shard in 3.67s

No secrets or external services were involved. This directly exercises the api.exec() timeout path with a descendant that survives the initial SIGTERM, and verifies it is gone after cleanup.

@Alex-Alaniz

Copy link
Copy Markdown
Contributor

@clawsweeper re-review

@Alex-Alaniz

Copy link
Copy Markdown
Contributor

One follow-up: the PR body still says no live descendant-process proof was run. The proof is now available in #98340 (comment), but ClawSweeper may keep the needs proof verdict until the Evidence section is updated or a maintainer triggers a fresh review.

Suggested body update: add the ExtensionAPI.exec proof output from that comment under Evidence, especially:

{"prHead":"54f1410d992812a11cddc13a85e21cb3e3ee894c","surface":"ExtensionAPI.exec","killed":true,"code":0,"grandchildAlive":false}

That should make the proof state match the current PR reality.

@ooiuuii

ooiuuii commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Added live Windows proof through the ExtensionAPI.exec boundary: a registered extension command spawned a long-lived descendant, api.exec timed out, and the descendant PID was no longer alive after cleanup. @clawsweeper re-review

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 1, 2026
@vincentkoc
vincentkoc merged commit 53557f9 into openclaw:main Jul 1, 2026
162 of 174 checks passed
chenyangjun-xy pushed a commit to chenyangjun-xy/openclaw that referenced this pull request Jul 1, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extension api.exec can leave descendant processes running after timeout

3 participants