python/cpython · error · ValueError

can not get address information

Error message

can not get address information

What it means

Raised by create_datagram_endpoint() when local_addr and remote_addr resolve to disjoint sets of (family, proto) pairs. Endpoints require at least one common family — e.g. local resolves only to IPv4 while remote resolves only to IPv6 — so no (family, proto) pair can host both addresses.

Source

Thrown at Lib/asyncio/base_events.py:1463

                            addr, family=family, type=socket.SOCK_DGRAM,
                            proto=proto, flags=flags, loop=self)
                        if not infos:
                            raise OSError('getaddrinfo() returned empty list')

                        for fam, _, pro, _, address in infos:
                            key = (fam, pro)
                            if key not in addr_infos:
                                addr_infos[key] = [None, None]
                            addr_infos[key][idx] = address

                # each addr has to have info for each (family, proto) pair
                addr_pairs_info = [
                    (key, addr_pair) for key, addr_pair in addr_infos.items()
                    if not ((local_addr and addr_pair[0] is None) or
                            (remote_addr and addr_pair[1] is None))]

                if not addr_pairs_info:
                    raise ValueError('can not get address information')

            exceptions = []

            for ((family, proto),
                 (local_address, remote_address)) in addr_pairs_info:
                sock = None
                r_addr = None
                try:
                    sock = socket.socket(
                        family=family, type=socket.SOCK_DGRAM, proto=proto)
                    if reuse_port:
                        _set_reuseport(sock)
                    if allow_broadcast:
                        sock.setsockopt(
                            socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
                    sock.setblocking(False)

                    if local_addr:

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Make both addresses the same family (both IPv4 or both IPv6 literals, or both hostnames that resolve to the same family)
  2. Pass family=socket.AF_INET (or AF_INET6) explicitly to force consistent resolution of both sides
  3. Verify with socket.getaddrinfo() what each side actually resolves to in this environment

Example fix

# before
await loop.create_datagram_endpoint(factory, local_addr=('127.0.0.1', 0), remote_addr=('::1', 9999))

# after
await loop.create_datagram_endpoint(factory, local_addr=('127.0.0.1', 0), remote_addr=('127.0.0.1', 9999))
Defensive patterns

Strategy: validation

Validate before calling

async def families_agree(loop, local, remote) -> bool:
    lf = {(i[0], i[2]) for i in await loop.getaddrinfo(*local, type=socket.SOCK_DGRAM)}
    rf = {(i[0], i[2]) for i in await loop.getaddrinfo(*remote, type=socket.SOCK_DGRAM)}
    return bool(lf & rf)
assert await families_agree(loop, local_addr, remote_addr)

Prevention

When it happens

Trigger: local_addr=('127.0.0.1', p) with remote_addr=('::1', q); or local resolves only IPv6 (single-stack /etc/hosts) while remote is IPv4-only.

Common situations: Hardcoded loopback literals mixing 127.0.0.1 and ::1; hosts files where one side has only one family record; dual-stack misconfiguration where the local machine lacks IPv4 or IPv6 connectivity so resolution skips that family.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/acbe0301813aaa4e. Report an issue: GitHub.