fix(agent): close _codex_session in AIAgent.close() to prevent Codex subprocess leak (#66671) - #66865
Closed
xxiaoxiong wants to merge 1 commit into
Closed
Conversation
…subprocess leak (NousResearch#66671) AIAgent.close() cleans up background processes, terminal sandboxes, browser daemons, child agents, and the OpenAI client — but never closes self._codex_session. The only _codex_session.close() calls were the crash/retire paths in codex_runtime.py, meaning every session.close on the Codex app-server route permanently leaks one codex app-server process plus its MCP children. Fix: add step 5b in close() that guards _codex_session with getattr, calls .close(), and sets it to None. Idempotent and guarded so non-codex agents (no attr) and double-close both no-op. Add regression tests: - test_close_closes_codex_session: close() calls close() and nulls it - test_close_handles_missing_codex_session: non-codex agent not crash - test_close_idempotent_on_codex_session: double-close safe Co-authored-by: teknium1 <127238744+teknium1@users.noreply.github.com>
Collaborator
Author
1 task
This was referenced Jul 18, 2026
Closed
Enough1122
pushed a commit
to Enough1122/hermes-agent
that referenced
this pull request
Jul 19, 2026
…sResearch#66671) AIAgent.close() cleaned up background processes, terminal sandboxes, browser daemons, child agents, the OpenAI/httpx client, conversation history, and the SQLite session row — but never closed self._codex_session. The only _codex_session.close() calls lived in crash/retire paths inside agent/codex_runtime.py, so every closed Codex-route session permanently leaked one codex app-server process plus its MCP children. Long-running `hermes serve` deployments accumulated one leaked process tree per closed session. Add a guarded, idempotent close step right after the OpenAI client close, mirroring its try/except + reference-clearing pattern. First call closes and clears the reference; any later call is a no-op. No public API change. Tests: add TestAIAgentCloseCodexSession covering close-and-clear, the no-attribute fallback path, and double-close idempotency. Test shape drawn from closed duplicate NousResearch#66865 as the reviewer's suggested salvage material. Fixes NousResearch#66671.
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
Fixes #66671.
AIAgent.close()releases all resources held by an agent instance — background processes, terminal sandboxes, browser daemon sessions, child agents, the OpenAI/httpx client, conversation history, and the SQLite session row — but never closesself._codex_session. The only_codex_session.close()calls in the codebase are the crash/retire paths inagent/codex_runtime.py. Result: everysession.closeon the Codex app-server route permanently leaks onecodex app-serversubprocess plus its MCP children. Long-runninghermes servedeployments accumulate one leaked process tree per closed session.Reported on v0.18.2 (
c7e09f25); I verified the close path is unchanged on currentmain.Fix
Add step 5b in
AIAgent.close()that guards_codex_sessionwithgetattr(so non-codex agents that never set the attribute are unaffected), calls.close()on it, and sets it toNone. Mirrors the existing crash/retire pattern inagent/codex_runtime.py:700-704and727-731. Idempotent and individually guarded so a failure here does not suppress the rest ofclose().Regression tests
tests/run_agent/test_codex_app_server_integration.py::TestCodexAgentClose:test_close_closes_codex_session—close()calls.close()on the codex session and nulls the attributetest_close_handles_missing_codex_session— agent with no_codex_sessiondoes not crash (non-codex routes)test_close_idempotent_on_codex_session— doubleclose()only calls.close()onceAll 3 tests pass on
mainwith this patch.Checklist
test_close_closes_codex_sessionandtest_close_idempotent_on_codex_sessionwould fail)