Skip to content

Commit ba8dcdb

Browse files
GH-86508: skip binding to local addresses of different family in asyncio.open_connection (#100615)
1 parent a286caa commit ba8dcdb

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

Lib/asyncio/base_events.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,10 @@ async def _connect_sock(self, exceptions, addr_info, local_addr_infos=None):
961961
sock = socket.socket(family=family, type=type_, proto=proto)
962962
sock.setblocking(False)
963963
if local_addr_infos is not None:
964-
for _, _, _, _, laddr in local_addr_infos:
964+
for lfamily, _, _, _, laddr in local_addr_infos:
965+
# skip local addresses of different family
966+
if lfamily != family:
967+
continue
965968
try:
966969
sock.bind(laddr)
967970
break
@@ -974,7 +977,10 @@ async def _connect_sock(self, exceptions, addr_info, local_addr_infos=None):
974977
exc = OSError(exc.errno, msg)
975978
my_exceptions.append(exc)
976979
else: # all bind attempts failed
977-
raise my_exceptions.pop()
980+
if my_exceptions:
981+
raise my_exceptions.pop()
982+
else:
983+
raise OSError(f"no matching local address with {family=} found")
978984
await self.sock_connect(sock, address)
979985
return sock
980986
except OSError as exc:

Lib/test/test_asyncio/test_events.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,47 @@ def test_create_connection_local_addr(self):
670670
self.assertEqual(port, expected)
671671
tr.close()
672672

673+
def test_create_connection_local_addr_skip_different_family(self):
674+
# See https://github.com/python/cpython/issues/86508
675+
port1 = socket_helper.find_unused_port()
676+
port2 = socket_helper.find_unused_port()
677+
getaddrinfo_orig = self.loop.getaddrinfo
678+
679+
async def getaddrinfo(host, port, *args, **kwargs):
680+
if port == port2:
681+
return [(socket.AF_INET6, socket.SOCK_STREAM, 0, '', ('::1', 0, 0, 0)),
682+
(socket.AF_INET, socket.SOCK_STREAM, 0, '', ('127.0.0.1', 0))]
683+
return await getaddrinfo_orig(host, port, *args, **kwargs)
684+
685+
self.loop.getaddrinfo = getaddrinfo
686+
687+
f = self.loop.create_connection(
688+
lambda: MyProto(loop=self.loop),
689+
'localhost', port1, local_addr=('localhost', port2))
690+
691+
with self.assertRaises(OSError):
692+
self.loop.run_until_complete(f)
693+
694+
def test_create_connection_local_addr_nomatch_family(self):
695+
# See https://github.com/python/cpython/issues/86508
696+
port1 = socket_helper.find_unused_port()
697+
port2 = socket_helper.find_unused_port()
698+
getaddrinfo_orig = self.loop.getaddrinfo
699+
700+
async def getaddrinfo(host, port, *args, **kwargs):
701+
if port == port2:
702+
return [(socket.AF_INET6, socket.SOCK_STREAM, 0, '', ('::1', 0, 0, 0))]
703+
return await getaddrinfo_orig(host, port, *args, **kwargs)
704+
705+
self.loop.getaddrinfo = getaddrinfo
706+
707+
f = self.loop.create_connection(
708+
lambda: MyProto(loop=self.loop),
709+
'localhost', port1, local_addr=('localhost', port2))
710+
711+
with self.assertRaises(OSError):
712+
self.loop.run_until_complete(f)
713+
673714
def test_create_connection_local_addr_in_use(self):
674715
with test_utils.run_test_server() as httpd:
675716
f = self.loop.create_connection(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :func:`asyncio.open_connection` to skip binding to local addresses of different family. Patch by Kumar Aditya.

0 commit comments

Comments
 (0)