Skip to content

Commit 293d1b9

Browse files
committed
Fix sock_connect() to resolve addresses for correct socket family
Fixes #139.
1 parent a78e4d2 commit 293d1b9

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

tests/test_sockets.py

+16
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ async def run():
116116

117117
self.loop.run_until_complete(run())
118118

119+
def test_socket_ipv4_nameaddr(self):
120+
async def run():
121+
sock = socket.socket(socket.AF_INET)
122+
with sock:
123+
sock.setblocking(False)
124+
await self.loop.sock_connect(sock, ('localhost', 0))
125+
126+
with self.assertRaises(OSError):
127+
# Regression test: sock_connect(sock) wasn't calling
128+
# getaddrinfo() with `family=sock.family`, which resulted
129+
# in `socket.connect()` being called with an IPv6 address
130+
# for IPv4 sockets, which used to cause a TypeError.
131+
# Here we expect that that is fixed so we should get an
132+
# OSError instead.
133+
self.loop.run_until_complete(run())
134+
119135
def test_socket_blocking_error(self):
120136
self.loop.set_debug(True)
121137
sock = socket.socket()

uvloop/loop.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -2265,7 +2265,10 @@ cdef class Loop:
22652265
if sock.family == uv.AF_UNIX:
22662266
fut = self._sock_connect(sock, address)
22672267
else:
2268-
_, _, _, _, address = (await self.getaddrinfo(*address[:2]))[0]
2268+
addrs = await self.getaddrinfo(
2269+
*address[:2], family=sock.family)
2270+
2271+
_, _, _, _, address = addrs[0]
22692272
fut = self._sock_connect(sock, address)
22702273
if fut is not None:
22712274
await fut

0 commit comments

Comments
 (0)