Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(mcp-server): stop cleanup_session from deadlocking on its own lock (#4134)

brekenb207fc93de52
diff --git a/libs/python/mcp-server/mcp_server/session_manager.py b/libs/python/mcp-server/mcp_server/session_manager.py
index 2e293e756c..28d64c96b0 100644
--- a/libs/python/mcp-server/mcp_server/session_manager.py
+++ b/libs/python/mcp-server/mcp_server/session_manager.py
@@ -228,23 +228,27 @@ class SessionManager:
session.is_shutting_down = True
return
- # Actually cleanup the session
- await self._force_cleanup_session(session_id)
+ # Actually cleanup the session (the lock is already held here)
+ await self._remove_session_locked(session_id)
async def _force_cleanup_session(self, session_id: str) -> None:
"""Force cleanup a session regardless of active tasks."""
async with self._session_lock:
- if session_id not in self._sessions:
- return
+ await self._remove_session_locked(session_id)
- session = self._sessions[session_id]
- logger.info(f"Cleaning up session: {session_id}")
+ async def _remove_session_locked(self, session_id: str) -> None:
+ """Release and remove a session. The caller must hold ``_session_lock``."""
+ if session_id not in self._sessions:
+ return
+
+ session = self._sessions[session_id]
+ logger.info(f"Cleaning up session: {session_id}")
- # Release computer back to pool
- await self._computer_pool.release(session.computer)
+ # Release computer back to pool
+ await self._computer_pool.release(session.computer)
- # Remove session
- del self._sessions[session_id]
+ # Remove session
+ del self._sessions[session_id]
async def _cleanup_loop(self) -> None:
"""Background task to cleanup idle sessions."""
diff --git a/libs/python/mcp-server/tests/test_session_manager.py b/libs/python/mcp-server/tests/test_session_manager.py
new file mode 100644
index 0000000000..57b93ee479
--- /dev/null
+++ b/libs/python/mcp-server/tests/test_session_manager.py
@@ -0,0 +1,39 @@
+"""Unit tests for the mcp-server SessionManager lifecycle."""
+
+import asyncio
+from unittest.mock import AsyncMock
+
+import pytest
+from mcp_server.session_manager import SessionManager
+
+
+async def _manager_with_session(session_id: str):
+ manager = SessionManager()
+ computer = AsyncMock()
+ manager._computer_pool.acquire = AsyncMock(return_value=computer)
+ manager._computer_pool._in_use.add(computer)
+ async with manager.get_session(session_id):
+ pass
+ return manager, computer
+
+
+async def test_cleanup_session_releases_idle_session():
+ manager, computer = await _manager_with_session("s1")
+
+ await asyncio.wait_for(manager.cleanup_session("s1"), timeout=2)
+
+ assert "s1" not in manager._sessions
+ assert computer in manager._computer_pool._available
+ assert not manager._session_lock.locked()
+
+
+async def test_cleanup_session_defers_while_tasks_are_active():
+ manager, computer = await _manager_with_session("s1")
+ await manager.register_task("s1", "task-1")
+
+ await asyncio.wait_for(manager.cleanup_session("s1"), timeout=2)
+
+ assert manager._sessions["s1"].is_shutting_down
+ assert computer not in manager._computer_pool._available