Skip to content

Unix connect errors use host/port which is not useful #4984

Closed
@zealws

Description

🐞 Describe the bug
A ConnectionRefusedError using UnixConnector results in an error message such as this:

aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host host:80 ssl:default [Connection refused]

The error message specifies the host/port of the connection. However, for Unix sockets, the host/port are not useful. They're still provided as part of the HTTP request, but they do not define the connection from client to server.

For unix sockets, ideally the error message would include the socket path on disk instead.

💡 To Reproduce

Run this code:
Note: don't create /tmp/server.sock to get a Connection Refused.

import aiohttp
import asyncio


async def main():
    async with aiohttp.ClientSession(
        connector=aiohttp.UnixConnector(path='/tmp/server.sock'),
    ) as session:
        r = await session.get('http://host/')
        print("Status:", r.status)
        print("Text:")
        print((await r.text()).strip())


asyncio.get_event_loop().run_until_complete(main())

💡 Expected behavior

Ideally, the exception would include the socket path on disk, which (for Unix sockets) is the primary way of identifying the connection, not host/port.

📋 Logs/tracebacks

Output of the sample script I pasted above:

Traceback (most recent call last):
  File "/home/zeal/dev/aiohttp/venv/lib/python3.8/site-packages/aiohttp-4.0.0a1-py3.8-linux-x86_64.egg/aiohttp/connector.py", line 1112, in _create_connection
    _, proto = await self._loop.create_unix_connection(
  File "/usr/lib/python3.8/asyncio/unix_events.py", line 244, in create_unix_connection
    await self.sock_connect(sock, path)
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 494, in sock_connect
    return await fut
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 499, in _sock_connect
    sock.connect(address)
ConnectionRefusedError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "client.py", line 15, in <module>
    asyncio.get_event_loop().run_until_complete(main())
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "client.py", line 9, in main
    r = await session.get('http://host/')
  File "/home/zeal/dev/aiohttp/venv/lib/python3.8/site-packages/aiohttp-4.0.0a1-py3.8-linux-x86_64.egg/aiohttp/client.py", line 446, in _request
    conn = await self._connector.connect(
  File "/home/zeal/dev/aiohttp/venv/lib/python3.8/site-packages/aiohttp-4.0.0a1-py3.8-linux-x86_64.egg/aiohttp/connector.py", line 506, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/home/zeal/dev/aiohttp/venv/lib/python3.8/site-packages/aiohttp-4.0.0a1-py3.8-linux-x86_64.egg/aiohttp/connector.py", line 1115, in _create_connection
    raise ClientConnectorError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host host:80 ssl:default [Connection refused]

📋 Your version of the Python

$ python --version
Python 3.8.5

📋 Your version of the aiohttp/yarl/multidict distributions

Installed from source at commit 90acab1, so the versions below are not correct, but attaching them anyway.

$ python -m pip show aiohttp
Name: aiohttp
Version: 4.0.0a1
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: fafhrd91@gmail.com
License: Apache 2
Location: /home/zeal/dev/aiohttp
Requires: attrs, chardet, multidict, async-timeout, yarl, typing-extensions
Required-by: 
$ python -m pip show multidict
Name: multidict
Version: 4.7.6
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /home/zeal/dev/aiohttp/venv/lib/python3.8/site-packages/multidict-4.7.6-py3.8-linux-x86_64.egg
Requires: 
Required-by: yarl, aiohttp

$ python -m pip show yarl
Name: yarl
Version: 1.4.2
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /home/zeal/dev/aiohttp/venv/lib/python3.8/site-packages
Requires: idna, multidict
Required-by: aiohttp

📋 Additional context

None

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions