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(