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

localhost proxy resolved to IPv6 (in version 2.3.1) #2402

Closed
CheViana opened this issue Oct 25, 2017 · 6 comments
Closed

localhost proxy resolved to IPv6 (in version 2.3.1) #2402

CheViana opened this issue Oct 25, 2017 · 6 comments
Labels

Comments

@CheViana
Copy link

Long story short

I'm using aiohttp to make calls via proxy "http://localhost:9999". "etc/hosts" maps localhost to IPv4 and IPv6 addresses.
aiohttp version 2.2.5 resolves that proxy value to "127.0.0.1:9999", aiohttp version 2.3.1 - to value "::1:9999".
I can't pinpoint changes in 2.3.1 that caused this behavior.
I found a workaround, but I think IPv4 should be used by default in this case.

Expected behavior

Proxy "http://localhost:9999" got resolved to "127.0.0.1:9999" (IPv4 is default).

Actual behavior

Proxy "http://localhost:9999" got resolved to "::1:9999" (IPv6 is used for some reason).

Steps to reproduce

    async with aiohttp.ClientSession() as session:
            async with session.get(
                'https://github.com/aio-libs/aiohttp',
                proxy='http://localhost:9999'
            ) as response:
                response.raise_for_status()
                result = await response.read()
                return result

As far as I nailed it, line 785 in connector.py does host resolution:
hosts = yield from self._resolve_host(req.url.raw_host, req.port)

In version 2.2.5 hosts[0] equals:

{'hostname': 'localhost',
 'host': '127.0.0.1',
 'port': 9999,
 'family': <AddressFamily.AF_INET: 2>,
 'proto': 6,
 'flags': <AddressInfo.AI_NUMERICHOST: 4>}

In version 2.3.1 hosts[0] equals:

{'family': <AddressFamily.AF_INET6: 10>,
 'flags': <AddressInfo.AI_NUMERICHOST: 4>,
 'host': '::1',
 'hostname': 'localhost',
 'port': 9999,
 'proto': 6}

(aiodns is not used)

I've tried to call directly socket.getaddrinfo (AFAIK that is the function that does resolution under the hood):

>>> import socket
>>> socket.getaddrinfo('localhost', 9999, type=socket.SOCK_STREAM, family=socket.AF_INET)
[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 4140)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 4140))]
>>> socket.getaddrinfo('localhost', 9999, type=socket.SOCK_STREAM, family=socket.AF_INET6)
[(<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('::1', 4140, 0, 0))]

family parameter is the trick, but I'm not modifying it in request code and I don't see where in aiohttp code it's value changed...

Your environment

Linux. Python 3.6.
Contents of /etc/hosts:

127.0.0.1 localhost ...
::1 localhost ...

(aiodns is not used)

Workaround

Specify socket family explicitly:

    connector = aiohttp.TCPConnector(family=socket.AF_INET)
    async with aiohttp.ClientSession(connector=connector) as session:
            async with session.get(
                'https://github.com/aio-libs/aiohttp',
                proxy='http://localhost:9999'
            ) as response:
                response.raise_for_status()
                result = await response.read()
                return result
@asvetlov
Copy link
Member

I pretty sure aiohttp has no changes for it.
What is hosts list content? Not only hosts[0] but the whole list?

@johnlockwood-wf
Copy link

I hit this issue too

@asvetlov
Copy link
Member

asvetlov commented Nov 1, 2017

The issue should be fixed by #2424
aiohttp will connect to all resolved IPs: IPv4 and IPv6 should be processed in parallel.

@asvetlov
Copy link
Member

asvetlov commented Nov 1, 2017

Fixed by #2447

@johnlockwood-wf
Copy link

That did indeed fix it for me. Thanks!

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants