await closing the controller (#1751)

* await closing the controller

* Update manager.py

* Cleanly exit

* Update agent.py

---------

Co-authored-by: Yufan Song <33971064+yufansong@users.noreply.github.com>
Co-authored-by: Jim Su <jimsu@protonmail.com>
This commit is contained in:
Pete Stenger
2024-05-13 13:34:03 -05:00
committed by GitHub
parent 755a4072b6
commit a48b02207f
2 changed files with 16 additions and 4 deletions

View File

@@ -138,6 +138,10 @@ class AgentUnit:
await self.send(event.to_dict())
return
def close(self):
async def close(self):
"""Cleanly exits an AgentUnit.
Execution awaited by the AgentManager.
"""
if self.controller is not None:
self.controller.close()
await self.controller.close()

View File

@@ -1,4 +1,4 @@
import atexit
import asyncio, atexit
from opendevin.core.logger import opendevin_logger as logger
from opendevin.server.session import session_manager
@@ -35,6 +35,14 @@ class AgentManager:
await self.sid_to_agent[sid].dispatch(action, data)
def close(self):
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self._close())
async def _close(self):
logger.info(f'Closing {len(self.sid_to_agent)} agent(s)...')
for sid, agent in self.sid_to_agent.items():
agent.close()
await agent.close()