← Back to PRs

#18126: Fix process tree kill leaving orphans on Unix (manual traversal fallback)

by Clawborn open 2026-02-16 14:35 View on GitHub →
size: S trusted-contributor
## Description Previously, `killProcessTree` tried `process.kill(-pid)` which only works if the pid is a process group leader. When processes are spawned without `detached: true`, they share the parent's group, so this call fails. Fallback was `process.kill(pid)`, which left children orphaned. ## Fix Added manual descendant discovery using `pgrep -P` when group kill fails, ensuring the entire tree is terminated. ## AI Transparency - **Assisted by**: OpenClaw Agent (Claude 3.5 Sonnet / Opus) - **Testing level**: Fully tested with new unit tests - **Prompt strategy**: Self-correction loop based on codebase analysis <!-- greptile_comment --> <h3>Greptile Summary</h3> This PR fixes orphaned child processes on Unix by adding `pgrep -P` based manual tree traversal when `process.kill(-pid, "SIGKILL")` fails (i.e., when the target process is not a process group leader). The implementation uses a BFS to discover all descendants before killing them bottom-up, then kills the root process last. - Adds `getChildrenUnix()` helper using `execSync('pgrep -P <pid>')` for child PID discovery - Replaces the old SIGTERM → grace period → SIGKILL flow with immediate SIGKILL (consistent with how all callers use this function — they only invoke it when SIGKILL is intended) - Adds a new Unix-specific integration test verifying tree termination via the manual traversal path - **Important**: A duplicate `killProcessTree` in `src/agents/shell-utils.ts` (used by `auto-reply/reply/bash-command.ts` and e2e tests) still has the old orphan-producing fallback and was not updated <h3>Confidence Score: 3/5</h3> - The core fix is sound but the duplicate implementation in shell-utils.ts means the orphan bug persists for some callers. - The pgrep-based tree traversal is a correct fix for the orphan problem, and the test validates the new path. Score is 3 rather than higher because the same bug exists in a duplicate killProcessTree in src/agents/shell-utils.ts that was not updated, leaving some call sites still vulnerable to orphaned processes. - src/agents/shell-utils.ts contains a duplicate killProcessTree that was not updated with the same fix and still orphans children. <sub>Last reviewed commit: e4f4d21</sub> <!-- greptile_other_comments_section --> <sub>(1/5) You can manually trigger the agent by mentioning @greptileai in a comment!</sub> <!-- /greptile_comment -->

Most Similar PRs