diff --git a/openhands/runtime/builder/remote.py b/openhands/runtime/builder/remote.py index 2e2c67c5a8f3..eb35e13b5c1c 100644 --- a/openhands/runtime/builder/remote.py +++ b/openhands/runtime/builder/remote.py @@ -18,10 +18,12 @@ class RemoteRuntimeBuilder(RuntimeBuilder): """This class interacts with the remote Runtime API for building and managing container images.""" - def __init__(self, api_url: str, api_key: str): + def __init__( + self, api_url: str, api_key: str, session: requests.Session | None = None + ): self.api_url = api_url self.api_key = api_key - self.session = requests.Session() + self.session = session or requests.Session() self.session.headers.update({'X-API-Key': self.api_key}) def build( diff --git a/openhands/runtime/impl/remote/remote_runtime.py b/openhands/runtime/impl/remote/remote_runtime.py index ebc1a86b384b..bb5135dff530 100644 --- a/openhands/runtime/impl/remote/remote_runtime.py +++ b/openhands/runtime/impl/remote/remote_runtime.py @@ -66,7 +66,9 @@ def __init__( ) self.runtime_builder = RemoteRuntimeBuilder( - self.config.sandbox.remote_runtime_api_url, self.config.sandbox.api_key + self.config.sandbox.remote_runtime_api_url, + self.config.sandbox.api_key, + self.session, ) self.runtime_id: str | None = None self.runtime_url: str | None = None diff --git a/openhands/runtime/utils/request.py b/openhands/runtime/utils/request.py index 2a0e17aa0fba..e05a083e7b0d 100644 --- a/openhands/runtime/utils/request.py +++ b/openhands/runtime/utils/request.py @@ -48,6 +48,8 @@ def send_request( _json = response.json() except (requests.exceptions.JSONDecodeError, json.decoder.JSONDecodeError): _json = None + finally: + response.close() raise RequestHTTPError( e, response=e.response,