Skip to content

GH-86508: skip binding to local addresses of different family in asyncio.open_connection #100615

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

Merged
merged 4 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.