{"record":{"id":"a5d2a66f400783ca","repo":"golang/go","slug":"error-reading-profile-header-w","errorCode":null,"errorMessage":"error reading profile header: %w","messagePattern":"error reading profile header: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/internal/pgo/deserialize.go","lineNumber":25,"sourceCode":"import (\n\t\"bufio\"\n\t\"fmt\"\n\t\"io\"\n\t\"strconv\"\n\t\"strings\"\n)\n\n// IsSerialized returns true if r is a serialized Profile.\n//\n// IsSerialized only peeks at r, so seeking back after calling is not\n// necessary.\nfunc IsSerialized(r *bufio.Reader) (bool, error) {\n\thdr, err := r.Peek(len(serializationHeader))\n\tif err == io.EOF {\n\t\t// Empty file.\n\t\treturn false, nil\n\t} else if err != nil {\n\t\treturn false, fmt.Errorf(\"error reading profile header: %w\", err)\n\t}\n\n\treturn string(hdr) == serializationHeader, nil\n}\n\n// FromSerialized parses a profile from serialization output of Profile.WriteTo.\nfunc FromSerialized(r io.Reader) (*Profile, error) {\n\td := emptyProfile()\n\n\tscanner := bufio.NewScanner(r)\n\tscanner.Split(bufio.ScanLines)\n\n\tif !scanner.Scan() {\n\t\tif err := scanner.Err(); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error reading preprocessed profile: %w\", err)\n\t\t}\n\t\treturn nil, fmt.Errorf(\"preprocessed profile missing header\")\n\t}","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/internal/pgo/deserialize.go#L7-L43","documentation":"IsSerialized peeks at the first len(serializationHeader) bytes of a buffered reader to decide whether the stream is a serialized PGO profile. EOF is treated as 'empty file, not serialized' (not an error); any other peek failure is wrapped and returned. This guards FromSerialized against consuming the wrong kind of stream.","triggerScenarios":"r.Peek fails with an error other than io.EOF — e.g. an underlying reader that errors mid-stream, a network-backed reader that drops, or a corrupted file descriptor.","commonSituations":"Reading a profile over an unstable source (pipe, socket, network), a truncated file, or a reader that has already been closed.","solutions":["Verify the reader is healthy and positioned at the start of the profile.","Buffer the profile into a bytes.Reader from a verified file before calling IsSerialized.","Retry once if the source is transiently failing."],"exampleFix":"// before: reader may fail mid-peek\nok, err := pgo.IsSerialized(bufReader)\n\n// after: materialize then peek a stable reader\ndata, err := os.ReadFile(profilePath)\nif err != nil { return err }\nok, err := pgo.IsSerialized(bufio.NewReader(bytes.NewReader(data)))","handlingStrategy":"validation","validationCode":"// Materialize the profile so Peek operates on a stable reader.\n// data, err := os.ReadFile(path)\n// if err != nil { return err }\n// br := bufio.NewReader(bytes.NewReader(data))\n// ok, err := pgo.IsSerialized(br)\n// if err != nil { return fmt.Errorf(\"profile unreadable: %w\", err) }","typeGuard":null,"tryCatchPattern":"// ok, err := pgo.IsSerialized(br)\n// if err != nil {\n//     // header peek failed: bad reader or truncated; retry from a buffered copy\n//     return err\n// }","preventionTips":["Buffer the profile into memory before peeking.","Ensure the reader is positioned at offset 0 and healthy.","Retry once for transient reader failures."],"tags":["pgo","profile","io","deserialize","go-toolchain"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}