nats-io/nats-server · warning

accounting inconsistent

Error message

accounting inconsistent

What it means

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.

Source

Thrown at server/filestore.go:10178

	}
	mb.msgs = readCount()
	mb.bytes = readCount()
	atomic.StoreUint64(&mb.first.seq, readSeq())
	mb.first.ts = readTimeStamp()
	atomic.StoreUint64(&mb.last.seq, readSeq())
	mb.last.ts = readTimeStamp()
	dmapLen := readCount()

	// Check if this is a short write index file.
	if bi < 0 || bi+checksumSize > len(buf) {
		os.Remove(ifn)
		return fmt.Errorf("short index file")
	}

	// Check for consistency if accounting. If something is off bail and we will rebuild.
	if mb.msgs != (atomic.LoadUint64(&mb.last.seq)-atomic.LoadUint64(&mb.first.seq)+1)-dmapLen {
		os.Remove(ifn)
		return fmt.Errorf("accounting inconsistent")
	}

	// Checksum
	copy(mb.lchk[0:], buf[bi:bi+checksumSize])
	bi += checksumSize

	// Now check for presence of a delete map
	if dmapLen > 0 {
		// New version is encoded avl seqset.
		if buf[1] == newVersion {
			dmap, _, err := avl.Decode(buf[bi:])
			if err != nil {
				return fmt.Errorf("could not decode avl dmap: %v", err)
			}
			mb.dmap = *dmap
		} else {
			// This is the old version.
			for i, fseq := 0, atomic.LoadUint64(&mb.first.seq); i < int(dmapLen); i++ {

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Retry/restart — the inconsistent index is removed and rebuilt automatically.
  2. Check server logs for the surrounding rebuild activity to confirm recovery succeeded.
  3. If corruption recurs, verify storage health and stop mixing data directories across NATS versions.
  4. Upgrade NATS Server if running a version with known accounting bugs in index writing.
Defensive patterns

Strategy: fallback

Try / catch

if err != nil && strings.Contains(err.Error(), "accounting inconsistent") {
    // index deleted and rebuilt automatically; confirm stream message counts recover
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of nats-io/nats-server@3a66a489d2 (2026-09-02). Data as JSON: /api/errors/94802af6758180dc. Report an issue: GitHub.