Skip to content

Studio backend tests: close a scoped MCP session a test leaves cached - #15407

Merged
danielhanchen merged 1 commit into
mainfrom
ci-mcp-sessions-reset
Oct 26, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
ci-mcp-sessions-reset

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

(Python 3.13, g-r) failed on #15384 (job 121698439734), which only touches FSDP2 code:

studio/backend/tests/test_mcp_http_sessions.py::test_unscoped_http_stays_one_shot
E   AssertionError: assert {('http://127...7fe68c1ef0e0>} == {}

The test asserts mcp_client._mcp_sessions == {} after two unscoped calls. Whether it passes depends on what ran before it on the same xdist worker.

Cause

test_mcp_http_read_timeout.py::test_tool_call_outlives_sdk_read_timeout[chat-1] makes a scoped call, which caches a session in _mcp_sessions, and never closes it. I found it by checking _mcp_sessions after each of the 41 backend test files that touch the MCP client. It was the only test that left an entry behind (('http://127.0.0.1:.../mcp', (), 'chat-1', 'owner')).

The same pattern as #15287 (cached tools and probe cool-offs leaking between tests): module state one test leaves behind decides a later test.

Reproduced on main by running the two in that order:

pytest -p no:randomly 'tests/test_mcp_http_read_timeout.py::test_tool_call_outlives_sdk_read_timeout[chat-1]' \
    tests/test_mcp_http_sessions.py::test_unscoped_http_stays_one_shot
main:        1 failed, 1 passed
this branch: 2 passed

Fix

  • The leaker: test_tool_call_outlives_sdk_read_timeout closes its sessions in a finally.
  • For the next one: the autouse _forget_mcp_discovery_state fixture in studio/backend/tests/conftest.py, from Studio backend tests: forget MCP discovery state between tests #15287, also closes any session left in _mcp_sessions, before and after each test (close_mcp_sessions(all_accounts = True)).
    • Stand-in sessions are tolerated. Tests that monkeypatch _mcp_sessions with SimpleNamespace objects (test_account_tool_isolation.py) have no close(), and their own monkeypatch restores the real dict, so that AttributeError is skipped.
  • The guard: test_mcp_session_cache_reset_between_tests.py, an ordered pair in one xdist group. The first test leaves a session cached the way a scoped call does. The second fails unless the cache is empty and that session was closed.

Verification

  • The ordering above: fails on main, passes here.
  • The guard pair: 2 passed, both serially and under -n 2 --dist loadgroup.
  • Sabotage: with the conftest close removed, the pair's second test fails.
  • All backend test files that touch the MCP client: 5781 passed under -n 8. test_blender_managed.py is left out because it fails on this host on main too.
  • ruff format and check are clean.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T17:45:52.905554Z f6f7e59 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: f6f7e5981d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 0ea0473 into main Oct 26, 2026
0 of 36 checks passed
@danielhanchen
danielhanchen deleted the ci-mcp-sessions-reset branch October 26, 2026 17:49
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