fix(anthropic): guard httpx finalizers (#37064)

This commit is contained in:
open-swe[bot]
2026-04-28 20:59:00 -04:00
committed by GitHub
parent dfb8a6184c
commit 97ac1d34d0
2 changed files with 20 additions and 6 deletions

View File

@@ -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

View File

@@ -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__()