From b9884f7609fa9642f9ef574ff095ceb172df1ac2 Mon Sep 17 00:00:00 2001 From: tofarr Date: Mon, 13 Jan 2025 09:15:23 -0700 Subject: [PATCH] Fixes for minor cases where FDs were not closed (#6228) --- openhands/runtime/builder/remote.py | 6 ++++-- openhands/runtime/impl/remote/remote_runtime.py | 4 +++- openhands/runtime/utils/request.py | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/openhands/runtime/builder/remote.py b/openhands/runtime/builder/remote.py index 2e2c67c5a8..eb35e13b5c 100644 --- a/openhands/runtime/builder/remote.py +++ b/openhands/runtime/builder/remote.py @@ -18,10 +18,12 @@ from openhands.utils.shutdown_listener import ( 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 ebc1a86b38..bb5135dff5 100644 --- a/openhands/runtime/impl/remote/remote_runtime.py +++ b/openhands/runtime/impl/remote/remote_runtime.py @@ -66,7 +66,9 @@ class RemoteRuntime(ActionExecutionClient): ) 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 2a0e17aa0f..e05a083e7b 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,