{"record":{"id":"1e06eabf0bcd2ed3","repo":"XTLS/Xray-core","slug":"failed-to-read-packet-length","errorCode":null,"errorMessage":"failed to read packet length","messagePattern":"failed to read packet length","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/vless/encoding/addons.go","lineNumber":173,"sourceCode":"\t}\n\treturn nil\n}\n\nfunc NewLengthPacketReader(reader io.Reader) *LengthPacketReader {\n\treturn &LengthPacketReader{\n\t\tReader: reader,\n\t\tcache:  make([]byte, 2),\n\t}\n}\n\ntype LengthPacketReader struct {\n\tio.Reader\n\tcache []byte\n}\n\nfunc (r *LengthPacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) {\n\tif _, err := io.ReadFull(r.Reader, r.cache); err != nil { // maybe EOF\n\t\treturn nil, errors.New(\"failed to read packet length\").Base(err)\n\t}\n\tlength := int32(r.cache[0])<<8 | int32(r.cache[1])\n\t// fmt.Println(\"Read\", length)\n\tmb := make(buf.MultiBuffer, 0, length/buf.Size+1)\n\tfor length > 0 {\n\t\tsize := length\n\t\tif size > buf.Size {\n\t\t\tsize = buf.Size\n\t\t}\n\t\tlength -= size\n\t\tb := buf.New()\n\t\tif _, err := b.ReadFullFrom(r.Reader, size); err != nil {\n\t\t\treturn nil, errors.New(\"failed to read packet payload\").Base(err)\n\t\t}\n\t\tmb = append(mb, b)\n\t}\n\treturn mb, nil\n}","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/proxy/vless/encoding/addons.go#L155-L191","documentation":"LengthPacketReader.ReadMultiBuffer could not io.ReadFull the fixed 2-byte length prefix from the underlying reader (it may be EOF at a record boundary, in which case the comment notes it is expected). Any other cause means the peer stopped mid-frame and the framing cannot be recovered.","triggerScenarios":"Thrown at proxy/vless/encoding/addons.go:173 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Propagate/close: the packet framing is broken and the stream cannot be resynchronized","Distinguish clean EOF at frame boundary from partial reads; only the former is normal termination","Check for connection resets or short writes on the peer side"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","contentChangedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}