Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resolving IPv6-only hosts #6204

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/6195.bugfix
@@ -0,0 +1 @@
Fix the threaded dns resolver skip the correct dns response when connecting a IPv6-only host.
7 changes: 4 additions & 3 deletions aiohttp/resolver.py
Expand Up @@ -37,9 +37,10 @@ async def resolve(

hosts = []
for family, _, proto, _, address in infos:
if family == socket.AF_INET6:
if not (socket.has_ipv6 and address[3]): # type: ignore[misc]
continue
if family == socket.AF_INET6 and not socket.has_ipv6:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

socket.has_ipv6 is a compile-time flag.
A host can disable IPv6 support at system startup time also.

continue

if 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.
Expand Down
23 changes: 23 additions & 0 deletions tests/test_resolver.py
Expand Up @@ -143,6 +143,29 @@ async def unknown_addrinfo(*args: Any, **kwargs: Any) -> List[Any]:
assert len(res) == 0


async def test_threaded_resolver_ipv6_only_host() -> None:
loop = Mock()

async def ipv6_addrinfo(*args: Any, **kwargs: Any) -> List[Any]:
return [
(
socket.AF_INET6,
socket.SOCK_STREAM,
6,
"",
("2404:6800:4004:819::200", 443, 0, 0),
)
]

loop.getaddrinfo = ipv6_addrinfo
resolver = ThreadedResolver()
resolver._loop = loop
res = await resolver.resolve("https://ipv6.google.com")
assert len(res) == 1
assert res[0]["hostname"] == "https://ipv6.google.com"
assert res[0]["host"] == "2404:6800:4004:819::200"


async def test_close_for_threaded_resolver(loop: Any) -> None:
resolver = ThreadedResolver()
await resolver.close()
Expand Down