{"record":{"id":"94802af6758180dc","repo":"nats-io/nats-server","slug":"accounting-inconsistent","errorCode":null,"errorMessage":"accounting inconsistent","messagePattern":"accounting inconsistent","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"server/filestore.go","lineNumber":10178,"sourceCode":"\t}\n\tmb.msgs = readCount()\n\tmb.bytes = readCount()\n\tatomic.StoreUint64(&mb.first.seq, readSeq())\n\tmb.first.ts = readTimeStamp()\n\tatomic.StoreUint64(&mb.last.seq, readSeq())\n\tmb.last.ts = readTimeStamp()\n\tdmapLen := readCount()\n\n\t// Check if this is a short write index file.\n\tif bi < 0 || bi+checksumSize > len(buf) {\n\t\tos.Remove(ifn)\n\t\treturn fmt.Errorf(\"short index file\")\n\t}\n\n\t// Check for consistency if accounting. If something is off bail and we will rebuild.\n\tif mb.msgs != (atomic.LoadUint64(&mb.last.seq)-atomic.LoadUint64(&mb.first.seq)+1)-dmapLen {\n\t\tos.Remove(ifn)\n\t\treturn fmt.Errorf(\"accounting inconsistent\")\n\t}\n\n\t// Checksum\n\tcopy(mb.lchk[0:], buf[bi:bi+checksumSize])\n\tbi += checksumSize\n\n\t// Now check for presence of a delete map\n\tif dmapLen > 0 {\n\t\t// New version is encoded avl seqset.\n\t\tif buf[1] == newVersion {\n\t\t\tdmap, _, err := avl.Decode(buf[bi:])\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"could not decode avl dmap: %v\", err)\n\t\t\t}\n\t\t\tmb.dmap = *dmap\n\t\t} else {\n\t\t\t// This is the old version.\n\t\t\tfor i, fseq := 0, atomic.LoadUint64(&mb.first.seq); i < int(dmapLen); i++ {","sourceCodeStart":10160,"sourceCodeEnd":10196,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/filestore.go#L10160-L10196","documentation":"Returned when the loaded index file's accounting is inconsistent: the number of messages recorded in the block (mb.msgs) does not equal (last.seq - first.seq + 1) minus the number of deleted-message map entries (dmapLen). The store deletes the index and returns this error, forcing an index rebuild from authoritative block data.","triggerScenarios":"Loading an index where mb.msgs != (last.seq - first.seq + 1) - dmapLen — the index's message accounting disagrees with the sequence range and deletion map.","commonSituations":"Indexes written by a different (older/buggy) server version, corruption of the index or block metadata after crashes, or manually edited/copied stream data directories.","solutions":["Retry/restart — the inconsistent index is removed and rebuilt automatically.","Check server logs for the surrounding rebuild activity to confirm recovery succeeded.","If corruption recurs, verify storage health and stop mixing data directories across NATS versions.","Upgrade NATS Server if running a version with known accounting bugs in index writing."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"accounting inconsistent\") {\n    // index deleted and rebuilt automatically; confirm stream message counts recover\n}","preventionTips":["Upgrade NATS Server if index accounting bugs exist in your version.","Never manually edit stream data files.","Restore backups as whole consistent sets (block + index together).","Watch for repeated rebuilds as an indicator of storage corruption."],"tags":["filestore","index","accounting","consistency","go"],"backgroundTag":"index-accounting-inconsistent","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}