Просмотр исходного кода

Handle errors when starting session (#4134)

Robert Brennan 1 год назад
Родитель
Сommit
ec1a86f150
1 измененных файлов с 22 добавлено и 8 удалено
  1. 22 8
      openhands/server/session/agent_session.py

+ 22 - 8
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(