{"record":{"id":"6e6edb497ff30c64","repo":"XX-net/XX-Net","slug":"connect-to-dns-server-s-d-fail-r","errorCode":null,"errorMessage":"Connect to DNS server %s:%d fail:%r","messagePattern":"Connect to DNS server (.+?):(.+?) fail:%r","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"code/default/smart_router/local/dns_query.py","lineNumber":367,"sourceCode":"                info = [(socket.AF_INET, socket.SOCK_STREAM, 0, \"\", (host, port))]\n\n        for res in info:\n            af, socktype, proto, canonname, sa = res\n            s = None\n            try:\n                s = socket.socket(af, socktype, proto)\n\n                s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n                s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 32 * 1024)\n                s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, True)\n                s.settimeout(connect_timeout)\n                s.connect((host, port))\n                return s\n            except socket.error:\n                if s:\n                    s.close()\n            except Exception as e:\n                xlog.warn(\"Connect to DNS server %s:%d fail:%r\", host, port)\n\n        return None\n\n    def connect(self, host, port):\n        if not g.config.PROXY_ENABLE:\n            sock = self.direct_connect(host, self.port)\n        else:\n            connect_timeout = 5\n\n            import socks\n\n            sock = socks.socksocket(socket.AF_INET)\n            sock.set_proxy(proxy_type=g.config.PROXY_TYPE,\n                           addr=g.config.PROXY_HOST,\n                           port=g.config.PROXY_PORT, rdns=True,\n                           username=g.config.PROXY_USER,\n                           password=g.config.PROXY_PASSWD)\n","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/XX-net/XX-Net/blob/cfa5bc17b67676e467f37ec50766127e0ab5f0aa/code/default/smart_router/local/dns_query.py#L349-L385","documentation":"direct_connect() logs this warning when socket.create_connection/connect to the DNS server (host, port) raises a non-socket.error exception, and returns None. Note the log line omits the exception object (%r is declared but 'e' is not passed), so the actual cause is hidden. Callers must handle the None return.","triggerScenarios":"TCP connect to (host, port) — usually port 853 for DoT or 53 for TCP DNS — failing with a non-socket.error exception such as a DNS resolution failure of the hostname, an SSL/timeout wrapper error, or a programming error. socket.error itself is silently retried/ignored.","commonSituations":"Blocking of TCP 853/53 by firewalls (DoT is commonly blocked), the server hostname failing to resolve, a dead server IP in the fallback list, corporate proxies intercepting direct connections.","solutions":["Test raw reachability: 'nc -vz host 853' (or 53) from the same host","If TCP 853 is blocked, switch to DoH (port 443) which passes almost all firewalls","Fix or replace the server address/port configuration with a known-good public resolver","Patch the log call to include 'e' (xlog.warn(..., host, port, e)) so the real cause is visible"],"exampleFix":"# before\nxlog.warn(\"Connect to DNS server %s:%d fail:%r\", host, port)\n# after\nxlog.warn(\"Connect to DNS server %s:%d fail:%r\", host, port, e)","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"sock = client.direct_connect(host, port)\nif sock is None:\n    sock = fallback_client.connect(host, port)  # e.g. DoH/443\n# never use sock without the None check","preventionTips":["Always null-check the return of connect()/direct_connect()","Prefer DoH (443) where 853/53 are likely blocked","Patch the log format to include the exception for diagnosability"],"tags":["dns","tcp-connect","connection-failed","dot"],"backgroundTag":"tcp-connect-failed","analyzedSha":"cfa5bc17b67676e467f37ec50766127e0ab5f0aa","analyzedAt":"2026-08-27T19:28:28.225Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}