python/cpython · warning

panic!\n

Error message

panic!\n

What it means

A stderr trace from CPython's internal getaddrinfo replacement (used on platforms without native getaddrinfo, e.g. some Windows/legacy builds). It prints 'panic!' when the ai_socktype for a service-name lookup is neither GAI_ANY, SOCK_DGRAM, nor SOCK_STREAM — an internal invariant violation — then proceeds; the lookup usually fails next with EAI_SERVICE because getservbyname() had no valid protocol hint.

Source

Thrown at Modules/getaddrinfo.c:370

            const char *proto;

            if (pai->ai_flags & AI_NUMERICSERV) {
                ERR(EAI_NONAME);
            }

            proto = NULL;
            switch (pai->ai_socktype) {
            case GAI_ANY:
                proto = NULL;
                break;
            case SOCK_DGRAM:
                proto = "udp";
                break;
            case SOCK_STREAM:
                proto = "tcp";
                break;
            default:
                fprintf(stderr, "panic!\n");
                break;
            }
            if ((sp = getservbyname(servname, proto)) == NULL)
                ERR(EAI_SERVICE);
            port = sp->s_port;
            if (pai->ai_socktype == GAI_ANY) {
                if (strcmp(sp->s_proto, "udp") == 0) {
                    pai->ai_socktype = SOCK_DGRAM;
                    pai->ai_protocol = IPPROTO_UDP;
                } else if (strcmp(sp->s_proto, "tcp") == 0) {
                    pai->ai_socktype = SOCK_STREAM;
                    pai->ai_protocol = IPPROTO_TCP;
                } else
                    ERR(EAI_PROTOCOL);                          /*xxx*/
            }
        }
    }

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Pass an explicit socktype of socket.SOCK_STREAM or socket.SOCK_DGRAM when resolving symbolic service names
  2. Or pass the port number instead of a service name so the getservbyname branch is skipped
  3. If you maintain the build, get the platform to supply native getaddrinfo so Modules/getaddrinfo.c is not compiled in

Example fix

# before
socket.getaddrinfo('host', 'http', type=socket.SOCK_SEQPACKET)

# after
socket.getaddrinfo('host', 'http', type=socket.SOCK_STREAM)
Defensive patterns

Strategy: validation

Validate before calling

import socket

socktype = ...  # possibly exotic
if socktype not in (0, socket.SOCK_STREAM, socket.SOCK_DGRAM):
    socktype = socket.SOCK_STREAM          # sane hint for service names
socket.getaddrinfo(host, 'http', type=socktype)

Type guard

import socket

def is_supported_socktype(t: int) -> bool:
    return t in (0, socket.SOCK_STREAM, socket.SOCK_DGRAM)

Try / catch

import socket

try:
    infos = socket.getaddrinfo(host, 'http', type=socktype)
except socket.gaierror as e:
    if e.errno == socket.EAI_SERVICE:
        infos = socket.getaddrinfo(host, 80, type=socket.SOCK_STREAM)  # use numeric port
    else:
        raise

Prevention

When it happens

Trigger: A hint passed to socket.getaddrinfo(host, 'servicename', type=<something other than 0/SOCK_STREAM/SOCK_DGRAM>) reaching the built-in resolver's getservbyname branch; realistically reachable only via exotic socket type constants or corrupted hints, since the public API validates types upstream.

Common situations: Passing a raw numeric or custom socktype (e.g. SOCK_SEQPACKET) together with a symbolic service name on a platform using Modules/getaddrinfo.c; essentially only seen by CPython porters and users of niche embedded builds — not on mainstream Linux/macOS glibc systems.

Related errors


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