Skip to content

Commit

Permalink
GH-86508: skip binding to local addresses of different family in `asy…
Browse files Browse the repository at this point in the history
…ncio.open_connection` (#100615)
  • Loading branch information
kumaraditya303 authored Jan 4, 2023
1 parent a286caa commit ba8dcdb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,10 @@ async def _connect_sock(self, exceptions, addr_info, local_addr_infos=None):
sock = socket.socket(family=family, type=type_, proto=proto)
sock.setblocking(False)
if local_addr_infos is not None:
for _, _, _, _, laddr in local_addr_infos:
for lfamily, _, _, _, laddr in local_addr_infos:
# skip local addresses of different family
if lfamily != family:
continue
try:
sock.bind(laddr)
break
Expand All @@ -974,7 +977,10 @@ async def _connect_sock(self, exceptions, addr_info, local_addr_infos=None):
exc = OSError(exc.errno, msg)
my_exceptions.append(exc)
else: # all bind attempts failed
raise my_exceptions.pop()
if my_exceptions:
raise my_exceptions.pop()
else:
raise OSError(f"no matching local address with {family=} found")
await self.sock_connect(sock, address)
return sock
except OSError as exc:
Expand Down
41 changes: 41 additions & 0 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,47 @@ def test_create_connection_local_addr(self):
self.assertEqual(port, expected)
tr.close()

def test_create_connection_local_addr_skip_different_family(self):
# See https://github.com/python/cpython/issues/86508
port1 = socket_helper.find_unused_port()
port2 = socket_helper.find_unused_port()
getaddrinfo_orig = self.loop.getaddrinfo

async def getaddrinfo(host, port, *args, **kwargs):
if port == port2:
return [(socket.AF_INET6, socket.SOCK_STREAM, 0, '', ('::1', 0, 0, 0)),
(socket.AF_INET, socket.SOCK_STREAM, 0, '', ('127.0.0.1', 0))]
return await getaddrinfo_orig(host, port, *args, **kwargs)

self.loop.getaddrinfo = getaddrinfo

f = self.loop.create_connection(
lambda: MyProto(loop=self.loop),
'localhost', port1, local_addr=('localhost', port2))

with self.assertRaises(OSError):
self.loop.run_until_complete(f)

def test_create_connection_local_addr_nomatch_family(self):
# See https://github.com/python/cpython/issues/86508
port1 = socket_helper.find_unused_port()
port2 = socket_helper.find_unused_port()
getaddrinfo_orig = self.loop.getaddrinfo

async def getaddrinfo(host, port, *args, **kwargs):
if port == port2:
return [(socket.AF_INET6, socket.SOCK_STREAM, 0, '', ('::1', 0, 0, 0))]
return await getaddrinfo_orig(host, port, *args, **kwargs)

self.loop.getaddrinfo = getaddrinfo

f = self.loop.create_connection(
lambda: MyProto(loop=self.loop),
'localhost', port1, local_addr=('localhost', port2))

with self.assertRaises(OSError):
self.loop.run_until_complete(f)

def test_create_connection_local_addr_in_use(self):
with test_utils.run_test_server() as httpd:
f = self.loop.create_connection(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :func:`asyncio.open_connection` to skip binding to local addresses of different family. Patch by Kumar Aditya.

0 comments on commit ba8dcdb

Please sign in to comment.