Repository navigation
Completed subagents continue to count against thread limit #22779
Description
What version of Codex CLI is running?
codex-cli 0.130.0
What subscription do you have?
Pro
Which model were you using?
ChatGPT 5.5
What platform is your computer?
Darwin 25.4.0 arm64 arm (MacBook Pro, M4 Max, MacOS Tahoe 26.4.1)
What terminal emulator and version are you using (if applicable)?
WarpTerminal v0.2026.05.13.09.15.stable_01
Codex doctor report
Not available. codex doctor --json is not supported by this installed CLI version; it exits with unexpected argument --json.
What issue are you seeing?
Completed subagents can continue to count against the per-session agent thread limit. After enough subagents have completed, a later spawn_agent call may fail with:
collab spawn failed: agent thread limit reached
This can happen even when earlier subagents have reached a final/completed state and are no longer doing work. Explicitly closing completed agents can free the quota, which suggests the quota is tied to thread removal/close rather than completion.
What steps can reproduce the bug?
Generic reproduction:
- Start a Codex session with subagents enabled.
- Spawn subagents sequentially or in batches.
- Wait for each spawned subagent to complete normally.
- Do not explicitly close the completed subagents.
- Continue spawning more subagents in the same session until the configured
agents.max_threadsquota is reached.
Expected result: completed subagents should no longer consume active spawn quota.
Actual result: later spawns can fail with agent thread limit reached until completed subagents are explicitly closed or the session is restarted.
A focused code-level reproduction is to set a low agents.max_threads, spawn an agent, drive it to TurnComplete, and then attempt another spawn. The second spawn should be allowed once the first agent is final.
What is the expected behavior?
Subagent quota should represent agents that are currently active/running or being reused for new work, not agents that have already reached a final status. Completed agents may remain addressable for history/listing purposes, but they should not block new spawns unless they are restarted and reacquire a quota slot.
Additional information
I investigated the relevant lifecycle/accounting path and found that spawned/resumed agents increment quota, while quota release was tied to explicit shutdown/close/thread removal. Normal completion updates agent status but does not release the quota slot.
I have a focused branch with a proposed fix and tests here:
https://github.com/pengyou200902/codex/tree/fix/subagent-thread-limit
Proposed approach:
- Track counted agent IDs separately from retained agent metadata.
- Release the quota slot when an agent reaches a final status, while preserving metadata.
- Reacquire a quota slot before sending new work to an existing completed agent.
- Handle resumed agents whose restored history already has a final status.
Validation run on the branch:
just fmtjust fix -p codex-corecargo test -p codex-core release_spawn_slot_preserves_agent_metadata_until_thread_releasecargo test -p codex-core completed_agent_releases_max_threads_slotcargo test -p codex-core spawn_slot
Per docs/contributing.md, I am opening this issue first and would only open a PR if maintainers invite one.
Activity
pengyou200902 commentedon May 17, 2026
Thanks for taking a look. I updated the branch after rebasing onto latest
main.The current fix avoids the heavier watcher-based approach and instead releases the subagent quota slot from the normal session event path when an agent reaches a final status. Agent metadata is still retained, so completed agents remain listable/addressable, but they no longer block new spawns. If a completed agent receives new work later, it reacquires a slot first.
I also kept the small
Box::pin(Self::spawn_internal(...))change incore/src/session/mod.rs; the forked-spawn regression still stack-overflows without it on the updated branch.Validation after rebase:
cargo test -p codex-core spawn_agent_fork_last_n_turns_keeps_only_recent_turnscargo test -p codex-core completed_agentcargo test -p codex-core spawn_slotcargo test -p codex-core --libBranch: https://github.com/pengyou200902/codex/tree/fix/subagent-thread-limit
debuggerone commentedon May 22, 2026
Now I am getting a "First spawning was denied because Full-History-Forks don't allow am explicit Worker-Type" while it now says that it found old closed agents - then closes them and then spawns 6 new ones.
So the issue was fixed now as it seems on VS Code Version 26.519.32039
debuggerone commentedon May 22, 2026
I am sure the "Full-History-Forks" denial has nothing to do with the old closed ones.. but who knows... maybe it blocks them and they are still running while new ones are spawned?
fix(speckit-pro): make Codex agent close best-effort and resume-safe
Document root cause of primary-review stalls: Codex CLI agent-quota leak
docs(orch): Codex thread cap is MultiAgentV2's configurable default; …
antonreshetov commentedon Jul 25, 2026
Confirming this is still reproducible in Codex Desktop on macOS.
Environment:
Observed behavior:
agent thread limit reached.This is especially confusing because the error does not report the configured maximum, which agents still occupy slots, or how to release/reclaim them. The public documentation also does not appear to describe this limit or its lifecycle.
It would help if completed agents automatically released spawn capacity, or if the error exposed active/resident counts and a supported recovery action.
GLGDLY commentedon Aug 7, 2026
This is still reproducing in Codex Desktop on Windows11 today, any workarounds?
costleya commentedon Aug 13, 2026
Reproducible on MacOS 26.5.2 (25F84), ChatGPT Version 26.803.61601
I have a limit of 15. Even though 14 agents are "done", they remain not closed. The main agent claims there is no "close" operation it can use. Right now workaround is to increase to 50 parallel to work around these zombies.
Manimap commentedon Aug 13, 2026
I can confirm the same issue on windows 11 with and codex Version 26.803.81509.
CKJ2026 commentedon Aug 14, 2026
Confirming a closely related Codex Desktop lifecycle/persistence reproduction on macOS, with a read-only local state audit.
Environment:
26.803.61601(build6396)codex-cli 0.147.0-alpha.6.526.5.2(build25F84, arm64)Observed sequence:
completedorinterrupted) and only 2 were genuinelyactive / inProgress.idle / completedandnotLoaded / completed, but they still remained unarchived.open.This provides a concrete final-status transition where completion did not reconcile the persisted edge/lifecycle state. It appears consistent with the quota-accounting defect described in this issue, but Codex Desktop may also have a second persistence/UI reconciliation layer involved.
Additional user-visible symptom: after the Mac is unlocked or restarted, historical completed subagents can appear as
working; manually opening/clicking them can make them transition todone. I can prove the state mismatch above, but I have not captured CPU or memory sampling, so I am not claiming that every stale edge is still consuming compute.Questions for maintainers:
maincontain an equivalent of the proposedcounted_agent_ids/ release-on-final-status fix frompengyou200902:fix/subagent-thread-limit?openedge and Desktop restart/unlock UI behavior be tracked in this issue, or as a separate Desktop state-reconciliation issue?agents.max_threads?The proposed fork is currently far behind
main, so I am treating it as design evidence only, not as an applicable or released fix.pomazanbohdan commentedon Aug 14, 2026
Correction to my previous comment: the cleanup itself succeeded and did not immediately reproduce the failure.
The observed sequence was:
019fea4e-4227-7810-be40-9afcc9a23aadthen ran normally for a substantial period and spawned many new agents over multiple iterations. Those agents performed work and were explicitly completed/closed by the parent workflow.collab spawn failed: agent thread limit reached.The failing attempt was captured in turn
019fff84-e58c-7591-b7f8-2f3010d3e920, spawn callcall_BqDBs1lklX4C3NFAc6Sdkcm8. The parent was idle at inspection time, and the historical agents were no longer doing work.The runtime still recorded
token_limit_reached=falseandfull_context_window_limit_reached=false, so this is not a model context/token failure. The issue is that a long sequence of child completions/closures still exhausted or failed to release the orchestration spawn quota. The cleanup/relaunch was therefore a successful reset, followed by a new quota-accounting failure under sustained agent churn.Expected behavior: once child agents are completed/closed, their slots should be released deterministically, regardless of how many sequential agents the parent has created. The quota must count only currently live/reusable handles, not historical completed/closed children.
GLGDLY commentedon Aug 14, 2026
some addons from my observation, it seems mcp of subagents are also not correctly closed / even leaked, making codex occupying more and more RAM with running multiagent v2 for a period of time
bmolina-agendapro commentedon Aug 17, 2026
Reproduced on Codex Desktop / codex-cli 0.147.0-alpha.6.5 on macOS (arm64).
This is worse than quota accounting alone because the documented recovery operation is not consistently exposed to the root agent. The official Subagents documentation says Codex handles “closing agent threads” and explicitly recommends asking Codex to “close completed subagent threads.” In the affected session, however, the model-visible collaboration toolset contained spawn_agent, followup_task, send_message, interrupt_agent, list_agents, and wait_agent — but no close_agent or equivalent.
Concrete result with max_concurrent_threads_per_session = 2:
Reusing a completed thread with followup_task is only a partial workaround: it works when the same role/contract is needed again, but it is unsafe for a different role, model, sandbox, or responsibility. Raising the cap to the number of configured roles reduces failures but does not fix lifecycle accounting.
Expected:
This mismatch creates real token and latency cost: users build orchestration around a documented lifecycle capability, then discover only after long agent runs that the runtime cannot perform it. It also makes a basic sequential specialized-role pipeline substantially less reliable and more expensive than expected.
delbyte commentedon Aug 21, 2026
this is still happening!
Platform: Ubuntu
Codex CLI, latest
pomazanbohdan commentedon Aug 23, 2026
A current Windows reproduction confirms the quota symptom is part of the same lifecycle cluster: completed child rollouts can remain resident/open or reappear as pending_init, so spawn_agent returns agent thread limit reached even when visible work is done. See the UI evidence in #37042 and the residency diagnosis in #39694 and #34518.
richardmears commentedon Aug 25, 2026
This issue still persists now, even after app restart. UI shows no active subagents, but Codex states "the collaboration system returned agent thread limit reached despite showing no live child agent."
Anecdotally, this seems to happen more often when the app updates and then an older parent thread is continued (i.e. it seems to happen less when the same version of the app is used throughout a single thread).
Codex App Version 26.818.61809
MacOS Tahoe 26.6 (25G72)
tireiron commentedon Sep 12, 2026
Repro on Codex CLI 0.153.4. The open threads (even though they're completed) are ~100, which is what I suspect the limit is, and there doesn't seem to be a way to clean them up. The agent was working around it by trying to reuse old unrelated threads. Starting a new session seems to be the most viable workaround, though.