{"record":{"id":"0af48f00dcf25d71","repo":"nginx/nginx","slug":"ngx-log-crit","errorCode":"NGX_LOG_CRIT","errorMessage":"getsockname() of the inherited socket #%d failed","messagePattern":"getsockname\\(\\) of the inherited socket #(.+?) failed","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/core/ngx_connection.c","lineNumber":164,"sourceCode":"#endif\n#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)\n    int                        timeout;\n#endif\n#if (NGX_HAVE_REUSEPORT)\n    int                        reuseport;\n#endif\n\n    ls = cycle->listening.elts;\n    for (i = 0; i < cycle->listening.nelts; i++) {\n\n        ls[i].sockaddr = ngx_palloc(cycle->pool, sizeof(ngx_sockaddr_t));\n        if (ls[i].sockaddr == NULL) {\n            return NGX_ERROR;\n        }\n\n        ls[i].socklen = sizeof(ngx_sockaddr_t);\n        if (getsockname(ls[i].fd, ls[i].sockaddr, &ls[i].socklen) == -1) {\n            ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno,\n                          \"getsockname() of the inherited \"\n                          \"socket #%d failed\", ls[i].fd);\n            ls[i].ignore = 1;\n            continue;\n        }\n\n        if (ls[i].socklen > (socklen_t) sizeof(ngx_sockaddr_t)) {\n            ls[i].socklen = sizeof(ngx_sockaddr_t);\n        }\n\n        switch (ls[i].sockaddr->sa_family) {\n\n#if (NGX_HAVE_INET6)\n        case AF_INET6:\n            ls[i].addr_text_max_len = NGX_INET6_ADDRSTRLEN;\n            len = NGX_INET6_ADDRSTRLEN + sizeof(\"[]:65535\") - 1;\n            break;\n#endif","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/nginx/nginx/blob/3f6f7824d4e2eb1ac37dec76683d525ac0ff521c/src/core/ngx_connection.c#L146-L182","documentation":"During a binary upgrade (SIGUSR2) the new master parses the NGX environment variable, which lists listening sockets inherited from the old master, and calls getsockname() on each fd to recover its address. If the syscall fails, this CRIT is logged and the socket is marked ignore=1: that fd's address is dropped from the new master's listener set.","triggerScenarios":"Starting the new binary with a stale or hand-crafted NGX variable; the old master closed the fds between the child inheriting NGX and the getsockname call; fd numbers shifted because a wrapper altered the environment; NGX pointing at non-listening fds.","commonSituations":"Custom upgrade scripts that copy or rewrite the environment instead of letting nginx pass NGX itself; supervisors (supervisord, custom wrappers) scrubbing env between masters; an aborted earlier upgrade leaving stale state; accidentally exporting NGX before a fresh 'nginx' start.","solutions":["Abort the broken new master (TERM it) and either retry the upgrade with the documented 'kill -USR2 $(cat /run/nginx.pid)' flow or do a clean restart from the service manager","Never set or edit NGX manually - only the value nginx itself writes is valid","Inspect the old master's environment and fds: 'tr \"\\0\" \"\\n\" < /proc/$(cat /run/nginx.pid)/environ | grep ^NGX=' and 'ls -l /proc/<oldpid>/fd'","Remove supervisors that rewrite the environment around the USR2 handoff, or switch to systemd-managed restarts for upgrades"],"exampleFix":"# before (hand-set inherited sockets - corrupts fd assumptions)\nNGX='3:4;' /usr/sbin/nginx\n# after (correct binary upgrade)\nkill -USR2 $(cat /run/nginx.pid)","handlingStrategy":"fallback","validationCode":"# before any USR2 upgrade, sanity-check the old master's inherited-socket environment\nold=$(cat /run/nginx.pid)\ntr '\\0' '\\n' < /proc/$old/environ | grep '^NGX='\nls -l /proc/$old/fd | awk '$NF ~ /socket/' | wc -l","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use only 'kill -USR2 <pid>' for live upgrades; never hand-write NGX","After USR2, verify the new master inherited all listeners before QUITting the old one","Keep a systemd-initiated restart as the fallback path when upgrade checks fail"],"tags":["nginx","upgrade","usr2","inherited-sockets","getsockname","runtime"],"backgroundTag":"binary-upgrade-failed","analyzedSha":"3f6f7824d4e2eb1ac37dec76683d525ac0ff521c","analyzedAt":"2026-08-22T03:09:46.447Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}