[Python-checkins] GH-86508: skip binding to local addresses of different family in `asyncio.open_connection` (GH-100615)
miss-islington
webhook-mailer at python.org
Wed Jan 4 03:24:42 EST 2023
https://github.com/python/cpython/commit/35a010f1f92e272561f4aaf5918215c4953bce91
commit: 35a010f1f92e272561f4aaf5918215c4953bce91
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2023-01-04T00:24:36-08:00
summary:
GH-86508: skip binding to local addresses of different family in `asyncio.open_connection` (GH-100615)
(cherry picked from commit ba8dcdbcab5fd9989be6c9a51002394e782c463c)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Library/2022-12-30-07-49-08.gh-issue-86508.nGZDzC.rst
M Lib/asyncio/base_events.py
M Lib/test/test_asyncio/test_events.py
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index 9a05abf45fe7..f4c30c1551a3 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -946,7 +946,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
@@ -959,7 +962,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:
diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py
index 92c69de742ba..28d5f39ef611 100644
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -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(
diff --git a/Misc/NEWS.d/next/Library/2022-12-30-07-49-08.gh-issue-86508.nGZDzC.rst b/Misc/NEWS.d/next/Library/2022-12-30-07-49-08.gh-issue-86508.nGZDzC.rst
new file mode 100644
index 000000000000..aedad0f252c2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-12-30-07-49-08.gh-issue-86508.nGZDzC.rst
@@ -0,0 +1 @@
+Fix :func:`asyncio.open_connection` to skip binding to local addresses of different family. Patch by Kumar Aditya.
More information about the Python-checkins
mailing list