From ec1a86f150392a3ed2a439e72b2a949f69adc381 Mon Sep 17 00:00:00 2001 From: Robert Brennan Date: Tue, 1 Oct 2024 12:40:09 -0400 Subject: [PATCH] Handle errors when starting session (#4134) --- openhands/server/session/agent_session.py | 30 +++++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/openhands/server/session/agent_session.py b/openhands/server/session/agent_session.py index 37286cc6b1..3c411c1e65 100644 --- a/openhands/server/session/agent_session.py +++ b/openhands/server/session/agent_session.py @@ -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(