{"record":{"id":"3af8d25356e10ca4","repo":"XTLS/Xray-core","slug":"write-remaining-bytes-w","errorCode":null,"errorMessage":"write remaining bytes: %w","messagePattern":"write remaining bytes: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"transport/internet/finalmask/xmc/protocol.go","lineNumber":348,"sourceCode":"\t*v = append([]byte(*v), buf...)\n\n\treturn nil\n}\n\ntype RestBytes []byte\n\nfunc (v *RestBytes) readFrom(r io.Reader) error {\n\tbuf, err := io.ReadAll(r)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"read remaining bytes: %w\", err)\n\t}\n\t*v = append((*v)[:0], buf...)\n\treturn nil\n}\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}","sourceCodeStart":330,"sourceCodeEnd":366,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/transport/internet/finalmask/xmc/protocol.go#L330-L366","documentation":"RestBytes.writeTo writes the raw trailing bytes to the io.Writer and wraps any Write failure. As with the other write wrappers, the encoding cannot fail; this is an underlying connection/buffer error while flushing the tail of a packet.","triggerScenarios":"Sending a packet with a RestBytes payload after the peer disconnected; write deadline exceeded; underlying conn already closed by another goroutine or by error handling elsewhere.","commonSituations":"Writing a final close/flush packet after the remote already went away; races between a watchdog closing the conn and the encoder still writing; outbound proxy failure beneath the transport.","solutions":["Unwrap and classify: ErrConnClosed/broken pipe => peer gone, skip further writes; timeout => deadline too tight or network stalled.","Serialize all writes through one writer goroutine/channel so close-vs-write races cannot happen.","Once any write fails, abandon the connection instead of attempting more packet writes.","Tune write deadlines to the expected RTT and payload size."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := rest.writeTo(w); err != nil {\n    if errors.Is(err, net.ErrClosed) || isBrokenPipe(err) {\n        stopWriterLoop()\n        return nil // peer gone; nothing more to flush\n    }\n    return err\n}","preventionTips":["Funnel all sends through one writer goroutine to avoid close/write races.","Skip trailing writes once teardown has begun.","Abandon the connection after its first write error."],"tags":["go","network","io","protocol-framing"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}