Handle errors when starting session (#4134)

This commit is contained in:
Robert Brennan
2024-10-01 12:40:09 -04:00
committed by GitHub
parent 926af7f5fd
commit ec1a86f150

View File

@@ -1,4 +1,5 @@
import asyncio
import concurrent.futures
from threading import Thread
from typing import Callable, Optional
@@ -75,6 +76,13 @@ class AgentSession:
self.thread = Thread(target=self._run, daemon=True)
self.thread.start()
def coro_callback(task):
fut: concurrent.futures.Future = concurrent.futures.Future()
try:
fut.set_result(task.result())
except Exception as e:
logger.error(f'Error starting session: {e}')
coro = self._start(
runtime_name,
config,
@@ -85,7 +93,9 @@ class AgentSession:
agent_configs,
status_message_callback,
)
asyncio.run_coroutine_threadsafe(coro, self.loop) # type: ignore
asyncio.run_coroutine_threadsafe(coro, self.loop).add_done_callback(
coro_callback
) # type: ignore
async def _start(
self,
@@ -172,13 +182,17 @@ class AgentSession:
logger.info(f'Initializing runtime `{runtime_name}` now...')
runtime_cls = get_runtime_cls(runtime_name)
self.runtime = runtime_cls(
config=config,
event_stream=self.event_stream,
sid=self.sid,
plugins=agent.sandbox_plugins,
status_message_callback=status_message_callback,
)
try:
self.runtime = runtime_cls(
config=config,
event_stream=self.event_stream,
sid=self.sid,
plugins=agent.sandbox_plugins,
status_message_callback=status_message_callback,
)
except Exception as e:
logger.error(f'Runtime initialization failed: {e}')
raise
if self.runtime is not None:
logger.debug(