mirror of
https://github.com/python/cpython.git
synced 2025-07-31 15:14:22 +00:00
GH-86508: skip binding to local addresses of different family in asyncio.open_connection
(#100615)
This commit is contained in:
parent
a286caa937
commit
ba8dcdbcab
3 changed files with 50 additions and 2 deletions
|
@ -961,7 +961,10 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
sock = socket.socket(family=family, type=type_, proto=proto)
|
sock = socket.socket(family=family, type=type_, proto=proto)
|
||||||
sock.setblocking(False)
|
sock.setblocking(False)
|
||||||
if local_addr_infos is not None:
|
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:
|
try:
|
||||||
sock.bind(laddr)
|
sock.bind(laddr)
|
||||||
break
|
break
|
||||||
|
@ -974,7 +977,10 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
exc = OSError(exc.errno, msg)
|
exc = OSError(exc.errno, msg)
|
||||||
my_exceptions.append(exc)
|
my_exceptions.append(exc)
|
||||||
else: # all bind attempts failed
|
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)
|
await self.sock_connect(sock, address)
|
||||||
return sock
|
return sock
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
|
|
|
@ -670,6 +670,47 @@ class EventLoopTestsMixin:
|
||||||
self.assertEqual(port, expected)
|
self.assertEqual(port, expected)
|
||||||
tr.close()
|
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):
|
def test_create_connection_local_addr_in_use(self):
|
||||||
with test_utils.run_test_server() as httpd:
|
with test_utils.run_test_server() as httpd:
|
||||||
f = self.loop.create_connection(
|
f = self.loop.create_connection(
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix :func:`asyncio.open_connection` to skip binding to local addresses of different family. Patch by Kumar Aditya.
|
Loading…
Add table
Add a link
Reference in a new issue