The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: close the Codex app-server session on agent teardown
JR Razmuse623432b8921
diff --git a/run_agent.py b/run_agent.pyindex e2bc5f8660b..60b3c1db0e6 100644--- a/run_agent.py+++ b/run_agent.py@@ -4274,6 +4274,21 @@ class AIAgent:except Exception:pass+ # 6c. Close the Codex app-server session. The runtime already drops+ # it on turn crash / retirement (agent/codex_runtime.py), but hard+ # teardown had no owner — a /new, /reset, or session expiry left the+ # app-server child process running until interpreter exit. Clear the+ # attribute BEFORE close() so a concurrent reader can't grab a+ # half-closed session, and so a raising close() can't strand a stale+ # reference behind.+ try:+ codex_session = getattr(self, "_codex_session", None)+ if codex_session is not None:+ self._codex_session = None+ codex_session.close()+ except Exception:+ pass+# 7. Free conversation history. Mirrors _release_evicted_agent_soft's# soft-eviction clear — close() is the hard teardown for true session# boundaries (/new, /reset, session expiry), so the message list won'tdiff --git a/tests/run_agent/test_codex_app_server_lifecycle.py b/tests/run_agent/test_codex_app_server_lifecycle.pynew file mode 100644index 00000000000..3e784ccaafc--- /dev/null+++ b/tests/run_agent/test_codex_app_server_lifecycle.py@@ -0,0 +1,82 @@+"""Codex app-server session lifecycle on hard agent teardown (#65260).++The Codex runtime drops ``agent._codex_session`` on turn crash and on+retirement (agent/codex_runtime.py), but ``AIAgent.close()`` — the hard+teardown for /new, /reset, and session expiry — had no owner for it, so+the app-server child process survived until interpreter exit.+"""++import threading++from run_agent import AIAgent+++class _FakeCodexSession:+ def __init__(self, raises: bool = False):+ self.close_calls = 0+ self._raises = raises++ def close(self):+ self.close_calls += 1+ if self._raises:+ raise RuntimeError("app-server already dead")+++def _bare_agent(session_id: str) -> AIAgent:+ """Minimal agent shell exercising close() without a real build."""+ agent = AIAgent.__new__(AIAgent)+ agent.session_id = session_id+ agent.client = None+ agent._active_children_lock = threading.Lock()+ agent._active_children = set()+ agent._end_session_on_close = False+ agent._session_messages = ["retained"]+ return agent+++def test_agent_close_releases_codex_app_server_session(monkeypatch):+ agent = _bare_agent("test-codex-lifecycle")+ codex_session = _FakeCodexSession()+ agent._codex_session = codex_session++ monkeypatch.setattr("run_agent.cleanup_vm", lambda _task_id: None)+ monkeypatch.setattr("run_agent.cleanup_browser", lambda _task_id: None)++ agent.close()+ agent.close()++ # Idempotent: the second close must not re-close a released session.+ assert codex_session.close_calls == 1+ assert agent._codex_session is None+ assert agent._session_messages == []+++def test_close_clears_reference_even_when_session_close_raises(monkeypatch):+ """A wedged app-server must not strand a stale session reference.++ The attribute is cleared BEFORE close() precisely so a raising close+ can't leave a dead session attached to the agent.+ """+ agent = _bare_agent("test-codex-lifecycle-raises")+ codex_session = _FakeCodexSession(raises=True)+ agent._codex_session = codex_session++ monkeypatch.setattr("run_agent.cleanup_vm", lambda _task_id: None)+ monkeypatch.setattr("run_agent.cleanup_browser", lambda _task_id: None)++ agent.close()++ assert codex_session.close_calls == 1+ assert agent._codex_session is None+++def test_close_without_codex_session_is_a_noop(monkeypatch):+ """Non-Codex sessions (the common case) must be unaffected."""+ agent = _bare_agent("test-no-codex")++ monkeypatch.setattr("run_agent.cleanup_vm", lambda _task_id: None)+ monkeypatch.setattr("run_agent.cleanup_browser", lambda _task_id: None)++ agent.close()++ assert getattr(agent, "_codex_session", None) is None