{"record":{"id":"740770a7b73156da","repo":"shadowsocks/shadowsocks-rust","slug":"failed-to-encode-dns-response","errorCode":null,"errorMessage":"failed to encode DNS response","messagePattern":"failed to encode DNS response","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/shadowsocks-service/src/local/fake_dns/tcp_server.rs","lineNumber":151,"sourceCode":"                    return Err(io::Error::new(io::ErrorKind::Other, err));\n                }\n            };\n\n            let rsp_message = match handle_dns_request(&req_message, &manager).await {\n                Ok(m) => m,\n                Err(err) => {\n                    error!(\"failed to handle DNS request, error: {}\", err);\n\n                    Message::error_msg(req_message.id, req_message.op_code, ResponseCode::ServFail)\n                }\n            };\n\n            let mut rsp_buffer = Vec::with_capacity(2 + 512);\n            rsp_buffer.resize(2, 0);\n            let mut rsp_encoder = BinEncoder::with_offset(&mut rsp_buffer, 2);\n            if let Err(err) = rsp_message.emit(&mut rsp_encoder) {\n                error!(\"failed to encode DNS response, error: {}\", err);\n                return Err(io::Error::new(io::ErrorKind::Other, err));\n            }\n\n            let rsp_length = (rsp_buffer.len() - 2) as u16;\n            BigEndian::write_u16(&mut rsp_buffer[0..2], rsp_length);\n\n            stream.write_all(&rsp_buffer).await?;\n        }\n\n        Ok(())\n    }\n}\n","sourceCodeStart":133,"sourceCodeEnd":163,"githubUrl":"https://github.com/shadowsocks/shadowsocks-rust/blob/8eb0f0a65b1d976ab6bed5787327ef86529b0435/crates/shadowsocks-service/src/local/fake_dns/tcp_server.rs#L133-L163","documentation":"In the fake-DNS TCP server, after building a DNS response message the hickory (trust-dns) BinEncoder fails to emit it; the code logs \"failed to encode DNS response\" and returns the failure as an io::Error (Other). The client connection is closed without a valid DNS reply.","triggerScenarios":"handle_client processes a TCP DNS query, constructs rsp_message, and rsp_message.emit(&mut rsp_encoder) returns an error during wire-format encoding.","commonSituations":"Malformed or unusual query producing a response that fails encoding; internal bug/incompatible hickory-proto version; buffer/offset issues in the TCP length-prefixed response path.","solutions":["Upgrade shadowsocks-service and its hickory-proto dependency to compatible versions","Capture the query causing the failure and reproduce; report/inspect for malformed-input handling","Check logs for the underlying hickory encoder error message for specifics","As a workaround, avoid routing the offending client through fake-dns and use real DNS"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// server-side failure surfaces as connection close; on the client side:\nmatch timeout(DNS_TIMEOUT, tcp_dns_query(q)).await {\n    Ok(Err(e)) | Err(_) => fallback_to_system_resolver(q).await,\n    Ok(Ok(m)) => m,\n}","preventionTips":["Keep shadowsocks-service and hickory-proto versions in sync","Log encoder errors with the offending query for diagnosis","Report reproducible encoding failures upstream"],"tags":["dns","encoding","fake-dns","serialization"],"backgroundTag":"dns-response-encode-failed","analyzedSha":"8eb0f0a65b1d976ab6bed5787327ef86529b0435","analyzedAt":"2026-09-09T12:20:43.168Z","contentChangedAt":"2026-09-09T12:20:43.168Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}