From ce2bd4fbf749cbe2ffbf7da8bed09959ca5c5742 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Thu, 24 May 2018 22:54:30 -0400 Subject: [PATCH] Fix sock_connect() to resolve addresses for correct socket family Fixes #139. --- tests/test_sockets.py | 16 ++++++++++++++++ uvloop/loop.pyx | 5 ++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/test_sockets.py b/tests/test_sockets.py index 7f73f266..8036cce1 100644 --- a/tests/test_sockets.py +++ b/tests/test_sockets.py @@ -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() diff --git a/uvloop/loop.pyx b/uvloop/loop.pyx index 696bb42d..49032968 100644 --- a/uvloop/loop.pyx +++ b/uvloop/loop.pyx @@ -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