{"record":{"id":"6cb0a74be4568862","repo":"apache/beam","slug":"error-encoding-byte-v","errorCode":null,"errorMessage":"error encoding byte: %v","messagePattern":"error encoding byte: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/core/graph/coder/bytes.go","lineNumber":30,"sourceCode":"// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\npackage coder\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\n\t\"github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/ioutilx\"\n\t\"github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors\"\n)\n\n// EncodeByte encodes a single byte.\nfunc EncodeByte(v byte, w io.Writer) error {\n\t// Encoding: raw byte.\n\tif _, err := ioutilx.WriteUnsafe(w, []byte{v}); err != nil {\n\t\treturn fmt.Errorf(\"error encoding byte: %v\", err)\n\t}\n\treturn nil\n}\n\n// DecodeByte decodes a single byte.\nfunc DecodeByte(r io.Reader) (byte, error) {\n\t// Encoding: raw byte\n\tvar b [1]byte\n\tif err := ioutilx.ReadNBufUnsafe(r, b[:]); err != nil {\n\t\tif err == io.EOF {\n\t\t\treturn 0, err\n\t\t}\n\t\treturn 0, errors.Wrap(err, \"error decoding byte\")\n\t}\n\treturn b[0], nil\n}\n\n// EncodeBytes encodes a []byte with a length prefix per the beam protocol.","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/core/graph/coder/bytes.go#L12-L48","documentation":"EncodeByte writes a single raw byte to an io.Writer using ioutilx.WriteUnsafe, and wraps any write failure as 'error encoding byte: %v'. It is part of the Beam Go SDK core coder package used when serializing byte-typed elements in pipelines.","triggerScenarios":"Any pipeline element encoding path that calls bytes.EncodeByte when the underlying io.Writer fails: closed stream, broken connection to a runner/harness, or a full/closed buffer.","commonSituations":"A runner data channel (gRPC stream) breaks mid-bundle so the sink writer errors; tests passing a nil or failing writer; writing to a closed pipe during local pipeline execution.","solutions":["Inspect the wrapped cause; fix the underlying writer/stream failure (re-establish the data channel, reopen the file).","Ensure the io.Writer passed to the coder is valid and open for the lifetime of encoding.","Retry the failing bundle if this occurred during distributed execution (runners usually retry automatically).","Check that the writer is not nil; WriteUnsafe on a nil writer will fail."],"exampleFix":"// before\nvar w io.Writer // nil\n coder.EncodeByte(0x1, w)\n// after\nvar w io.Writer = &bytes.Buffer{}\nif err := coder.EncodeByte(0x1, w); err != nil { log.Fatal(err) }","handlingStrategy":"try-catch","validationCode":"if w == nil { return errors.New(\"writer must be non-nil before encoding bytes\") }","typeGuard":null,"tryCatchPattern":"if err := coder.EncodeByte(v, w); err != nil {\n\tif errors.Is(err, io.ErrClosedPipe) || errors.Is(err, io.ErrUnexpectedEOF) {\n\t\t// re-open stream and retry the record\n\t}\n\treturn err\n}","preventionTips":["Keep the bundle's writer stream open for the full encoding phase.","Never pass nil io.Writer to coders.","Rely on runner-level bundle retries for transient stream failures."],"tags":["go","beam","serialization","io"],"backgroundTag":"file-write-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}