{"record":{"id":"73bf49d8832927ea","repo":"python/cpython","slug":"panic-n","errorCode":null,"errorMessage":"panic!\\n","messagePattern":"panic!\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"Modules/getaddrinfo.c","lineNumber":370,"sourceCode":"            const char *proto;\n\n            if (pai->ai_flags & AI_NUMERICSERV) {\n                ERR(EAI_NONAME);\n            }\n\n            proto = NULL;\n            switch (pai->ai_socktype) {\n            case GAI_ANY:\n                proto = NULL;\n                break;\n            case SOCK_DGRAM:\n                proto = \"udp\";\n                break;\n            case SOCK_STREAM:\n                proto = \"tcp\";\n                break;\n            default:\n                fprintf(stderr, \"panic!\\n\");\n                break;\n            }\n            if ((sp = getservbyname(servname, proto)) == NULL)\n                ERR(EAI_SERVICE);\n            port = sp->s_port;\n            if (pai->ai_socktype == GAI_ANY) {\n                if (strcmp(sp->s_proto, \"udp\") == 0) {\n                    pai->ai_socktype = SOCK_DGRAM;\n                    pai->ai_protocol = IPPROTO_UDP;\n                } else if (strcmp(sp->s_proto, \"tcp\") == 0) {\n                    pai->ai_socktype = SOCK_STREAM;\n                    pai->ai_protocol = IPPROTO_TCP;\n                } else\n                    ERR(EAI_PROTOCOL);                          /*xxx*/\n            }\n        }\n    }\n","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Modules/getaddrinfo.c#L352-L388","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass an explicit socktype of socket.SOCK_STREAM or socket.SOCK_DGRAM when resolving symbolic service names","Or pass the port number instead of a service name so the getservbyname branch is skipped","If you maintain the build, get the platform to supply native getaddrinfo so Modules/getaddrinfo.c is not compiled in"],"exampleFix":"# before\nsocket.getaddrinfo('host', 'http', type=socket.SOCK_SEQPACKET)\n\n# after\nsocket.getaddrinfo('host', 'http', type=socket.SOCK_STREAM)","handlingStrategy":"validation","validationCode":"import socket\n\nsocktype = ...  # possibly exotic\nif socktype not in (0, socket.SOCK_STREAM, socket.SOCK_DGRAM):\n    socktype = socket.SOCK_STREAM          # sane hint for service names\nsocket.getaddrinfo(host, 'http', type=socktype)","typeGuard":"import socket\n\ndef is_supported_socktype(t: int) -> bool:\n    return t in (0, socket.SOCK_STREAM, socket.SOCK_DGRAM)","tryCatchPattern":"import socket\n\ntry:\n    infos = socket.getaddrinfo(host, 'http', type=socktype)\nexcept socket.gaierror as e:\n    if e.errno == socket.EAI_SERVICE:\n        infos = socket.getaddrinfo(host, 80, type=socket.SOCK_STREAM)  # use numeric port\n    else:\n        raise","preventionTips":["Prefer numeric ports over service names in getaddrinfo calls","Always pass an explicit SOCK_STREAM or SOCK_DGRAM hint","Treat any 'panic!' stderr output from getaddrinfo as a signal your hints are malformed"],"tags":["socket","getaddrinfo","network","cpython-internals"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}