Skip to content

Commit

Permalink
Fix: File Descriptor leak (#6883)
Browse files Browse the repository at this point in the history
  • Loading branch information
tofarr authored Feb 21, 2025
1 parent bf77da7 commit a20f299
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions openhands/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from litellm.exceptions import (
RateLimitError,
)
from litellm.llms.custom_httpx.http_handler import HTTPHandler
from litellm.types.utils import CostPerToken, ModelResponse, Usage
from litellm.utils import create_pretrained_tokenizer

Expand Down Expand Up @@ -231,8 +232,17 @@ def wrapper(*args, **kwargs):
# Record start time for latency measurement
start_time = time.time()

# we don't support streaming here, thus we get a ModelResponse
resp: ModelResponse = self._completion_unwrapped(*args, **kwargs)
# LiteLLM currently have an issue where HttpHandlers are being created but not
# closed. We have submitted a PR to them, (https://github.com/BerriAI/litellm/pull/8711)
# and their dev team say they are in the process of a refactor that will fix this.
# In the meantime, we manage the lifecycle of the HTTPHandler manually.
handler = HTTPHandler(timeout=self.config.timeout)
kwargs['client'] = handler
try:
# we don't support streaming here, thus we get a ModelResponse
resp: ModelResponse = self._completion_unwrapped(*args, **kwargs)
finally:
handler.close()

# Calculate and record latency
latency = time.time() - start_time
Expand Down

0 comments on commit a20f299

Please sign in to comment.