π The fast, Pythonic way to build MCP servers and clients.
stdio: revert shared keep_alive=False sessions (#5180, #5215) (#5219)
nate nowack944b483ee7ce
diff --git a/fastmcp_slim/fastmcp/client/transports/stdio.py b/fastmcp_slim/fastmcp/client/transports/stdio.pyindex 9bbe550ba..8967934b7 100644--- a/fastmcp_slim/fastmcp/client/transports/stdio.py+++ b/fastmcp_slim/fastmcp/client/transports/stdio.py@@ -90,14 +90,7 @@ class StdioTransport(ClientTransport):self._active_sessions -= 1finally:if not self.keep_alive:- # Other clients may still be using this transport. Serialize- # final cleanup with connect() so a new client cannot acquire- # the session while it is being disconnected. Shielded because- # an exit caused by cancellation must still stop the subprocess.- with anyio.CancelScope(shield=True):- async with self._connect_lock:- if not self._active_sessions:- await self.disconnect()+ await self.disconnect()else:logger.debug("Stdio transport has keep_alive=True, not disconnecting")diff --git a/tests/client/test_stdio.py b/tests/client/test_stdio.pyindex 60f080f82..84232ce3e 100644--- a/tests/client/test_stdio.py+++ b/tests/client/test_stdio.py@@ -1,4 +1,5 @@import asyncio+import contextlibimport gcimport inspectimport os@@ -294,21 +295,33 @@ class TestKeepAlive:await wait_for_process_exit(pid)- async def test_shared_transport_stays_alive_until_last_client_exits(+ @pytest.mark.filterwarnings(+ "ignore:Exception ignored in.*coroutine:pytest.PytestUnraisableExceptionWarning"+ )+ async def test_client_abandoned_by_cancellation_does_not_wedge_transport(self, stdio_script):- transport = PythonStdioTransport(stdio_script, keep_alive=False)- first, second = Client(transport), Client(transport)+ """A Client left behind by a cancelled scope, once garbage-collected, must+ not leave the transport unusable for the next Client.- async with first:- pid = (await first.call_tool("pid")).data- async with second:- assert (await second.call_tool("pid")).data == pid+ Abandoning a Client this way leaks its session runner, a known issue that+ finalization reports as `coroutine ignored GeneratorExit`; that noise is+ tolerated here so the test checks only that the next Client connects.+ """+ transport = PythonStdioTransport(stdio_script, keep_alive=False)- assert await first.ping()- assert (await first.call_tool("pid")).data == pid+ with anyio.move_on_after(0.5):+ async with Client(transport) as abandoned:+ await abandoned.ping()+ await anyio.sleep(10)+ del abandoned+ await asyncio.sleep(0.1)+ with contextlib.suppress(RuntimeError):+ gc_collect_harder()- await wait_for_process_exit(pid)+ with anyio.fail_after(3):+ async with Client(transport) as client:+ assert (await client.call_tool("pid")).dataasync def test_cancelled_scope_still_stops_unshared_subprocess(self, stdio_script):transport = PythonStdioTransport(stdio_script, keep_alive=False)@@ -320,35 +333,6 @@ class TestKeepAlive:pid = int(result.content[0].text) # ty: ignore[unresolved-attribute]scope.cancel()- assert pid is not None- assert transport._connect_task is None- await wait_for_process_exit(pid)-- async def test_cancelled_client_leaves_shared_transport_connected(- self, stdio_script- ):- transport = PythonStdioTransport(stdio_script, keep_alive=False)- connected = asyncio.Event()-- async def cancelled_client():- async with Client(transport) as client:- await client.ping()- connected.set()- await asyncio.Event().wait()-- async with Client(transport) as client:- pid = (await client.call_tool("pid")).data- task = asyncio.create_task(cancelled_client())- try:- await connected.wait()- finally:- task.cancel()- with pytest.raises(asyncio.CancelledError):- await task-- assert await client.ping()- assert (await client.call_tool("pid")).data == pid-await wait_for_process_exit(pid)async def test_keep_alive_false_starts_new_session_across_multiple_calls(