fix: kill Claude subprocess on stop/exit to prevent orphaned agents - #430
fix: kill Claude subprocess on stop/exit to prevent orphaned agents#430aholston wants to merge 2 commits into
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical issue where Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses the issue of orphaned claude subprocesses by implementing cleanup logic on exit, stop, and disconnect paths. The use of atexit, a SIGTERM handler for Docker, and explicit cleanup in the TUI unmount process are all solid improvements. My main feedback is a suggestion to refactor some duplicated code in pentestgpt/core/backend.py to improve maintainability.
| pids_before = _get_child_pids() | ||
| result = self._client.connect() | ||
| if result is not None: | ||
| await result | ||
| self._subprocess_pids = _get_child_pids() - pids_before |
There was a problem hiding this comment.
There's significant code duplication between this resume method and the connect method. Much of the logic for setting up environment overrides, creating ClaudeAgentOptions, initializing ClaudeSDKClient, and connecting is nearly identical.
Consider refactoring the common logic into a private helper method, for example _initialize_and_connect(self, resume_session_id: str | None = None). This would improve maintainability by keeping the connection logic in one place.
The connect method could call await self._initialize_and_connect() and the resume method could call await self._initialize_and_connect(resume_session_id=session_id).
Summary
ClaudeCodeBackendnow captures child PIDs after connecting and killsthem explicitly in
disconnect()and viaatexit, so theclaudeCLIsubprocess is cleaned up on any exit path
tui.pyon_unmountnow callscontroller.stop()so the controller'sdisconnect path actually runs when the TUI closes
main.pyinstalls a SIGTERM handler sodocker stoptriggerssys.exit()and therefore
atexitcleanupRoot cause
When the Python controller exited (TUI closed, Ctrl+C, or
docker stop),the
claudeCLI subprocess it spawned was orphaned — re-parented to thecontainer's bash (PID 1) — and kept running indefinitely. The controller's
stop()method only set an in-memory flag, which does nothing if the Pythonprocess is already gone.
Test plan
make test)claudeprocess remains (ps aux)docker stop pentestgpt— verify process is gone on restartatexit