fix(agent): close _codex_session in AIAgent.close() to prevent subprocess leaks (#66671) - #66678
Closed
kyssta-exe wants to merge 1 commit into
Closed
kyssta-exe wants to merge 1 commit into
kyssta-exe wants to merge 1 commit into
Conversation
…cess leaks (NousResearch#66671) AIAgent.close() cleans up background processes, terminal sandboxes, browser daemons, child agents, HTTP clients, message history, and the SQLite session row — but never closes self._codex_session. This orphans the Codex app-server subprocess tree plus its MCP children on every closed Codex-route session. Fix: add a guarded, idempotent _codex_session close step to the close() method, mirroring the existing crash-path cleanup in codex_runtime.py. CodexAppServerSession.close() is already guarded by self._closed, so calling it multiple times is safe.
Contributor
tonydwb
reviewed
Jul 18, 2026
tonydwb
left a comment
There was a problem hiding this comment.
Code Review Summary
Verdict: Approved
Clean, well-scoped fix for a real subprocess leak. The change:
- Adds a guarded
_codex_sessionclose toAIAgent.close()(step 7, renumbers step 8) - Uses
getattr+Nonecheck matching existing defensive patterns CodexAppServerSession.close()is idempotent, so repeated calls are safe- Preserves comment style and numbering of surrounding code
No issues found. LGTM.
Reviewed by Hermes Agent
1 task
This was referenced Jul 18, 2026
fix(agent): close _codex_session in AIAgent.close() to prevent Codex subprocess leak (#66671)
#66865
Closed
Collaborator
|
Thanks for the focused lifecycle repair. The premise is confirmed on current main: Problems
Suggested changes
Automated hermes-sweeper review. |
14 tasks
Contributor
Author
|
Closing this PR because it has merge conflicts (mergeable=CONFLICTING). Please rebase on the latest main branch and resolve the conflicts before re-opening. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AIAgent.close()cleans up background processes, terminal sandboxes, browser daemons, child agents, HTTP clients, message history, and the SQLite session row — but never closesself._codex_session. This permanently orphans the Codex app-server subprocess tree (plus its MCP children) whenever a Codex-route session is closed viasession.closeor any other path that tears down the agent.Fix
Add a guarded, idempotent
_codex_sessionclose step toAIAgent.close(), matching the existing crash-path cleanup inagent/codex_runtime.py.CodexAppServerSession.close()is already guarded byself._closed, so repeated calls are safe.Testing
python3 -c "import ast; ast.parse(...)".Fixes #66671