Repository navigation
fix(agent_host): release the Codex native thread as each Work ends - #312
sanketpatel32 wants to merge 3 commits into
Conversation
…ode-Amadeus#207) Refs Code-Amadeus#207. A loaded Codex App Server thread owns the MCP runtime spawned for the explicitly bound connections, and that ownership is exclusive while the thread stays resident. Sequential Work runs could open a second owner while the previous thread still held the bound data root, and the only teardown was adapter close, which could terminate MCP descendants before their cleanup ran. The Host now treats the native thread as the per-Work release unit. The last dependent run sends thread/unsubscribe, the managed thread_unload_delay_secs=0 lets the App Server retire the thread and settle its MCP runtime, and thread/loaded/list confirms the unload inside the same critical section a later open must pass through. Release runs from run() finalization on completion, cancellation, timeout, startup and stream failures alike; an orphaned submission whose acknowledgement was lost is retained rather than torn down. Outcomes are recorded as metadata.codex.thread_release plus a native.release event, kept distinct from the business result so a cleanup failure never overwrites it, and adapter close settles tracked threads before shared transport teardown, reporting unsettled when confirmation failed. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
54a0607 to
22bc3fe
Compare
Lucas1479
left a comment
There was a problem hiding this comment.
Thank you @sanketpatel32 for working on #207. The ownership bookkeeping is careful: a per-thread refcount, a release gate, and release facts kept apart from the business result.
However, the release mechanism is the one #207 reports as insufficient, so I can't merge this approach:
-
thread/unsubscribewiththread_unload_delay_secs=0does not shut the MCP server down gracefully.- #207 describes exactly this experiment on a real app-server. The lock was released, but the synthetic MCP's
finallysentinel never ran in any of the three unloaded rounds. The issue excludes that change for this reason. - The pinned Codex 0.154.0 source explains why:
- Unload submits
Op::Shutdown, which reachesshutdown_session_runtime→mcp_runtime.shutdown()(handlers.rs, runtime.rs). RmcpClient::shutdown()terminates the stdio process before the transport is dropped (rmcp_client.rs).- On Windows that means
TerminateJobObject/TerminateProcess, with no stdin close and no grace period (stdio_server_launcher.rs).
- Unload submits
- Child processes the MCP server started inside that job die with it. That matches #207's observation that an MCP-launched Edge window disappeared on client disconnect.
- With this PR, that would happen at the end of every Work. It conflicts with #207's acceptance items on graceful settlement and on preserving launched applications.
- #207 describes exactly this experiment on a real app-server. The lock was released, but the synthetic MCP's
-
The graceful-cleanup tests check the fixture, not Codex.
_McpOwningCodex.thread_unsubscribecloses the child's stdin itself, so the sentinel and child-preservation assertions describe the fake app-server's behavior. A fix for #207 needs evidence from the real pinned app-server, as the issue asks: a stdio MCP that holds a lock, writes afinallysentinel and launches a child. -
Minor:
CODEX_APP_SERVER_RELEASE_CONFIRM_TIMEOUT_Swas added toconfig/catalog_legacy.json. That list is for existing legacy keys; new settings should be declared in the config catalog.
#207 notes that the ownership contract needs maintainer review before a lifecycle fix. Before reworking this PR, please propose the approach in the issue, with real-binary evidence. Two possible directions are reusing the bound connection across Works, or a release path that lets the MCP server exit gracefully.
…e catalog Moves the new timeout from config/catalog_legacy.json into config/catalog/providers/codex.json (integer 0–600, default 30, app-server transport only) and regenerates settings.py, configCatalog.generated.ts and .env.example.
|
Addressed in d9d519e + the approach proposal now posted on the issue:
The rework itself waits on the proposal being accepted, per your instruction. |
Summary
Closes #207 — makes the loaded native thread the unit of MCP ownership in the Codex App Server adapter, so a second Work can never bind a connection whose previous owner still holds it.
_thread_runsregisters each Work's run under its thread; the last ending run releases the thread (siblings share a refcount)._settle_native_thread()runs inrun()'sfinally— done, cancelled, timeout, startup failure, stream failure, error and direct coroutine cancellation all reach it. Orphaned submissions →retained(unknown live work is never torn down).thread/unsubscribe, then pollthread/loaded/listuntil unload withinrelease_confirm_timeout_s(30 s,CODEX_APP_SERVER_RELEASE_CONFIRM_TIMEOUT_S). A terminal model turn is never treated as cleanup.released/release_unconfirmed/release_failed/shared/retained/unsupported(injected clients lacking the API — nothing is fabricated). Facts land onresult.metadata["codex"]["thread_release"]and anative.releaseProviderEvent appended after the business result, so cleanup failure can't overwrite it._sdk_lock; a_release_gateforces every open to wait for in-flight releases — no bind-during-unconfirmed-teardown.close(): rejects new opens, releases all tracked threads under the lock before shared transport teardown, and reports{"settled": False, "errors": [...]}→adapter_unsettled:<id>.test_mcp_stdio_lifetime.py's graceful-window-then-kill pattern), so MCP-launched user apps are not killed by a forced unload.docs/mcp_environment.mddocuments the contract.Test plan
test_codex_thread_release.py(17 tests): sequential release-before-open, resume, cancel/timeout/error/direct-cancel, shared refcount, concurrent-release serialization, close ordering, and a synthetic MCP with lock + cleanup sentinel + launched-child preservationtest_codex_app_server_adapter.py+test_codex_thread_release.py: 91 pass; MCP/provider suites: 114 passruffcleanGenerated with Devin