uvloop icon indicating copy to clipboard operation
uvloop copied to clipboard

getaddrinfo() returns wrong scope ID for IPv6 link-local addresses when type=SocketKind.SOCK_STREAM

Open agronholm opened this issue 2 years ago • 0 comments

  • uvloop version: 0.17.0
  • Python version: 3.11.3
  • Platform: Linux
  • Can you reproduce the bug with PYTHONASYNCIODEBUG in env?: yes
  • Does uvloop behave differently from vanilla asyncio? How?: Vanilla asyncio returns the correct scope ID, uvloop returns 0

When passing type=SocketKind.SOCK_STREAM to getaddrinfo() in uvloop, the scope ID for link-local addresses is left as 0. This works correctly (returns nonzero scope ID) in socket.getaddrinfo() and vanilla asyncio's corresponding function.

Repro script (requires psutil installed):

from asyncio import get_running_loop, run
from socket import SocketKind

import psutil
import uvloop


async def main():
    for ifname, addresses in psutil.net_if_addrs().items():
        for addr in addresses:
            if addr.address.startswith("fe80::") and "%" in addr.address:
                gai_res = await get_running_loop().getaddrinfo(addr.address, 0, type=SocketKind.SOCK_STREAM)
                scope_id = gai_res[-1][-1][-1]
                print("Scope id for", gai_res[-1], "is", scope_id)
                return

    print("Could not find a suitable link-local address")

# Comment out this line, and it works fine
uvloop.install()
run(main())

agronholm avatar May 14 '23 09:05 agronholm