Conversation
Cron jobs build a fresh AIAgent per run; on the codex_app_server runtime the agent lazily spawns a codex app-server subprocess that nothing ever closed. One-shot CLI runs mask this (process exit closes the child's stdin), but cron runs inside the long-lived gateway process, and the client's reader threads pin it against GC — so every scheduled run leaked one app-server process, indefinitely. Add close_codex_session() and call it from the cron runner's finally. On the inactivity-timeout path this also kills the hung subprocess, matching the intended caught-and-killed semantics. Fixes NousResearch#62101 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GBqupVzebanvnWrdYZXu1K
Contributor
Duplicate of #62105 — same fix for #62101 (cron per-run |
teknium1
reviewed
Jul 11, 2026
teknium1
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for tracing the missing cron-owned Codex session cleanup; current main does leave _codex_session outside AIAgent.close() (run_agent.py:3463-3544) while cron creates a fresh agent per run (cron/scheduler.py:3044-3075).
Problems
- The proposed
cron/scheduler.py:3144close can run while the worker is still active: the pool is stopped withwait=False(cron/scheduler.py:3133-3134) andagent.interrupt()follows later (cron/scheduler.py:3157-3159).CodexAppServerSession.close()clearsself._client(agent/transports/codex_app_server_session.py:300-310), but the activerun_turn()loop readsself._client.is_alive()(agent/transports/codex_app_server_session.py:455-465). This is a close-vs-turn race.
Suggested changes
- Make
run_turn()safe against concurrent close, then route cleanup through_teardown_cron_agent()(cron/scheduler.py:3324-3346), which already covers both inline and deferred cron teardown. - Add a scheduler-path regression; the added tests currently validate only the standalone helper.
Automated hermes-sweeper review.
This was referenced Jul 30, 2026
6 tasks
This branch has not been deployed
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.
What does this PR do?
Fixes a process leak when
model.openai_runtime: codex_app_serveris active: every cron run inside the long-lived gateway process could leave behind onecodex app-serversubprocess and its MCP children.The fix now follows the cron agent's existing lifecycle boundary:
agent/codex_runtime.pyprovides an idempotent, exception-safeclose_codex_session(agent)helper.cron/scheduler.pyinvokes that helper from_teardown_cron_agent(), so cleanup happens on both inline and delivery-deferred teardown paths.agent/transports/codex_app_server_session.pyretains stable client/thread references duringrun_turn(), allowing inactivity-timeout teardown to close the transport concurrently without aNoneTyperace.turn/interruptis treated as non-fatal because the turn is already stopping.Related Issue
Fixes #62101
Why
Cron constructs a fresh
AIAgentper run. The Codex runtime lazily attaches aCodexAppServerSession, butAIAgent.close()does not currently own that session. In a one-shot CLI process, parent exit masks the problem; in the long-lived gateway, the client reader threads keep the subprocess and its pipes alive indefinitely.Cleanup originally landed immediately after
ThreadPoolExecutor.shutdown(wait=False). Review correctly identified that the worker can still be insiderun_turn()at that point. This revision moves cleanup to the centralized cron teardown path and makes the active turn safe when close happens concurrently.Validation
pytest -o addopts='' tests/agent/test_codex_runtime_close.py tests/agent/transports/test_codex_app_server_session.py tests/cron/test_run_one_job.py -q— 79 passedpytest -o addopts='' tests/cron/test_cron_inactivity_timeout.py tests/cron/test_codex_execution_paths.py -q— 13 passedpython -m py_compilefor all changed production and test modules — passedself._client.is_alive()dereference made the new concurrency regression fail with the originalAttributeError; restoring the fix returned the suite to green.The focused regressions cover:
run_turn()turn/interruptagent.close()failsManual verification
With
provider: openai-codexandopenai_runtime: codex_app_server, a triggered cron run returns the gateway's Codex child-process count to baseline after delivery/teardown instead of leaking one app-server per run.Checklist