From 28c7e1452ea9a9321a3430f3d9c16333d1e64182 Mon Sep 17 00:00:00 2001 From: Hanaasagi Date: Wed, 21 Jul 2021 16:28:29 +0000 Subject: [PATCH] Fix: fix the code about handling `getaddrinfo` when disable ipv6 in CPython and enable ipv6 in system. Related to #5901; `getaddrinfo` will return an `(int, bytes)` tuple, if CPython could not handle the address family. It will cause a index out of range error in aiohttp. --- CHANGES/5901.bugfix | 4 ++++ aiohttp/resolver.py | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 CHANGES/5901.bugfix diff --git a/CHANGES/5901.bugfix b/CHANGES/5901.bugfix new file mode 100644 index 00000000000..5c5940fde1b --- /dev/null +++ b/CHANGES/5901.bugfix @@ -0,0 +1,4 @@ +Fix the error in handling the return value of `getaddrinfo`. +`getaddrinfo` will return an `(int, bytes)` tuple, if CPython could not handle the address family. +It will cause a index out of range error in aiohttp. For example, if user compile CPython with +`--disable-ipv6` option but his system enable the ipv6. diff --git a/aiohttp/resolver.py b/aiohttp/resolver.py index e62e7f377b6..d82aee6acf3 100644 --- a/aiohttp/resolver.py +++ b/aiohttp/resolver.py @@ -37,7 +37,11 @@ async def resolve( hosts = [] for family, _, proto, _, address in infos: - if family == socket.AF_INET6 and address[3]: # type: ignore[misc] + if ( + socket.has_ipv6 + and family == socket.AF_INET6 + and address[3] # type: ignore[misc] + ): # This is essential for link-local IPv6 addresses. # LL IPv6 is a VERY rare case. Strictly speaking, we should use # getnameinfo() unconditionally, but performance makes sense.