{"record":{"id":"4fda520f627d5ff6","repo":"XX-net/XX-Net","slug":"query-over-tcp-s-type-s-connect-fail","errorCode":null,"errorMessage":"query_over_tcp %s type:%s connect fail.","messagePattern":"query_over_tcp (.+?) type:(.+?) connect fail\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"code/default/smart_router/local/dns_query.py","lineNumber":416,"sourceCode":"                [sock, last_query_time] = self.connections.pop()\n                if time.time() - last_query_time < self.connection_timeout:\n                    return sock\n            except:\n                pass\n\n        server_ip = self.get_server()\n        if not server_ip:\n            return None\n\n        sock = self.connect(server_ip, self.port)\n        return sock\n\n    def query(self, domain, dns_type=1):\n        t0 = time.time()\n        try:\n            sock = self.get_connection()\n            if not sock:\n                xlog.warn(\"query_over_tcp %s type:%s connect fail.\", domain, dns_type)\n                return []\n\n            d = DNSRecord(DNSHeader())\n            d.add_question(DNSQuestion(domain, dns_type))\n\n            data = d.pack()\n            data = struct.pack(\"!H\", len(data)) + data\n            sock.sendall(data)\n\n            response = sock.recv(8192)\n            if not response:\n                return []\n\n            length = struct.unpack(\"!H\", bytes(response[:2]))[0]\n            while len(response) - 2 < length:\n                response += sock.recv(8192)\n\n            t2 = time.time()","sourceCodeStart":398,"sourceCodeEnd":434,"githubUrl":"https://github.com/XX-net/XX-Net/blob/cfa5bc17b67676e467f37ec50766127e0ab5f0aa/code/default/smart_router/local/dns_query.py#L398-L434","documentation":"In the TCP DNS client's query(), get_connection() returned no usable socket, so no query was sent and an empty IP list is returned. This means the previously established connection pool had no live connection and a fresh direct/proxy connect (see direct_connect, error 154) failed.","triggerScenarios":"Calling query() on the TCP DNS client when all pooled connections are stale/closed and direct_connect() to the DNS server fails — network down, firewall blocking TCP 53, or proxy mode with PROXY_ENABLE and no working proxy path.","commonSituations":"Firewalled outbound TCP 53, flaky networks after idle (pooled sockets reset by middleboxes), proxy misconfiguration when PROXY_ENABLE is set.","solutions":["Verify TCP reachability of the DNS server: 'nc -vz 8.8.8.8 53'","Use DoH (port 443) instead of plain TCP DNS in restrictive networks","Check proxy settings if PROXY_ENABLE is on — connect() routes through the proxy in that mode","Retry with backoff; transient connect failures recover and the pool re-establishes"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for attempt in range(2):\n    ips = tcp_client.query(domain)\n    if ips:\n        break\n    time.sleep(0.5 * (attempt + 1))","preventionTips":["Always null/empty-check query() results before using them","Expire pooled connections aggressively to avoid stale sockets","Keep an alternate transport client ready to fail over"],"tags":["dns","tcp","connection-pool","get-connection"],"backgroundTag":"tcp-connect-failed","analyzedSha":"cfa5bc17b67676e467f37ec50766127e0ab5f0aa","analyzedAt":"2026-08-27T19:28:28.225Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}