nats-io/nats-server · error

indexCacheBuf corrupt state in %s: mb.first %d mb.last %d

Error message

indexCacheBuf corrupt state in %s: mb.first %d mb.last %d

What it means

While indexing a raw message-block buffer into the cache, mb.first was greater than mb.last+1 (only legal purged state is first == last+1). This would make the computed index size wrap, so indexCacheBuf aborts with errCorruptState instead of allocating a bogus buffer.

Source

Thrown at server/filestore.go:8457

	}
	return nil
}

// Index a raw msg buffer.
// Lock should be held.
func (mb *msgBlock) indexCacheBuf(buf []byte) error {
	var le = binary.LittleEndian

	var alloc bool
	var idx []uint32
	var index uint32

	mbFirstSeq := atomic.LoadUint64(&mb.first.seq)
	mbLastSeq := atomic.LoadUint64(&mb.last.seq)

	// Sanity check here since we calculate size to allocate based on this.
	if mbFirstSeq > (mbLastSeq + 1) { // Purged state first == last + 1
		mb.fs.warn("indexCacheBuf corrupt state in %s: mb.first %d mb.last %d", mb.mfn, mbFirstSeq, mbLastSeq)
		// This would cause idxSz to wrap.
		return errCorruptState
	}

	idxSz := mbLastSeq - mbFirstSeq + 1

	if mb.cache == nil {
		mb.cache = mb.ecache.Value()
	}
	if mb.cache == nil {
		mb.cache = &cache{}
		alloc = true
	} else {
		// The buf arg already came from the pool probably, so there's
		// no point in reusing mb.cache.buf's underlying capacity here.
		// Just recycle it for the next block load.
		mb.cache.stopRecycle()
		recycleMsgBlockBuf(mb.cache.buf)

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Treat the block as corrupt: let recovery rebuild the block or report lost data
  2. Check for disk corruption or partial writes to the msg block file
  3. Restore the block from backup if the data is critical
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at server/filestore.go:8457 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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