Skip to content

Studio backend tests: forget MCP discovery state between tests - #15287

Merged
danielhanchen merged 1 commit into
mainfrom
ci-mcp-discovery-state-reset
Oct 25, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
ci-mcp-discovery-state-reset

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

studio/backend/tests/test_mcp_servers.py::test_get_enabled_mcp_tools_caches_discovery fails now and then in (Python 3.13, g-r), for example on #15279, and passes when run alone:

tests/test_mcp_servers.py:1336: in test_get_enabled_mcp_tools_caches_discovery
E   assert 0 == 1
E    +  where 0 = len([])

Cause

The tests, not the product. core.inference.mcp_client keeps 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 server s1.

  1. 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 for s1, and nothing removes it.
  2. When test_get_enabled_mcp_tools_caches_discovery later 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 own s1, 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.py leaving s1 in the tool cache.

Running the two tests in that order reproduces the CI failure every time.

Fix

  • An autouse fixture in 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 when mcp_client has not been imported.
  • test_mcp_discovery_state_reset_between_tests.py pins 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

  • The leak order (test_mcp_apps.py::test_a_rediscovery..., then test_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.
  • All 40 backend test files that touch 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.py x4 and test_validate_url_rejects_bad[noscheme.com]).

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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-25T19:38:49.205448Z d187b8d 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. Delightful!

Reviewed commit: d187b8d9f6

ℹ️ 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 83c1c62 into main Oct 25, 2026
0 of 35 checks passed
@danielhanchen
danielhanchen deleted the ci-mcp-discovery-state-reset branch October 25, 2026 19:44
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.
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