From ec9b5025edb8b15e5f550778c394b5f644784516 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sat, 26 Mar 2022 19:00:00 +0100 Subject: [PATCH 1/2] bpo-27929: resolve names only for AF_INET/AF_INET6 with asyncio For other families, it may not make sense to resolve names. For example, with AF_BLUETOOTH, there is no name resolution and attempting to do so will trigger a "ai_family not supported". The bug report contains an example: ```python import asyncio import socket sock = socket.socket(family=socket.AF_BLUETOOTH, type=socket.SOCK_STREAM, proto=socket.BTPROTO_RFCOMM) sock.setblocking(False) addr = "00:12:34:56:78:99" loop = asyncio.get_event_loop() loop.run_until_complete(loop.sock_connect(sock, (addr, 1))) ``` https://bugs.python.org/issue27929 --- Lib/asyncio/selector_events.py | 3 ++- .../next/Library/2022-03-28-13-35-50.bpo-27929.j5mAmV.rst | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-03-28-13-35-50.bpo-27929.j5mAmV.rst diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py index 33ebc4b27808ce..e99a50395e7cb0 100644 --- a/Lib/asyncio/selector_events.py +++ b/Lib/asyncio/selector_events.py @@ -620,7 +620,8 @@ async def sock_connect(self, sock, address): if self._debug and sock.gettimeout() != 0: raise ValueError("the socket must be non-blocking") - if not hasattr(socket, 'AF_UNIX') or sock.family != socket.AF_UNIX: + if sock.family == socket.AF_INET or ( + base_events._HAS_IPv6 and sock.family == socket.AF_INET6): resolved = await self._ensure_resolved( address, family=sock.family, type=sock.type, proto=sock.proto, loop=self, diff --git a/Misc/NEWS.d/next/Library/2022-03-28-13-35-50.bpo-27929.j5mAmV.rst b/Misc/NEWS.d/next/Library/2022-03-28-13-35-50.bpo-27929.j5mAmV.rst new file mode 100644 index 00000000000000..f6e8226f5a750c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-03-28-13-35-50.bpo-27929.j5mAmV.rst @@ -0,0 +1,3 @@ +Fix :func:`asyncio.sock_connect` to only resolve names for ``AF_INET`` or +``AF_INET6`` families. Resolution may not make sense for other families, +like ``AF_BLUETOOTH`` and ``AF_UNIX``. From 5bf289e222b768cf5f13fe17ebb4ff8d1eda59f3 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Mon, 28 Mar 2022 22:41:05 +0300 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2022-03-28-13-35-50.bpo-27929.j5mAmV.rst --- .../next/Library/2022-03-28-13-35-50.bpo-27929.j5mAmV.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2022-03-28-13-35-50.bpo-27929.j5mAmV.rst b/Misc/NEWS.d/next/Library/2022-03-28-13-35-50.bpo-27929.j5mAmV.rst index f6e8226f5a750c..4c80a10bc56844 100644 --- a/Misc/NEWS.d/next/Library/2022-03-28-13-35-50.bpo-27929.j5mAmV.rst +++ b/Misc/NEWS.d/next/Library/2022-03-28-13-35-50.bpo-27929.j5mAmV.rst @@ -1,3 +1,3 @@ -Fix :func:`asyncio.sock_connect` to only resolve names for ``AF_INET`` or -``AF_INET6`` families. Resolution may not make sense for other families, -like ``AF_BLUETOOTH`` and ``AF_UNIX``. +Fix :meth:`asyncio.loop.sock_connect` to only resolve names for :const:`socket.AF_INET` or +:const:`socket.AF_INET6` families. Resolution may not make sense for other families, +like :const:`socket.AF_BLUETOOTH` and :const:`socket.AF_UNIX`.