Skip to content

Commit 9bef654

Browse files
fix: make AgentGraph destructor loop-safe to prevent connection leaks
1 parent e0457c0 commit 9bef654

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

src/agent/graph.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ def __init__(
4343

4444
def __del__(self) -> None:
4545
if self.pool:
46-
asyncio.run(self.close_pool())
46+
try:
47+
loop = asyncio.get_running_loop()
48+
if loop.is_running():
49+
loop.create_task(self.close_pool())
50+
else:
51+
asyncio.run(self.close_pool())
52+
except RuntimeError:
53+
# No event loop is running
54+
asyncio.run(self.close_pool())
4755

4856
async def initialize(self) -> dict[str, CompiledStateGraph]:
4957
checkpointer: BaseCheckpointSaver[str] = await self.create_checkpointer()

0 commit comments

Comments
 (0)