Skip to content

[app-server] let idle auto-attached threads unload - #1

Draft
freaksdotcom wants to merge 1 commit into
mainfrom
agent/dispose-mcp-on-last-unsubscribe
Draft

freaksdotcom wants to merge 1 commit into
mainfrom
agent/dispose-mcp-on-last-unsubscribe

Conversation

@freaksdotcom

@freaksdotcom freaksdotcom commented Jul 26, 2026 •

Copy link
Copy Markdown
Owner

TL;DR: Automatic app-server thread attachments now continue delivering events without keeping idle thread runtimes and their MCP processes alive forever. Explicit owners still retain the runtime, the existing 30-minute idle grace remains unchanged, and normal turns or inline reviews atomically establish ownership before work is submitted.

Problem

The app server automatically attaches every initialized connection to internally created threads so those clients can observe events and answer server requests. Those delivery-only attachments were stored in the same set as explicit thread owners. When a TUI exited, its explicit subscription was removed, but another long-lived initialized client could keep the subscriber count nonzero indefinitely, preventing the 30-minute idle-unload timer from ever starting.

This leaves the thread runtime, including per-thread MCP child processes, alive after the interactive client is gone.

Solution

  • Track each connection attachment as either delivery-only Auto or retaining Explicit ownership.
  • Preserve event and server-request routing for both attachment kinds while basing unload eligibility only on explicit owners.
  • Upgrade Auto to Explicit monotonically for explicit lifecycle requests, normal turn/start, realtime work, and inline reviews.
  • Serialize turn/review admission with unload commitment so work is either accepted with retaining ownership or rejected after closing has committed.
  • Keep the existing 30-minute zero-owner, idle grace period. Ownership or activity changes restart the full deadline; archive behavior remains immediate.
  • Use Tokio's monotonic clock for deterministic deadline tests without changing real-time behavior.

The explicit ownership distinction is preferable to immediate unload: automatic observers remain functional, transient disconnects retain the existing recovery window, and truly unused runtimes eventually release MCP processes.

Review focus

  • ThreadEntry attachment bookkeeping and monotonic Auto to Explicit upgrades.
  • try_admit_connection_attachment lock ordering and its admission-versus-closing race contract.
  • Classification of automatic fanout versus explicit lifecycle, turn, realtime, and review call sites.
  • Coalesced watcher handling and exact 30-minute deadline resets.
  • The internally spawned-child WebSocket test, which exercises the real automatic-attachment path.

Validation

  • App-server lifecycle unit tests: 5 passed.
  • Thread ownership-state tests: 3 passed.
  • Automatic child delivery plus direct turn/start upgrade: passed.
  • Exact deadline/rejoin/cold-resume transcript test: passed.
  • WebSocket unsubscribe/disconnect grace test: passed.
  • Inline-review regression: passed.
  • just fix -p codex-app-server: passed with no findings.
  • just fmt: passed.
  • git diff --check: passed.

The broader app-server run completed 1,026 tests: 1,019 passed, 1 skipped, and 7 initially failed. Building the missing companion binaries cleared five failures. The two remaining unrelated tests fail because the copied codex-linux-sandbox cannot load libssl.so.3, after which their image/apply-patch operations time out; all affected lifecycle, ownership, archive, and WebSocket tests passed in that run.

Related work

This independently validated patch addresses openai/codex#11324 and overlaps the CLA-blocked draft openai/codex#30985. It additionally covers race-safe ownership admission for normal turns and inline reviews, closing-boundary rejection, real automatic event delivery, and transcript-preserving cold resume.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant