Skip to content

πŸ™Œ OpenHands: AI-Driven Development

[remote runtime] poll runtime info to wait until alive instead of using long timeout (#4334)

Xingyao Wang343cc8710fe2
diff --git a/openhands/runtime/remote/runtime.py b/openhands/runtime/remote/runtime.py
index 7bd4b0990b..c259f16a2d 100644
--- a/openhands/runtime/remote/runtime.py
+++ b/openhands/runtime/remote/runtime.py
@@ -1,17 +1,12 @@
import os
import tempfile
import threading
+import time
from typing import Callable, Optional
from zipfile import ZipFile
import requests
from requests.exceptions import Timeout
-from tenacity import (
- retry,
- retry_if_exception_type,
- stop_after_attempt,
- wait_fixed,
-)
from openhands.core.config import AppConfig
from openhands.core.logger import openhands_logger as logger
@@ -41,7 +36,6 @@ from openhands.runtime.utils.request import (
send_request_with_retry,
)
from openhands.runtime.utils.runtime_build import build_runtime_image
-from openhands.utils.tenacity_stop import stop_if_should_exit
class RemoteRuntime(Runtime):
@@ -254,14 +248,50 @@ class RemoteRuntime(Runtime):
{'X-Session-API-Key': start_response['session_api_key']}
)
- @retry(
- stop=stop_after_attempt(60) | stop_if_should_exit(),
- wait=wait_fixed(2),
- retry=retry_if_exception_type(RuntimeError),
- reraise=True,
- )
def _wait_until_alive(self):
logger.info(f'Waiting for runtime to be alive at url: {self.runtime_url}')
+ # send GET request to /runtime/<id>
+ pod_running = False
+ max_not_found_count = 12 # 2 minutes
+ not_found_count = 0
+ while not pod_running:
+ runtime_info_response = send_request_with_retry(
+ self.session,
+ 'GET',
+ f'{self.config.sandbox.remote_runtime_api_url}/runtime/{self.runtime_id}',
+ timeout=5,
+ )
+ if runtime_info_response.status_code != 200:
+ raise RuntimeError(
+ f'Failed to get runtime status: {runtime_info_response.status_code}. Response: {runtime_info_response.text}'
+ )
+ runtime_data = runtime_info_response.json()
+ assert runtime_data['runtime_id'] == self.runtime_id
+ pod_status = runtime_data['pod_status']
+ logger.info(
+ f'Waiting for runtime pod to be active. Current status: {pod_status}'
+ )
+ if pod_status == 'Ready':
+ pod_running = True
+ break
+ elif pod_status == 'Not Found' and not_found_count < max_not_found_count:
+ not_found_count += 1
+ logger.info(
+ f'Runtime pod not found. Count: {not_found_count} / {max_not_found_count}'
+ )
+ elif (
+ pod_status == 'Failed'
+ or pod_status == 'Unknown'
+ or pod_status == 'Not Found'
+ ):
+ # clean up the runtime
+ self.close()
+ raise RuntimeError(
+ f'Runtime pod failed to start. Current status: {pod_status}'
+ )
+ # Pending otherwise - add proper sleep
+ time.sleep(10)
+
response = send_request_with_retry(
self.session,
'GET',