Skip to content

fix(process): handle async taskkill spawn errors to prevent Windows process leaks - #101389

Closed
wm0018 wants to merge 2 commits into
openclaw:mainfrom
wm0018:fix/windows-taskkill-async-error-leak
Closed

wm0018 wants to merge 2 commits into
openclaw:mainfrom
wm0018:fix/windows-taskkill-async-error-leak

Conversation

@wm0018

@wm0018 wm0018 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes #101381

In src/process/exec.ts, the Windows taskkill.exe process tree cleanup uses spawn() inside try/catch blocks, but Node.js spawn() errors are emitted asynchronously — not thrown synchronously. If taskkill.exe fails to start (missing binary, permission denied, antivirus interception), the try/catch never fires, child.kill("SIGKILL") is never called, and the orphaned child process tree permanently leaks system resources.

Why This Change Was Made

Three spawn(taskkillPath, ...) calls in the killChild function had no error event handlers. The fix captures the returned ChildProcess and attaches .on("error", () => child.kill("SIGKILL")) so any async spawn failure falls back to direct child termination.

Fixed locations

Line Context Fix
530 Graceful taskkill /T gracefulKiller.on("error", () => child.kill("SIGKILL"))
550 Force taskkill /T /F (post-timeout) forceKiller.on("error", () => child.kill("SIGKILL"))
571 Direct taskkill /T /F (no-tree-kill path) directKiller.on("error", () => child.kill("SIGKILL"))

All three paths now defend against both synchronous (catch clause) and asynchronous (error event) spawn failures.

User Impact

  • Before: underprivileged, corrupted, or blocked taskkill.exe → orphaned Windows process trees accumulate indefinitely
  • After: async spawn failure → child.kill("SIGKILL") runs, process tree is terminated

Evidence

  • pnpm tsgo — pass
  • node scripts/run-vitest.mjs src/process/exec.test.ts src/process/exec.windows.test.ts src/process/kill-tree.test.ts — 49 tests passed

@clawsweeper

clawsweeper Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 7, 2026, 1:47 AM ET / 05:47 UTC.

Summary
The branch adds error handlers to the three Windows taskkill.exe spawns in runCommandWithTimeout so async taskkill start failures fall back to child.kill("SIGKILL").

PR surface: Source +11. Total +11 across 1 file.

Reproducibility: yes. for source-level reproduction: current main and the latest release show taskkill child spawns without error handlers, and Node documents failed spawns as error events rather than successful spawn events. No live Windows end-to-end run was performed in this read-only review. (nodejs.org)

Review metrics: none identified.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #101381
Summary: This PR is the current candidate fix for the open source-reproducible Windows taskkill async spawn error issue.

Members:

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

Merge readiness
Overall: 🦪 silver shellfish
Proof: 🧂 unranked krab
Patch quality: 🐚 platinum hermit
Result: blocked until real behavior proof from a real setup is added.

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

Rank-up moves:

  • [P2] Add real Windows proof for the async taskkill spawn-error fallback, with private details redacted.
  • [P1] Add focused regression coverage that makes the mocked taskkill child emit error and asserts child.kill("SIGKILL") is called.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body lists typecheck and test commands only; before merge, the contributor should add real Windows behavior proof such as a redacted terminal run, logs, or a reproducible validation transcript showing the async taskkill failure fallback. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] The PR body reports only typecheck and test commands; it does not include real Windows behavior proof showing the async taskkill failure path after the fix.
  • [P2] The patch adds no focused regression test that emits error from the mocked taskkill child, so future refactors could drop the fallback without a direct test failure.

Maintainer options:

  1. Decide the mitigation before merge
    Land a narrow process-layer fix that preserves the trusted System32 taskkill lookup, handles async taskkill child errors, and includes focused fallback coverage plus real Windows proof.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • [P1] This PR should wait for contributor-supplied real behavior proof; there is no narrow automated repair lane that can prove the contributor's Windows setup for them.

Security
Cleared: The diff keeps the same trusted System32 taskkill executable path, adds only child-process error handling, and does not change dependencies, CI, secrets, permissions, or command construction.

Review details

Best possible solution:

Land a narrow process-layer fix that preserves the trusted System32 taskkill lookup, handles async taskkill child errors, and includes focused fallback coverage plus real Windows proof.

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

Yes for source-level reproduction: current main and the latest release show taskkill child spawns without error handlers, and Node documents failed spawns as error events rather than successful spawn events. No live Windows end-to-end run was performed in this read-only review. (nodejs.org)

Is this the best way to solve the issue?

Yes for the code shape: observing error on the taskkill children is the narrowest owner-boundary fix in runCommandWithTimeout. The merge path still needs real Windows proof and ideally a small regression test for the mocked taskkill error event.

AGENTS.md: found and applied where relevant.

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

Label changes

