mirror of
https://github.com/hwchase17/langchain.git
synced 2026-05-05 11:12:11 +00:00
fix(anthropic): guard httpx finalizers (#37064)
This commit is contained in:
@@ -22,10 +22,9 @@ class _SyncHttpxClientWrapper(anthropic.DefaultHttpxClient):
|
||||
"""Borrowed from anthropic._base_client."""
|
||||
|
||||
def __del__(self) -> None:
|
||||
if self.is_closed:
|
||||
return
|
||||
|
||||
try:
|
||||
if self.is_closed:
|
||||
return
|
||||
self.close()
|
||||
except Exception: # noqa: S110
|
||||
pass
|
||||
@@ -35,10 +34,9 @@ class _AsyncHttpxClientWrapper(anthropic.DefaultAsyncHttpxClient):
|
||||
"""Borrowed from anthropic._base_client."""
|
||||
|
||||
def __del__(self) -> None:
|
||||
if self.is_closed:
|
||||
return
|
||||
|
||||
try:
|
||||
if self.is_closed:
|
||||
return
|
||||
# TODO(someday): support non asyncio runtimes here
|
||||
asyncio.get_running_loop().create_task(self.aclose())
|
||||
except Exception: # noqa: S110
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from langchain_anthropic._client_utils import (
|
||||
_AsyncHttpxClientWrapper,
|
||||
_get_default_async_httpx_client,
|
||||
_get_default_httpx_client,
|
||||
_SyncHttpxClientWrapper,
|
||||
)
|
||||
|
||||
|
||||
@@ -60,3 +62,17 @@ def test_client_proxy_none_value() -> None:
|
||||
# Both should be created successfully with None proxy
|
||||
assert sync_client is not None
|
||||
assert async_client is not None
|
||||
|
||||
|
||||
def test_sync_client_wrapper_del_handles_uninitialized_client() -> None:
|
||||
"""Test sync wrapper finalizer handles clients without initialized state."""
|
||||
client = _SyncHttpxClientWrapper.__new__(_SyncHttpxClientWrapper)
|
||||
|
||||
client.__del__()
|
||||
|
||||
|
||||
async def test_async_client_wrapper_del_handles_uninitialized_client() -> None:
|
||||
"""Test async wrapper finalizer handles clients without initialized state."""
|
||||
client = _AsyncHttpxClientWrapper.__new__(_AsyncHttpxClientWrapper)
|
||||
|
||||
client.__del__()
|
||||
|
||||
Reference in New Issue
Block a user