{"record":{"id":"7cd3841798b8df6b","repo":"thanos-io/thanos","slug":"invalid-magic-number-x","errorCode":null,"errorMessage":"invalid magic number %x","messagePattern":"invalid magic number %x","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/block/indexheader/binary_reader.go","lineNumber":674,"sourceCode":"\n\treturn &BinaryTOC{\n\t\tSymbols:             d.Be64(),\n\t\tPostingsOffsetTable: d.Be64(),\n\t}, nil\n}\n\nfunc (r *BinaryReader) init() (err error) {\n\tstart := time.Now()\n\n\tdefer func() {\n\t\tr.metrics.loadDuration.Observe(time.Since(start).Seconds())\n\t}()\n\t// Verify header.\n\tif r.b.Len() < headerLen {\n\t\treturn errors.Wrap(encoding.ErrInvalidSize, \"index header's header\")\n\t}\n\tif m := binary.BigEndian.Uint32(r.b.Range(0, 4)); m != MagicIndex {\n\t\treturn errors.Errorf(\"invalid magic number %x\", m)\n\t}\n\tr.version = int(r.b.Range(4, 5)[0])\n\tr.indexVersion = int(r.b.Range(5, 6)[0])\n\n\tr.indexLastPostingEnd = int64(binary.BigEndian.Uint64(r.b.Range(6, headerLen)))\n\n\tif r.version != BinaryFormatV1 {\n\t\treturn errors.Errorf(\"unknown index header file version %d\", r.version)\n\t}\n\n\tr.toc, err = newBinaryTOCFromByteSlice(r.b)\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"read index header TOC\")\n\t}\n\n\t// TODO(bwplotka): Consider contributing to Prometheus to allow specifying custom number for symbolsFactor.\n\tr.symbols, err = index.NewSymbols(r.b, r.indexVersion, int(r.toc.Symbols))\n\tif err != nil {","sourceCodeStart":656,"sourceCodeEnd":692,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/pkg/block/indexheader/binary_reader.go#L656-L692","documentation":"While validating the binary index-header header, the first 4 bytes must equal MagicIndex. This error is raised when the magic number read from the buffer does not match, meaning the data at the cache path is not a Thanos binary index-header of the expected format.","triggerScenarios":"newBinaryReader/validate on a buffer whose first 4 bytes differ from MagicIndex: the cache file is some other file (e.g., the raw index), the file was written by a different/older format, or the bytes are scrambled garbage.","commonSituations":"Cache path pointing to the wrong file; manually copied files; files produced by an incompatible Thanos/Thanos-fork version; bit-rot corruption of the first bytes.","solutions":["Delete the cached file at that path and let the library regenerate it from the bucket.","Verify you are reading the intended .index-header file, not the full index or another file.","Check Thanos version compatibility between writer and reader of the cache file.","If corruption recurs on the same volume, check disk health."],"exampleFix":"null","handlingStrategy":"validation","validationCode":"b, err := os.ReadFile(hdrPath)\nif err == nil && len(b) >= 4 && binary.BigEndian.Uint32(b[:4]) != MagicIndex {\n    os.Remove(hdrPath) // wrong file or corruption; regenerate\n}","typeGuard":"func hasIndexMagic(b []byte) bool {\n    return len(b) >= 4 && binary.BigEndian.Uint32(b[:4]) == MagicIndex\n}","tryCatchPattern":"r, err := NewBinaryReader(ctx, logger, bkt, dir, id, sampling, metrics)\nif err != nil && strings.Contains(err.Error(), \"invalid magic number\") {\n    os.Remove(filepath.Join(dir, id.String()+\".index-header\"))\n    return NewBinaryReader(ctx, logger, bkt, dir, id, sampling, metrics)\n}","preventionTips":["Never write other files to the cache path of an index-header.","Keep consistent Thanos versions across writers and readers of the cache.","Delete unrecognizable cache files instead of failing queries.","Check memory/disk corruption if magic bytes flip randomly."],"tags":["magic-number","index-header","corruption","format"],"backgroundTag":"invalid-argument-format","analyzedSha":"35b8b991177def87ed52dcf10f9b6d87f07282c8","analyzedAt":"2026-09-07T01:49:59.689Z","contentChangedAt":"2026-09-07T01:49:59.689Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}