Skip to content

Exceptions not handled properly in the proxy's AsyncTunnelHTTPConnection leading to the leaks and pool starvation #1023

Description

@janosik47

The problem is described in #921

I made a proposal how to fix it in a comment

Activity

  1. rail-chamidullin-myriad commented on Dec 12, 2025

    @rail-chamidullin-myriad

    @janosik47 I am dealing exactly with the same issue and I got to the same conclusion as you. Any update on this? Is there some PR with the fix?

  2. janwijbrand commented on Apr 9, 2026

    @janwijbrand

    FWIW to move forward on this issue, I opted for a monkey patch in my code base. Hopefully only temporary. The monkey patch looks more or less like this:

    _original_handle_async_request = AsyncTunnelHTTPConnection.handle_async_request
    
    @functools.wraps(_original_handle_async_request)
    async def _patched_handle_async_request(self, request):
        """Wrapper around ``handle_async_request`` that cleans up the inner
        connection when a tunnel setup error (e.g. TLS handshake failure)
        leaves it as a zombie.
        """
        try:
            return await _original_handle_async_request(self, request)
        except BaseException:
            if not self._connected and not self._connection.is_closed():
                with AsyncShieldCancellation():
                    await self._connection.aclose()
            raise
    
    def apply():
        if (
            AsyncTunnelHTTPConnection.handle_async_request
            is not _patched_handle_async_request
        ):
            AsyncTunnelHTTPConnection.handle_async_request = (
                _patched_handle_async_request
            )
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions