Skip to content

fix(process): fall back when Windows taskkill cannot spawn - #101392

Merged
steipete merged 1 commit into
openclaw:mainfrom
ZengWen-DT:fix/101381-taskkill-spawn-error-fallback
Jul 7, 2026
Merged

steipete merged 1 commit into
openclaw:mainfrom
ZengWen-DT:fix/101381-taskkill-spawn-error-fallback

Conversation

@ZengWen-DT

@ZengWen-DT ZengWen-DT commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Closes #101381

What Problem This Solves

Fixes an issue where Windows timeout cleanup could fail to terminate a timed-out command when taskkill.exe failed after spawn() returned. Node reports failed child-process spawns through the child process error event, so the old try/catch fallback could miss the failure and leave the original command process running.

Why This Change Was Made

The Windows cleanup path now attaches an async spawn-error fallback to each taskkill.exe child and routes both synchronous and asynchronous failures through the same direct child.kill("SIGKILL") fallback. The change keeps the trusted System32 taskkill.exe resolution in src/process/exec.ts; the shared process-tree helper also now observes async taskkill errors so its existing best-effort ignore contract covers both failure modes.

This is the canonical candidate for #101381 after #101389 was closed as the weaker duplicate; #101392 keeps the timer cleanup, shared helper guard, and focused Windows regression coverage.

User Impact

Windows users should no longer accumulate leaked direct child processes when command timeout cleanup cannot start taskkill.exe. Normal taskkill.exe cleanup behavior is unchanged when it starts successfully.

Evidence

Node contract premise:

$ node -e "const {spawn}=require('node:child_process'); let caught=false; try { const child=spawn('/definitely/missing/openclaw-taskkill.exe'); child.on('error', err => console.log(JSON.stringify({caught, event:'error', code:err.code}))); } catch (err) { caught=true; console.log(JSON.stringify({caught, code:err.code})); } setTimeout(()=>{},20);"
{"caught":false,"event":"error","code":"ENOENT"}

Before/after behavior harness:

$ node --input-type=module - <<'EOF'
import { EventEmitter } from 'node:events';
const taskkillChild = new EventEmitter();
let fallbackCalled = false;
try {
  taskkillChild.emit('error', Object.assign(new Error('spawn ENOENT'), { code: 'ENOENT' }));
} catch (err) {
  console.log(JSON.stringify({ before: 'no-listener-throws', code: err.code ?? null, fallbackCalled }));
}
const taskkillChildAfter = new EventEmitter();
taskkillChildAfter.once('error', () => { fallbackCalled = true; });
taskkillChildAfter.emit('error', Object.assign(new Error('spawn ENOENT'), { code: 'ENOENT' }));
console.log(JSON.stringify({ after: 'listener-routes-fallback', fallbackCalled }));
EOF
{"before":"no-listener-throws","code":"ENOENT","fallbackCalled":false}
{"after":"listener-routes-fallback","fallbackCalled":true}

Focused local tests:

$ node scripts/run-vitest.mjs src/process/exec.test.ts src/process/exec.windows.test.ts src/process/kill-tree.test.ts
[test] starting test/vitest/vitest.process.config.ts

 RUN  v4.1.9 /srv/projects/openclaw/contrib/.radar/worktrees/issue-101381

 Test Files  3 passed (3)
      Tests  52 passed (52)
[test] passed 1 Vitest shard in 4.61s

Windows runner proof from this PR:

$ node scripts/test-projects.mjs src/shared/runtime-import.test.ts src/process/exec.windows.test.ts src/process/windows-command.test.ts src/infra/windows-install-roots.test.ts src/node-host/invoke-system-run-allowlist.test.ts src/daemon/schtasks.startup-fallback.test.ts extensions/lobster/src/lobster-runner.test.ts test/scripts/format-generated-module.test.ts test/scripts/npm-runner.test.ts test/scripts/pnpm-runner.test.ts test/scripts/run-with-env.test.ts test/scripts/ui.test.ts test/scripts/vitest-process-group.test.ts
✓ process src/process/exec.windows.test.ts (21 tests) 281ms
Test Files 1 passed (1)
Tests 21 passed (21)
[test] passed 8 Vitest shards in 25.31s

Other local checks:

$ git diff --check
# passed

$ .agents/skills/autoreview/scripts/autoreview --mode local
autoreview clean: no accepted/actionable findings reported
overall: patch is correct (0.82)

Not run locally: full pnpm check / pnpm test; this linked checkout is using focused local proof and GitHub CI for broad gates. Live Windows terminal proof from my local environment was not available because powershell.exe, pwsh, and cmd.exe were not present; the Windows behavior is covered by the Node child_process contract check, focused OpenClaw regression tests, and the Windows CI runner log above.

AI-assisted: AI-assisted; proof run manually.

@clawsweeper

clawsweeper Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 7, 2026, 2:29 AM ET / 06:29 UTC.

Summary
This PR adds async taskkill.exe spawn-error fallback handling in runCommandWithTimeout, ignores async taskkill errors in the shared kill-tree helper, and adds focused Windows regression tests.

PR surface: Source +12, Tests +72. Total +84 across 4 files.

Reproducibility: yes. for source-level reproduction: current main and v2026.6.11 still spawn taskkill.exe without async error handlers, and Node confirms failed spawns report through error events. I did not establish a live Windows end-to-end reproduction in this read-only review.

Review metrics: none identified.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #101381
Summary: This PR is the strongest current candidate fix for the open Windows taskkill async spawn-error issue; other same-root-cause PRs are closed, partial, or weaker and do not safely supersede it.

Members:

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

Merge readiness
Overall: 🦪 silver shellfish
Proof: 🦪 silver shellfish
Patch quality: 🦞 diamond lobster
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 redacted real Windows/OpenClaw terminal output, logs, or a reproducible validation transcript showing the async taskkill spawn-error fallback after this fix.

Proof guidance:

  • [P1] Needs real behavior proof before merge: Needs real behavior proof before merge: the PR body shows contract checks, a small harness, focused mocked tests, and Windows CI, but not a real Windows/OpenClaw run of the changed fallback; add redacted terminal output, logs, or a reproducible validation transcript and update the PR body for re-review.

Risk before merge

  • [P2] The PR body now includes Node contract output, an EventEmitter harness, focused local tests, and Windows CI, but still does not show a real Windows/OpenClaw execution of the changed async fallback path.
  • [P1] There are several same-root-cause PRs open or recently closed, so maintainers should keep this as the canonical branch only if its proof gate is satisfied and then close the weaker duplicates.

Maintainer options:

  1. Decide the mitigation before merge
    Land one canonical process-layer fix that preserves trusted System32 taskkill resolution, handles async taskkill errors in both exec cleanup and the shared helper, includes focused tests, and has redacted 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] The remaining blocker is contributor-supplied real Windows/OpenClaw proof and duplicate-candidate coordination, not a narrow automated code repair.

Security
Cleared: The diff only changes error handling around existing taskkill spawns and tests; it adds no dependencies, CI, permissions, secrets, package resolution, or new command construction.

Review details

Best possible solution:

Land one canonical process-layer fix that preserves trusted System32 taskkill resolution, handles async taskkill errors in both exec cleanup and the shared helper, includes focused tests, and has redacted real Windows proof.

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

Yes for source-level reproduction: current main and v2026.6.11 still spawn taskkill.exe without async error handlers, and Node confirms failed spawns report through error events. I did not establish a live Windows end-to-end reproduction in this read-only review.

Is this the best way to solve the issue?

Yes for the code shape: observing taskkill child error events and falling back to direct child kill is the narrowest maintainable fix in the owner path. The merge gate remains real Windows/OpenClaw behavior proof, not a code-shape blocker.

AGENTS.md: found and applied where relevant.

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

Label changes

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 🦪 silver shellfish and patch quality is 🦞 diamond lobster.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: Needs real behavior proof before merge: the PR body shows contract checks, a small harness, focused mocked tests, and Windows CI, but not a real Windows/OpenClaw run of the changed fallback; add redacted terminal output, logs, or a reproducible validation transcript and update the PR body for re-review.
Evidence reviewed

PR surface:

Source +12, Tests +72. Total +84 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 2 44 32 +12
Tests 2 72 0 +72
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 116 32 +84

What I checked:

  • Current main still has the exec gap: Current main at 13f8dfa starts Windows taskkill.exe children in runCommandWithTimeout without retaining async error handlers, so the synchronous catch fallback is bypassed after spawn returns. (src/process/exec.ts:530, 13f8dfae864f)
  • Current shared helper ignores only synchronous failures: The current shared runTaskkill helper catches synchronous spawn throws but does not observe async child-process error events. (packages/agent-core/src/harness/env/kill-tree.ts:111, 13f8dfae864f)
  • PR head handles async taskkill errors: The PR head introduces killDirectChild and spawnTaskkillOrFallback, attaches once("error", ...) to taskkill children, and routes failures back to direct child kill while preserving System32 resolution. (src/process/exec.ts:517, 1553c4f68f8a)
  • PR head adds focused regression coverage: The PR adds mocked Windows tests for forced and graceful taskkill child error fallback plus a shared kill-tree test that verifies async taskkill errors are observed and ignored. (src/process/exec.windows.test.ts:517, 1553c4f68f8a)
  • Node contract supports the bug premise: Node docs state error is emitted when a process could not be spawned and spawn is not emitted on failed spawns; a local Node check showed a missing executable returned with caught:false and emitted ENOENT asynchronously.
  • Latest release still has old behavior: Release tag v2026.6.11 still contains the same taskkill spawn/try-catch shape without async error handlers, so this fix has not shipped. (src/process/exec.ts:444, e085fa1a3ffd)

