{"record":{"id":"9184270145596f03","repo":"XTLS/Xray-core","slug":"write-bytes-w","errorCode":null,"errorMessage":"write bytes: %w","messagePattern":"write bytes: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"transport/internet/finalmask/xmc/protocol.go","lineNumber":362,"sourceCode":"}\n\nfunc (v *RestBytes) writeTo(w io.Writer) error {\n\tif _, err := w.Write(*v); err != nil {\n\t\treturn fmt.Errorf(\"write remaining bytes: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc (v *Bytes) writeTo(w io.Writer) error {\n\tlength := Varint(len(*v))\n\terr := length.writeTo(w)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"write bytes length: %w\", err)\n\t}\n\n\t_, err = w.Write(*v)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"write bytes: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc readByte(r io.Reader) (byte, error) {\n\tvar buf [1]byte\n\t_, err := io.ReadFull(r, buf[:])\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"read byte: %w\", err)\n\t}\n\n\treturn buf[0], nil\n}\n\nfunc writePacket(w io.Writer, packetID int, fields ...field) error {\n\t_, err := writePacketWithLength(w, packetID, fields...)\n\treturn err","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/transport/internet/finalmask/xmc/protocol.go#L344-L380","documentation":"After the length prefix, Bytes.writeTo writes the payload slice and wraps any Write failure with this message. The payload itself is opaque; failure is always the underlying writer (connection closed/reset, deadline exceeded, or a custom writer erroring).","triggerScenarios":"Writing the body of a byte-array field to a conn that has been closed by the peer or by local error handling; write deadline expiring mid-payload on slow links.","commonSituations":"Half-closed TCP after server restart; MTU/throughput issues making large writes exceed deadlines; concurrent close from a monitor goroutine.","solutions":["On first write error, mark the connection dead and stop the write loop — subsequent writes will only fail again.","Use write deadlines proportional to payload size.","If errors appear immediately at connect, verify the endpoint and protocol version.","Capture which packet ID was being written when the failure occurred for faster diagnosis."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := bytesField.writeTo(w); err != nil {\n    markConnDead() // first write failure is terminal\n    return fmt.Errorf(\"connection failed during payload write: %w\", err)\n}","preventionTips":["Never write again on a connection that returned a write error.","Use deadlines scaled to payload size and link speed.","Verify endpoint/protocol version when failures occur at connect time."],"tags":["go","network","io","protocol-framing"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}