From d8b89f897cd9468a9158252f67667d71c923b9a1 Mon Sep 17 00:00:00 2001 From: Tim O'Farrell Date: Wed, 12 Feb 2025 15:29:01 +0000 Subject: [PATCH 1/2] Agent session no longer stuck in starting on raised exception --- openhands/server/session/agent_session.py | 69 ++++++++++++----------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/openhands/server/session/agent_session.py b/openhands/server/session/agent_session.py index 298474b884a2..e5998065cf23 100644 --- a/openhands/server/session/agent_session.py +++ b/openhands/server/session/agent_session.py @@ -97,42 +97,43 @@ async def start( logger.warning('Session closed before starting') return self._starting = True - self._started_at = time.time() - self._create_security_analyzer(config.security.security_analyzer) - await self._create_runtime( - runtime_name=runtime_name, - config=config, - agent=agent, - github_token=github_token, - selected_repository=selected_repository, - ) - - self.controller = self._create_controller( - agent, - config.security.confirmation_mode, - max_iterations, - max_budget_per_task=max_budget_per_task, - agent_to_llm_config=agent_to_llm_config, - agent_configs=agent_configs, - ) - if github_token: - self.event_stream.set_secrets( - { - 'github_token': github_token.get_secret_value(), - } - ) - if initial_message: - self.event_stream.add_event(initial_message, EventSource.USER) - self.event_stream.add_event( - ChangeAgentStateAction(AgentState.RUNNING), EventSource.ENVIRONMENT - ) - else: - self.event_stream.add_event( - ChangeAgentStateAction(AgentState.AWAITING_USER_INPUT), - EventSource.ENVIRONMENT, + try: + self._started_at = time.time() + self._create_security_analyzer(config.security.security_analyzer) + await self._create_runtime( + runtime_name=runtime_name, + config=config, + agent=agent, + github_token=github_token, + selected_repository=selected_repository, ) - self._starting = False + self.controller = self._create_controller( + agent, + config.security.confirmation_mode, + max_iterations, + max_budget_per_task=max_budget_per_task, + agent_to_llm_config=agent_to_llm_config, + agent_configs=agent_configs, + ) + if github_token: + self.event_stream.set_secrets( + { + 'github_token': github_token.get_secret_value(), + } + ) + if initial_message: + self.event_stream.add_event(initial_message, EventSource.USER) + self.event_stream.add_event( + ChangeAgentStateAction(AgentState.RUNNING), EventSource.ENVIRONMENT + ) + else: + self.event_stream.add_event( + ChangeAgentStateAction(AgentState.AWAITING_USER_INPUT), + EventSource.ENVIRONMENT, + ) + finally: + self._starting = False async def close(self): """Closes the Agent session""" From 6abe84e40de8ca81593bf8193c18265edfd728cc Mon Sep 17 00:00:00 2001 From: Tim O'Farrell Date: Wed, 12 Feb 2025 17:11:47 +0000 Subject: [PATCH 2/2] Better error handling Sometimes the call to start_or_attach_to_runtime throws unkown errors, and in this case the http session was getting left in an open state. We now close the runtime in this case --- openhands/runtime/impl/remote/remote_runtime.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/openhands/runtime/impl/remote/remote_runtime.py b/openhands/runtime/impl/remote/remote_runtime.py index 068461cc61fb..56b0ec28caa8 100644 --- a/openhands/runtime/impl/remote/remote_runtime.py +++ b/openhands/runtime/impl/remote/remote_runtime.py @@ -92,8 +92,9 @@ def _get_action_execution_server_host(self): async def connect(self): try: await call_sync_from_async(self._start_or_attach_to_runtime) - except AgentRuntimeNotReadyError: - self.log('error', 'Runtime failed to start, timed out before ready') + except Exception: + self.close() + self.log('error', 'Runtime failed to start') raise await call_sync_from_async(self.setup_initial_env) self._runtime_initialized = True