Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

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

Daniel Han0ea04730dbca
diff --git a/studio/backend/tests/conftest.py b/studio/backend/tests/conftest.py
index 07c1ae30469..ee973a9ac06 100644
--- a/studio/backend/tests/conftest.py
+++ b/studio/backend/tests/conftest.py
@@ -138,12 +138,20 @@ def _forget_hub_unreachable_verdict():
@pytest.fixture(autouse = True)
def _forget_mcp_discovery_state():
# Discovered tools and failed-probe cool-offs are keyed by server id, and most tests reuse "s1": a cool-off
- # one test records made a later test on the worker skip discovery for its own "s1".
+ # one test records made a later test on the worker skip discovery for its own "s1". A scoped HTTP session a
+ # test leaves cached is the same: test_mcp_http_sessions asserts the cache is empty after its own calls.
def _forget():
client = sys.modules.get("core.inference.mcp_client")
if client is not None:
client._tool_cache.clear()
client._probe_cooloff_until.clear()
+ if client._mcp_sessions:
+ try:
+ client.close_mcp_sessions(all_accounts = True)
+ except AttributeError:
+ # A test's monkeypatched stand-in sessions (SimpleNamespace) have no close(); its own
+ # monkeypatch puts the real dict back.
+ pass
_forget()
yield
diff --git a/studio/backend/tests/test_mcp_http_read_timeout.py b/studio/backend/tests/test_mcp_http_read_timeout.py
index 765ba9f10ae..aa5d4cea914 100644
--- a/studio/backend/tests/test_mcp_http_read_timeout.py
+++ b/studio/backend/tests/test_mcp_http_read_timeout.py
@@ -79,9 +79,12 @@ def test_tool_call_outlives_sdk_read_timeout(slow_server, monkeypatch, scope):
import mcp.shared._httpx_utils as httpx_utils
monkeypatch.setattr(httpx_utils, "MCP_DEFAULT_SSE_READ_TIMEOUT", 1.0)
- out = mcp_client.call_tool_sync(
- slow_server, None, "slow", {"seconds": 2}, timeout = 30, scope = scope
- )
+ try:
+ out = mcp_client.call_tool_sync(
+ slow_server, None, "slow", {"seconds": 2}, timeout = 30, scope = scope
+ )
+ finally:
+ mcp_client.close_mcp_sessions() # the scoped case caches a session
assert "slept 2" in out, out
diff --git a/studio/backend/tests/test_mcp_session_cache_reset_between_tests.py b/studio/backend/tests/test_mcp_session_cache_reset_between_tests.py
new file mode 100644
index 00000000000..14a38822fa7
--- /dev/null
+++ b/studio/backend/tests/test_mcp_session_cache_reset_between_tests.py
@@ -0,0 +1,37 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+"""A scoped MCP session one test leaves cached does not outlive that test.
+
+``mcp_client._mcp_sessions`` holds a scoped HTTP session until something closes it. The scoped case of
+test_tool_call_outlives_sdk_read_timeout left one behind, and test_unscoped_http_stays_one_shot, later on
+the same xdist worker, found it in the cache. The two tests below run in order on one worker: the first
+leaves a session cached the way a scoped call does, the second fails unless conftest closed it in between.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from core.inference import mcp_client
+
+pytestmark = pytest.mark.xdist_group("mcp_session_cache_reset_between_tests")
+
+
+class _Session:
+ closed = []
+
+ def close(self):
+ _Session.closed.append(self)
+
+
+def test_a_scoped_call_leaves_its_session_cached():
+ # Assigned, not monkeypatched: a scoped call stores its session the same way.
+ session = _Session()
+ mcp_client._mcp_sessions[("http://127.0.0.1:1/mcp", (), "chat-1", "owner")] = session
+ _Session.closed.clear()
+ assert mcp_client._mcp_sessions
+
+
+def test_the_next_test_starts_with_an_empty_cache_and_the_session_closed():
+ assert mcp_client._mcp_sessions == {}
+ assert len(_Session.closed) == 1