{"record":{"id":"bee5b42a45542cbb","repo":"XTLS/Xray-core","slug":"write-boolean-w","errorCode":null,"errorMessage":"write boolean: %w","messagePattern":"write boolean: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"transport/internet/finalmask/xmc/protocol.go","lineNumber":297,"sourceCode":"func (v *Boolean) readFrom(r io.Reader) error {\n\tb, err := readByte(r)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"read boolean: %w\", err)\n\t}\n\tif b > 1 {\n\t\treturn fmt.Errorf(\"read boolean: invalid value: %d\", b)\n\t}\n\t*v = b == 1\n\treturn nil\n}\n\nfunc (v *Boolean) writeTo(w io.Writer) error {\n\tvalue := byte(0)\n\tif *v {\n\t\tvalue = 1\n\t}\n\tif _, err := w.Write([]byte{value}); err != nil {\n\t\treturn fmt.Errorf(\"write boolean: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc (v *UUID) writeTo(w io.Writer) error {\n\t_, err := w.Write(v[:])\n\tif err != nil {\n\t\treturn fmt.Errorf(\"write UUID: %w\", err)\n\t}\n\treturn nil\n}\n\ntype Bytes []byte\n\nfunc (v *Bytes) readFrom(r io.Reader) error {\n\tvar length Varint\n\terr := length.readFrom(r)\n\tif err != nil {","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/transport/internet/finalmask/xmc/protocol.go#L279-L315","documentation":"Boolean.writeTo writes a single 0/1 byte to the underlying io.Writer and wraps any failure from that Write with this message. The error itself is always an I/O problem (closed connection, broken pipe, buffer/full write error) — the boolean encoding itself cannot fail.","triggerScenarios":"Encoding a Minecraft protocol packet containing a Boolean field after the peer has disconnected or the connection entered an error state; writing to a bytes.Buffer that is backed by a full/closed resource.","commonSituations":"Server closes the socket mid-handshake (timeout, ban, restart); NAT/firewall drops the TCP connection; concurrent write on the same conn from two goroutines causing one side to see a closed writer.","solutions":["Inspect the wrapped error (%w) — ErrConnClosed/broken pipe means the peer went away; treat as connection loss, not a bug.","Ensure only one goroutine writes to the connection at a time (the transport serializes writes; don't bypass it).","Add write deadlines so a stalled peer surfaces as a timeout instead of an indefinite block followed by RST.","If it happens immediately on connect, check for protocol/port mismatch hitting a non-Minecraft service."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := boolField.writeTo(w); err != nil {\n    if errors.Is(err, net.ErrClosed) || isBrokenPipe(err) {\n        // peer gone: abandon connection\n        return err\n    }\n    var nerr net.Error\n    if errors.As(err, &nerr) && nerr.Timeout() {\n        return fmt.Errorf(\"write stalled: %w\", err)\n    }\n    return err\n}","preventionTips":["Write through a single serialized writer path.","Set conn.SetWriteDeadline before each packet flush.","Treat the first write error as terminal for the connection."],"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"}