Skip to content

fix(mcp): enforce session pool capacity during promotion - #4962

Open
XIIRUAN wants to merge 3 commits into
bytedance:mainfrom
XIIRUAN:fix/mcp-session-pool-capacity
Open

fix(mcp): enforce session pool capacity during promotion#4962
XIIRUAN wants to merge 3 commits into
bytedance:mainfrom
XIIRUAN:fix/mcp-session-pool-capacity

Conversation

@XIIRUAN

@XIIRUAN XIIRUAN commented Aug 23, 2026

Copy link
Copy Markdown

Why

MCPSessionPool checks its LRU capacity before starting session creation. Because initialization awaits, different keys can all observe spare capacity and enter _inflight, then each promote into _entries without another capacity check. This can leave the persistent-session registry above MAX_SESSIONS, retaining extra subprocesses and connections.

What changed

  • Recheck and enforce LRU capacity atomically when an initialized session is promoted.
  • Close promotion-time eviction victims outside the registry lock through their owner-task lifecycle.
  • Add a deterministic regression test for two distinct keys initializing concurrently with a one-session capacity.
  • Document the promotion-time capacity invariant.

Surface area

  • Frontend UI — page / component / setting / interaction under frontend/
  • Backend API — endpoint / SSE event / request-response shape under backend/app
  • Agents / LangGraph — agent node, graph wiring, langgraph.json, or prompt change
  • Sandboxdocker/ or sandboxed execution
  • Skills — change under skills/
  • Dependencies — new/upgraded entry in backend/pyproject.toml or frontend/package.json
  • Default behavior change — the configured session-pool capacity is now enforced during concurrent promotion
  • Docs / tests / CI only — no runtime behavior change

Screenshots / Recording

N/A — backend-only concurrency fix.

Bug fix verification

  • Test path: backend/tests/test_mcp_session_pool.py::test_concurrent_distinct_sessions_respect_capacity
  • Red on main, green on this branch: yes.
  • On main, two distinct concurrent initializations with MAX_SESSIONS = 1 leave two registered sessions. This branch keeps one registered session and closes the evicted owner session.

Validation

  • cd backend && uv run pytest tests/test_mcp_session_pool.py::test_concurrent_distinct_sessions_respect_capacity -q
  • cd backend && uv run pytest tests/test_mcp_session_pool.py -q -k "not test_session_pool_tool_pins_cwd_and_temp_env and not test_session_pool_tool_does_not_override_explicit_tmpdir" — 40 passed, 2 deselected (existing Windows path/permission assertions)
  • cd backend && uv run ruff check .
  • cd backend && uv run ruff format --check .
  • python scripts/check_agent_guidance.py
  • Full non-live backend suite was also run on Windows: 11,794 passed, 117 skipped; 140 platform-dependent failures were unrelated to this change.

AI assistance

Tool(s) used: Codex

How you used it: Used to inspect the concurrency path, draft the regression test and implementation, and run validation. I reviewed and verified the final diff.

  • I've read and understand every line of this change and take responsibility for it — it's not unreviewed AI output.

@CLAassistant

CLAassistant commented Aug 23, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added area:docs Documentation and Markdown only area:mcp Model Context Protocol integration risk:medium Medium risk: regular code changes size/S PR changes 20-100 lines labels Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:docs Documentation and Markdown only area:mcp Model Context Protocol integration risk:medium Medium risk: regular code changes size/S PR changes 20-100 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants