Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errno attribute in the aiohttp.ClientConnectorError exception is lost after 3.10.0 when multiple connection attempts fail #76

Closed
1 task done
x0day opened this issue Aug 7, 2024 · 5 comments · Fixed by #77
Labels
bug Something isn't working

Comments

@x0day
Copy link

x0day commented Aug 7, 2024

Describe the bug

After aiohttp version 3.10.0, the errno attribute in the aiohttp.ClientConnectorError exception has changed.

To Reproduce

the case is worked before 3.10.0.

pip install pytest pytest-httpserver
import aiohttp
import pytest
import errno
from pytest_httpserver import HTTPServer

pytestmark = pytest.mark.asyncio


async def test_session_will_raise_connection_refused_error(
    httpserver: HTTPServer
):
    httpserver.expect_request("/foobar").respond_with_data("OK")
    httpserver.stop()

    session = aiohttp.ClientSession()
    try:
        await session.get(httpserver.url_for("/foobar"))
    except aiohttp.ClientConnectorError as exc:
        # check port closed error.
        assert exc.errno == errno.ECONNREFUSED
    finally:
        await session.close()

Expected behavior

testcase is passed

Logs/tracebacks

None != 61

Expected :61
Actual   :None
<Click to see difference>

../../../Library/Caches/pypoetry/virtualenvs/workspace/lib/python3.11/site-packages/aiohttp/connector.py:1036: in _wrap_create_connection
    sock = await aiohappyeyeballs.start_connection(
../../../Library/Caches/pypoetry/virtualenvs/workspace/lib/python3.11/site-packages/aiohappyeyeballs/impl.py:112: in start_connection
    raise OSError(
E   OSError: Multiple exceptions: [Errno 61] Connect call failed ('::1', 50139, 0, 0), [Errno 61] Connect call failed ('127.0.0.1', 50139)

The above exception was the direct cause of the following exception:
test4.py:17: in test_session_will_raise_connection_refused_error
    await session.get(httpserver.url_for("/foobar"))
../../../Library/Caches/pypoetry/virtualenvs/workspace/lib/python3.11/site-packages/aiohttp/client.py:648: in _request
    conn = await self._connector.connect(
../../../Library/Caches/pypoetry/virtualenvs/workspace/lib/python3.11/site-packages/aiohttp/connector.py:546: in connect
    proto = await self._create_connection(req, traces, timeout)
../../../Library/Caches/pypoetry/virtualenvs/workspace/lib/python3.11/site-packages/aiohttp/connector.py:954: in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
../../../Library/Caches/pypoetry/virtualenvs/workspace/lib/python3.11/site-packages/aiohttp/connector.py:1313: in _create_direct_connection
    raise last_exc
../../../Library/Caches/pypoetry/virtualenvs/workspace/lib/python3.11/site-packages/aiohttp/connector.py:1282: in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
../../../Library/Caches/pypoetry/virtualenvs/workspace/lib/python3.11/site-packages/aiohttp/connector.py:1051: in _wrap_create_connection
    raise client_error(req.connection_key, exc) from exc
E   aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host localhost:50139 ssl:default [None]

During handling of the above exception, another exception occurred:
test4.py:20: in test_session_will_raise_connection_refused_error
    assert exc.errno == errno.ECONNREFUSED
E   assert None == 61
E    +  where None = ClientConnectorError(ConnectionKey(host='localhost', port=50139, is_ssl=False, ssl=True, proxy=None, proxy_auth=None, proxy_headers_hash=None), OSError("Multiple exceptions: [Errno 61] Connect call failed ('::1', 50139, 0, 0), [Errno 61] Connect call failed ('127.0.0.1', 50139)")).errno
E    +  and   61 = errno.ECONNREFUSED

Python Version

$ python --version
Python 3.11.8

aiohttp Version

$ python -m pip show aiohttp
Name: aiohttp
Version: 3.10.0
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: 
Author-email: 
License: Apache 2
Requires: aiohappyeyeballs, aiosignal, attrs, frozenlist, multidict, yarl
Required-by: aiohttp-socks, aioresponses

multidict Version

$ python -m pip show multidict

yarl Version

$ python -m pip show yarl
Name: multidict
Version: 6.0.5
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2

OS

macos

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@x0day x0day added the bug Something isn't working label Aug 7, 2024
@bdraco
Copy link
Member

bdraco commented Aug 7, 2024

@bdraco
Copy link
Member

bdraco commented Aug 7, 2024

I agree that behavior is less than ideal, but I don't have a good suggestion on how to improve it in cpython. Would you please open an issue at https://github.com/python/cpython/issues/new/choose Perhaps someone will have an idea on how to improve that.

@bdraco bdraco changed the title errno attribute in the aiohttp.ClientConnectorError exception is lost after 3.10.0 errno attribute in the aiohttp.ClientConnectorError exception is lost after 3.10.0 when multiple connection attempts fail Aug 7, 2024
@bdraco
Copy link
Member

bdraco commented Aug 7, 2024

Ideally cpython would raise an ExceptionGroup but I think thats a breaking change.

@bdraco
Copy link
Member

bdraco commented Aug 7, 2024

It looks like 3.12+ allows getting all the exceptions

python/cpython#93974

@bdraco
Copy link
Member

bdraco commented Aug 7, 2024

We could backport that change to aiohappyeyeballs but there is another issue since ExceptionGroup is 3.11+ only.

@bdraco bdraco transferred this issue from aio-libs/aiohttp Aug 7, 2024
@bdraco bdraco closed this as completed in #77 Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants