Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(workspace): clean up failed Docker initialization (#3098)
quifoxd8283ff6a26b
diff --git a/src/agentscope/app/workspace_manager/_docker_workspace_manager.py b/src/agentscope/app/workspace_manager/_docker_workspace_manager.pyindex c0f9af04e..e2153cdaf 100644--- a/src/agentscope/app/workspace_manager/_docker_workspace_manager.py+++ b/src/agentscope/app/workspace_manager/_docker_workspace_manager.py@@ -231,7 +231,12 @@ class DockerWorkspaceManager(default_mcps=self._default_mcps,skill_paths=self._skill_paths,)- await ws.initialize()+ try:+ await ws.initialize()+ except BaseException:+ # The container may already be running and nothing holds it.+ await self._dispose_prewarmed(ws)+ raisereturn ws# ── public API ────────────────────────────────────────────────diff --git a/tests/workspace_manager_docker_test.py b/tests/workspace_manager_docker_test.pyindex 9cd8cf6df..3a476f79f 100644--- a/tests/workspace_manager_docker_test.py+++ b/tests/workspace_manager_docker_test.py@@ -6,7 +6,7 @@ import asyncioimport osimport tempfilefrom unittest.async_case import IsolatedAsyncioTestCase-from unittest.mock import patch+from unittest.mock import AsyncMock, patchfrom agentscope.app.workspace_manager import (PrewarmConfig,@@ -169,6 +169,22 @@ class TestDockerWorkspaceManager(IsolatedAsyncioTestCase):await asyncio.sleep(0.05)self.assertEqual(len(_FakeWorkspace.created), 2)+ async def test_failed_initialize_closes_the_workspace(self) -> None:+ """A workspace whose ``initialize`` fails is closed, not leaked."""+ manager = DockerWorkspaceManager(self.basedir)+ with patch.object(+ _FakeWorkspace,+ "initialize",+ AsyncMock(side_effect=RuntimeError("gateway failed")),+ ):+ with self.assertRaisesRegex(RuntimeError, "gateway failed"):+ await manager.get_workspace("u1", "a1", "s1", "ws-id")++ self.assertListEqual(+ [ws.closed for ws in _FakeWorkspace.created],+ [True],+ )+async def test_aexit_closes_buffered_and_cached_workspaces(self) -> None:"""Neither the buffer nor the cache survives shutdown."""manager = DockerWorkspaceManager(