Repository navigation
Studio backend tests: forget MCP discovery state between tests - #15287
Merged
Merged
Conversation
mcp_client._tool_cache and _probe_cooloff_until are module state keyed by server id, and most MCP tests use "s1". A failed probe in test_mcp_apps.py::test_a_rediscovery_that_fails_or_races_an_edit_caches_nothing left "s1" cooling off, so test_get_enabled_mcp_tools_caches_discovery, later on the same xdist worker, skipped discovery for its own "s1" and failed with assert 0 == 1 (seen on #15279's g-r shard). An autouse conftest fixture clears both before and after every test, like the other module-state resets there, and a two-test pair pins it.
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
added a commit
that referenced
this pull request
Oct 26, 2026
…#15407) ## 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 #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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
studio/backend/tests/test_mcp_servers.py::test_get_enabled_mcp_tools_caches_discoveryfails now and then in(Python 3.13, g-r), for example on #15279, and passes when run alone:Cause
The tests, not the product.
core.inference.mcp_clientkeeps discovered tools (_tool_cache) and failed-probe cool-offs (_probe_cooloff_until) as module state keyed by server id, and most MCP tests name their servers1.test_mcp_apps.py::test_a_rediscovery_that_fails_or_races_an_edit_caches_nothing(from Studio: render MCP Apps widgets in the chat thread #9301) runs a probe that fails. That correctly records a cool-off fors1, and nothing removes it.test_get_enabled_mcp_tools_caches_discoverylater runs on the same worker, it resets the tool cache but not the cool-off map.get_enabled_mcp_tools()then skips discovery for its owns1, so the fake probe is never called.How it was found: I ran the 151 MCP-related backend test files with a detector on that state. It named this leak, plus
test_mcp_tool_read_off_event_loop.pyleavings1in the tool cache.Running the two tests in that order reproduces the CI failure every time.
Fix
studio/backend/tests/conftest.py,_forget_mcp_discovery_state, clears both maps before and after every test. It follows the other module-state resets already there and does nothing whenmcp_clienthas not been imported.test_mcp_discovery_state_reset_between_tests.pypins it: two tests in one xdist group. The first records a cool-off and a cache entry the way the client does; the second asserts neither survived.Verification
test_mcp_apps.py::test_a_rediscovery..., thentest_get_enabled_mcp_tools_caches_discovery), plus the new pair: 5 passed here. Without the fixture, 2 failed: the caching test and the pair's second test.mcp_client: 5703 passed here against 5701 on main, which is the 2 new tests. The same 5 failures on both sides come from this host's environment (test_blender_managed.pyx4 andtest_validate_url_rejects_bad[noscheme.com]).