Likely related people:

  • vincentkoc: Recent history shows fix(windows): resolve taskkill in core spawns changed the same Windows taskkill/System32 cleanup path, and current blame points to a later refactor carrying these lines. (role: recent area contributor; confidence: high; commits: a192b2ea52b3, 835e6a75325a; files: src/process/exec.ts, src/process/exec.windows.test.ts, packages/agent-core/src/harness/env/kill-tree.ts)
  • sauerdaniel: Introduced the graceful process-tree termination behavior that defines the Windows taskkill SIGTERM-to-SIGKILL semantics now being hardened. (role: adjacent feature contributor; confidence: medium; commits: 20957efa4674; files: packages/agent-core/src/harness/env/kill-tree.ts, src/process/kill-tree.ts)
  • steipete: Recent commit history shows subprocess hardening in src/process/exec.ts, and CODEOWNERS only has the repository-level owner fallback for this path. (role: recent subprocess hardening contributor and CODEOWNERS fallback; confidence: medium; commits: a4b032e5d710; files: src/process/exec.ts, .github/CODEOWNERS)
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.
Review history (1 earlier review cycle)
  • reviewed 2026-07-07T05:58:40.345Z sha 1553c4f :: needs real behavior proof before merge. :: none

@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
@ZengWen-DT

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@steipete

steipete commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Land-ready verification:

  • Root cause confirmed against current main and the official Node child-process contract: a failed executable spawn returns a ChildProcess and emits error asynchronously, so the previous surrounding try/catch could not reach the direct-child fallback.
  • Reviewed the complete runCommandWithTimeout timeout/abort path, cron and WhatsApp callers, the shared agent-core kill-tree sibling, focused tests, current shipped behavior, and blame/provenance.
  • Best-fix verdict: this is the correct owner-boundary fix. It preserves trusted System32 resolution and routes graceful, delayed-force, and immediate-force taskkill failures through one fallback; PATH lookup and no-op listeners were rejected.
  • Focused exact-head proof: sanitized AWS Crabbox provider aws, lease cbx_1ea9b9166ea5, run run_8dfb93f3ed3f; pnpm test src/process/exec.test.ts src/process/exec.windows.test.ts src/process/kill-tree.test.ts passed 3 files / 52 tests.
  • Native Windows proof: CI run 28844539463, job 85545399409 (checks-windows-node-test) passed the updated src/process/exec.windows.test.ts on Windows Server 2025.
  • Mandatory autoreview: .agents/skills/autoreview/scripts/autoreview --mode branch --base origin/main --engine codex --model gpt-5.5 --thinking high completed clean with no accepted/actionable findings.
  • Repo prepare guard: OPENCLAW_TESTBOX=1 scripts/pr prepare-run 101392 passed hosted exact-head gates at 1553c4f68f8a51f0e0064c05030aa3ce85c0caae.

Known proof gap: I did not destructively hide or replace the protected System32 taskkill.exe on a live Windows host. The exact mocked failure branches ran on native Windows CI, and a real missing-executable Node probe confirmed the asynchronous event contract.

By Wynne668 (@ZengWen-DT, account created 2026-06-05) | OpenClaw: 85 PRs, 0 issues, 40 commits/12mo | GitHub: 46 commits, 85 PRs, 0 issues, 0 reviews/12mo.

@steipete
steipete merged commit 55fa22b into openclaw:main Jul 7, 2026
137 of 142 checks passed
@steipete

steipete commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 7, 2026
krissding added a commit to krissding/openclaw that referenced this pull request Jul 7, 2026
…estart (openclaw#101392 follow-up)

The spawn() call in relaunchGatewayScheduledTask() creates a detached child
process with child.unref() but without an 'error' listener. When spawn fails,
Node.js emits an async 'error' event that crashes the parent as an unhandled
error.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
krissding added a commit to krissding/openclaw that referenced this pull request Jul 7, 2026
…101392 follow-up)

The spawn mock in schtasks.startup-fallback.test.ts was missing the `on`
property, causing "child.on is not a function" when launchFallbackTaskScript
calls child.on("error", ...).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
sheyanmin pushed a commit to sheyanmin/openclaw that referenced this pull request Jul 8, 2026
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