Skip to content

Commit

Permalink
Fix sock_connect() to resolve addresses for correct socket family
Browse files Browse the repository at this point in the history
Fixes #139.
  • Loading branch information
1st1 committed May 25, 2018
1 parent a455af3 commit ce2bd4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
16 changes: 16 additions & 0 deletions tests/test_sockets.py
Expand Up @@ -116,6 +116,22 @@ async def run():

self.loop.run_until_complete(run())

def test_socket_ipv4_nameaddr(self):
async def run():
sock = socket.socket(socket.AF_INET)
with sock:
sock.setblocking(False)
await self.loop.sock_connect(sock, ('localhost', 0))

with self.assertRaises(OSError):
# Regression test: sock_connect(sock) wasn't calling
# getaddrinfo() with `family=sock.family`, which resulted
# in `socket.connect()` being called with an IPv6 address
# for IPv4 sockets, which used to cause a TypeError.
# Here we expect that that is fixed so we should get an
# OSError instead.
self.loop.run_until_complete(run())

def test_socket_blocking_error(self):
self.loop.set_debug(True)
sock = socket.socket()
Expand Down
5 changes: 4 additions & 1 deletion uvloop/loop.pyx
Expand Up @@ -2272,7 +2272,10 @@ cdef class Loop:
if sock.family == uv.AF_UNIX:
fut = self._sock_connect(sock, address)
else:
_, _, _, _, address = (await self.getaddrinfo(*address[:2]))[0]
addrs = await self.getaddrinfo(
*address[:2], family=sock.family)

_, _, _, _, address = addrs[0]
fut = self._sock_connect(sock, address)
if fut is not None:
await fut
Expand Down

0 comments on commit ce2bd4f

Please sign in to comment.