Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Catch timeout errors in `is_running`
Jakub Novakfd50e0a4c804
diff --git a/packages/python-sdk/e2b/exceptions.py b/packages/python-sdk/e2b/exceptions.pyindex 8c39ab5d6..d785367ab 100644--- a/packages/python-sdk/e2b/exceptions.py+++ b/packages/python-sdk/e2b/exceptions.py@@ -4,6 +4,18 @@ def format_sandbox_timeout_exception(message: str):)+def format_request_timeout_error() -> Exception:+ return TimeoutException(+ f"Request timed out — the 'request_timeout' option can be used to increase this timeout",+ )+++def format_execution_timeout_error() -> Exception:+ return TimeoutException(+ f"Execution timed out — the 'timeout' option can be used to increase this timeout",+ )++class SandboxException(Exception):"""Raised when a sandbox exception occurs.diff --git a/packages/python-sdk/e2b/sandbox_async/main.py b/packages/python-sdk/e2b/sandbox_async/main.pyindex a352a29ac..fafd9df31 100644--- a/packages/python-sdk/e2b/sandbox_async/main.py+++ b/packages/python-sdk/e2b/sandbox_async/main.py@@ -3,6 +3,7 @@ import httpxfrom typing import Optional, Dict, Literal, overload+from e2b.exceptions import format_request_timeout_errorfrom e2b.sandbox.utils import class_method_variantfrom e2b.connection_config import ConnectionConfigfrom e2b.envd.api import (@@ -80,14 +81,19 @@ class AsyncSandbox(SandboxSetup, SandboxApi):async def is_running(self, request_timeout: Optional[float] = None) -> Literal[True]:- r = await self._envd_api.get(- ENVD_API_HEALTH_ROUTE,- timeout=self.connection_config.get_request_timeout(request_timeout),- )+ try:+ r = await self._envd_api.get(+ ENVD_API_HEALTH_ROUTE,+ timeout=self.connection_config.get_request_timeout(request_timeout),+ )++ err = await ahandle_envd_api_exception(r)++ if err:+ raise err- err = await ahandle_envd_api_exception(r)- if err:- raise err+ except httpx.TimeoutException:+ raise format_request_timeout_error()return Truediff --git a/packages/python-sdk/e2b/sandbox_sync/main.py b/packages/python-sdk/e2b/sandbox_sync/main.pyindex bc5c353ed..5270776e9 100644--- a/packages/python-sdk/e2b/sandbox_sync/main.py+++ b/packages/python-sdk/e2b/sandbox_sync/main.py@@ -1,5 +1,4 @@import logging-import httpcoreimport httpxfrom typing import Optional, Dict, Literal, overload@@ -10,7 +9,7 @@ from e2b.envd.api import (handle_envd_api_exception,ENVD_API_HEALTH_ROUTE,)-from e2b.exceptions import SandboxException+from e2b.exceptions import SandboxException, format_request_timeout_errorfrom e2b.sandbox.main import SandboxSetupfrom e2b.sandbox_sync.filesystem.filesystem import Filesystemfrom e2b.sandbox_sync.process.main import Process@@ -117,14 +116,19 @@ class Sandbox(SandboxSetup, SandboxApi):)def is_running(self, request_timeout: Optional[float] = None) -> Literal[True]:- r = self._envd_api.get(- ENVD_API_HEALTH_ROUTE,- timeout=self.connection_config.get_request_timeout(request_timeout),- )+ try:+ r = self._envd_api.get(+ ENVD_API_HEALTH_ROUTE,+ timeout=self.connection_config.get_request_timeout(request_timeout),+ )++ err = handle_envd_api_exception(r)++ if err:+ raise err- err = handle_envd_api_exception(r)- if err:- raise err+ except httpx.TimeoutException:+ raise format_request_timeout_error()return True