Skip to content

fix(client): remove unsafe __del__ method from AsyncHttpxClientWrapper - #1798

Open
vjaudir8123-debug wants to merge 1 commit into
anthropics:mainfrom
vjaudir8123-debug:fix/async-client-loop-corruption
Open

fix(client): remove unsafe __del__ method from AsyncHttpxClientWrapper#1798
vjaudir8123-debug wants to merge 1 commit into
anthropics:mainfrom
vjaudir8123-debug:fix/async-client-loop-corruption

Conversation

@vjaudir8123-debug

Copy link
Copy Markdown
Contributor

Summary

The __del__ method in AsyncHttpxClientWrapper was attempting to close the client by calling asyncio.get_running_loop().create_task(self.aclose()).

Since garbage collection can run on any thread at any time, this approach is fundamentally unsafe in Python asyncio:

  1. If it triggers in a background thread without an event loop, it throws a RuntimeError (which is swallowed) and leaks the connection without warning.
  2. If it does manage to find an event loop from another thread, calling create_task is thread-unsafe and can corrupt the entire async loop.

This PR removes the unsafe __del__ method. The connection lifecycle should be managed explicitly by the consumer (via async with or explicit .close()).

The `__del__` method was attempting to close the client by calling `asyncio.get_running_loop().create_task(self.aclose())`. Since garbage collection can run on any thread at any time, this could trigger in a background thread without an event loop (swallowing a `RuntimeError` and leaking the connection) or, if it did find a loop, it was thread-unsafe and could corrupt the asyncio loop. The connection must be explicitly managed by the consumer.

Co-authored-by: Claude <noreply@anthropic.com>
@vjaudir8123-debug
vjaudir8123-debug requested a review from a team as a code owner August 4, 2026 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant