fix httpx client resource leak with proper async cleanup

This commit is contained in:
Shoubhit Dash 2025-10-30 15:01:44 +05:30
parent a185dc252b
commit 2e8c19034a
2 changed files with 17 additions and 0 deletions

View file

@ -42,6 +42,10 @@ class MemoryClient:
async def __aexit__(self, *args):
"""Async context manager exit - cleanup client."""
await self.aclose()
async def aclose(self):
"""Close the httpx client."""
if self._client:
await self._client.aclose()
self._client = None

View file

@ -105,6 +105,11 @@ class SupermemoryOpenAIWrapper:
self._executor.shutdown(wait=True)
self._executor = None
async def aclose(self) -> None:
"""Async cleanup of resources."""
await self.memory_client.aclose()
self.close()
def __enter__(self):
"""Context manager entry."""
return self
@ -113,6 +118,14 @@ class SupermemoryOpenAIWrapper:
"""Context manager exit - cleanup resources."""
self.close()
async def __aenter__(self):
"""Async context manager entry."""
return self
async def __aexit__(self, *args):
"""Async context manager exit - cleanup resources."""
await self.aclose()
async def _add_memory_if_needed(self, messages: List[ChatCompletionMessageParam]) -> None:
"""Add conversation to memory if configured to do so."""
if self.options.add_memory != "always":