Label changes:

  • add P2: This PR targets a credible Windows process-cleanup reliability bug with limited blast radius and no evidence of a current emergency.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body lists typecheck and test commands only; before merge, the contributor should add real Windows behavior proof such as a redacted terminal run, logs, or a reproducible validation transcript showing the async taskkill failure fallback. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Label justifications:

  • P2: This PR targets a credible Windows process-cleanup reliability bug with limited blast radius and no evidence of a current emergency.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body lists typecheck and test commands only; before merge, the contributor should add real Windows behavior proof such as a redacted terminal run, logs, or a reproducible validation transcript showing the async taskkill failure fallback. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +11. Total +11 across 1 file.

View PR surface stats
Area Files Added Removed Net
Source 1 14 3 +11
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 1 14 3 +11

What I checked:

  • PR diff handles all local taskkill spawns: The patch captures the graceful, delayed force, and direct taskkill.exe child processes and attaches error handlers that call the existing direct child-kill fallback. (src/process/exec.ts:530, 5cd097227ef8)
  • Current main still has the reported gap: Current main starts the same Windows taskkill children without retaining the returned ChildProcess or listening for error, so an async taskkill spawn failure bypasses the synchronous catch fallback. (src/process/exec.ts:530, 094c0d421faf)
  • Shipped release still has the old shape: The latest shipped tag v2026.6.11 points at e085fa1a3ffd32d0ea6917e1e6fb4ecbffbb77d2 and still has taskkill spawns without error handlers. (src/process/exec.ts:444, e085fa1a3ffd)
  • Node child_process contract supports the bug: Node documents that error is emitted when a child process cannot be spawned, and that failed spawns emit error instead of spawn; a local Node 24 check confirmed a missing executable returned a ChildProcess and emitted ENOENT without a synchronous throw. (nodejs.org)
  • Adjacent test does not cover this fallback: The existing Windows timeout test asserts that taskkill is spawned but does not simulate the spawned taskkill child emitting error or assert the direct fallback kill. (src/process/exec.windows.test.ts:485, 094c0d421faf)
  • History points to recent Windows taskkill ownership: a192b2ea52b3166a7d190bf5f60f3feb030306bb changed these core taskkill spawns from PATH lookup to the trusted System32 resolver, which is the same code path this PR preserves. (src/process/exec.ts:528, a192b2ea52b3)

Likely related people:

  • vincentkoc: Recent merged work changed the same runCommandWithTimeout Windows taskkill calls to use the trusted System32 resolver and the current main commit is also by this author. (role: recent area contributor; confidence: high; commits: a192b2ea52b3, 2a140e6e6ae8, 094c0d421faf; files: src/process/exec.ts, src/process/exec.windows.test.ts, docs/releases/2026.6.11.md)
  • steipete: Earlier history shows Peter Steinberger authored exec spawn fallback hardening and committed the process-tree termination change that shaped the adjacent kill-tree behavior. (role: earlier exec fallback contributor and merger; confidence: medium; commits: c6cdbb630c92, f4fa53de3f03, 20957efa4674; files: src/process/exec.ts, src/process/kill-tree.ts)
  • sauerdaniel: The shared graceful process-tree termination helper that defines related Windows taskkill semantics was introduced by this author. (role: adjacent feature contributor; confidence: medium; commits: 20957efa4674; files: src/process/kill-tree.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.

…rocess leaks

Node.js spawn() emits 'error' asynchronously when the target executable
fails to start — try/catch cannot see it. Attach error handlers to all
three Windows taskkill spawns in killChild so missing or blocked
taskkill.exe falls back to child.kill("SIGKILL") instead of silently
leaking orphaned process trees.

Fixes openclaw#101381
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. 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 7, 2026
…rocess leaks

Node.js spawn() emits 'error' asynchronously when the target executable fails
to start — try/catch cannot see it. Attach once('error') handlers to all taskkill
spawns so missing, blocked, or corrupted taskkill.exe falls back to direct
child.kill('SIGKILL') instead of silently leaking orphaned process trees.

- Extract spawnTaskkillOrFallback helper to reduce duplication across 3 call sites
- Extract killDirectChild helper for consistent guarded child termination
- Fix kill-tree.ts runTaskkill to handle async spawn errors (best-effort contract)
- Add regression tests for graceful, forced, and no-tree-kill taskkill error paths
- Add kill-tree test for async taskkill error survival

Fixes openclaw#101381
@wm0018
wm0018 force-pushed the fix/windows-taskkill-async-error-leak branch from 5cd0972 to 877b9fb Compare July 7, 2026 05:57
@vincentkoc vincentkoc self-assigned this Jul 7, 2026
@vincentkoc

Copy link
Copy Markdown
Member

Closing as the weaker duplicate of #101392 for #101381.

#101392 covers the same asynchronous taskkill.exe spawn-error gap, but centralizes the direct-child fallback, clears the force-kill timer, covers the shared agent-core sibling, and includes focused Windows regression tests. This branch leaves the timer armed after graceful spawn failure and has no regression test.

The canonical branch still needs real Windows proof before landing; this closure only consolidates the implementation discussion.

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

Labels

P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: taskkill fallback swallows spawn errors, permanently leaking Windows process trees

2 participants