Skip to content

Commit

Permalink
feat: runtime improvements for rate-limit and 502/503/404 error (#5975)
Browse files Browse the repository at this point in the history
  • Loading branch information
xingyaoww authored Jan 3, 2025
1 parent ef8e04a commit f14f75b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
7 changes: 6 additions & 1 deletion evaluation/utils/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,12 @@ def _process_instance_wrapper(
+ '\n'
)
if isinstance(
e, (AgentRuntimeDisconnectedError, AgentRuntimeUnavailableError)
e,
(
AgentRuntimeDisconnectedError,
AgentRuntimeUnavailableError,
AgentRuntimeNotFoundError,
),
):
runtime_failure_count += 1
msg += f'Runtime disconnected error detected for instance {instance.instance_id}, runtime failure count: {runtime_failure_count}'
Expand Down
11 changes: 7 additions & 4 deletions openhands/runtime/impl/remote/remote_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from openhands.runtime.plugins import PluginRequirement
from openhands.runtime.utils.command import get_remote_startup_command
from openhands.runtime.utils.request import (
RequestHTTPError,
send_request,
)
from openhands.runtime.utils.runtime_build import build_runtime_image
Expand Down Expand Up @@ -367,10 +366,14 @@ def _send_action_server_request(self, method, url, **kwargs):
except requests.Timeout:
self.log('error', 'No response received within the timeout period.')
raise
except RequestHTTPError as e:
if e.response.status_code in (404, 502):
except requests.HTTPError as e:
if e.response.status_code == 404:
raise AgentRuntimeNotFoundError(
'Runtime unavailable: System resources may be exhausted due to running commands. This may be fixed by retrying.'
) from e
elif e.response.status_code == 502:
raise AgentRuntimeDisconnectedError(
f'{e.response.status_code} error while connecting to {self.runtime_url}'
'Runtime disconnected: System resources may be exhausted due to running commands. This may be fixed by retrying.'
) from e
elif e.response.status_code == 503:
self.log('warning', 'Runtime appears to be paused. Resuming...')
Expand Down
15 changes: 15 additions & 0 deletions openhands/runtime/utils/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from typing import Any

import requests
from tenacity import retry, retry_if_exception, stop_after_attempt, wait_exponential

from openhands.utils.tenacity_stop import stop_if_should_exit


class RequestHTTPError(requests.HTTPError):
Expand All @@ -18,6 +21,18 @@ def __str__(self) -> str:
return s


def is_rate_limit_error(exception):
return (
isinstance(exception, requests.HTTPError)
and exception.response.status_code == 429
)


@retry(
retry=retry_if_exception(is_rate_limit_error),
stop=stop_after_attempt(3) | stop_if_should_exit(),
wait=wait_exponential(multiplier=1, min=4, max=60),
)
def send_request(
session: requests.Session,
method: str,
Expand Down

0 comments on commit f14f75b

Please sign in to comment.