{"record":{"id":"5bdae9a356dd1f6a","repo":"nats-io/nats-server","slug":"stream-state-encountered-internal-inconsistency-on","errorCode":null,"errorMessage":"Stream state encountered internal inconsistency on write","messagePattern":"Stream state encountered internal inconsistency on write","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/filestore.go","lineNumber":12358,"sourceCode":"\tfn := filepath.Join(fs.fcfg.StoreDir, msgDir, streamStreamStateFile)\n\n\t// Need to have our own hasher here, as under a read lock we can't mutate the\n\t// fs.hh safely.\n\tkey := sha256.Sum256([]byte(fs.cfg.Name))\n\thh, _ := highwayhash.NewDigest64(key[:])\n\thh.Write(buf)\n\tbuf = hh.Sum(buf)\n\n\t// Snapshot prior dirty count.\n\tpriorDirty := fs.dirty\n\n\tstatesEqual := trackingStatesEqual(&fs.state, &mstate)\n\t// Release lock.\n\tfs.mu.RUnlock()\n\n\t// Check consistency here.\n\tif !statesEqual {\n\t\tfs.warn(\"Stream state encountered internal inconsistency on write\")\n\t\t// Rebuild our fs state from the mb state.\n\t\tfs.rebuildState(nil)\n\t\treturn errCorruptState\n\t}\n\n\tif cap(buf) > sz {\n\t\tfs.debug(\"WriteFullState reallocated from %d to %d\", sz, cap(buf))\n\t}\n\n\t// Only warn about construction time since file write not holding any locks.\n\tif took := time.Since(start); took > time.Minute {\n\t\tfs.warn(\"WriteFullState took %v (%d bytes)\", took.Round(time.Millisecond), len(buf))\n\t}\n\n\t// Write our update index.db\n\t// Protect with dios.\n\tfs.dios.acquire()\n\terr := os.WriteFile(fn, buf, defaultFilePerms)","sourceCodeStart":12340,"sourceCodeEnd":12376,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/filestore.go#L12340-L12376","documentation":"While persisting the stream state file, the in-memory fs.state diverged from the state aggregated from message blocks (trackingStatesEqual returned false). The store rebuilds its state from the blocks and returns errCorruptState rather than writing inconsistent accounting to disk.","triggerScenarios":"Thrown at server/filestore.go:12358 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["The automatic rebuildState reconciles the counters; verify stream info afterwards","Run message integrity checks on the stream","If it recurs, suspect a bug or failing storage and report with logs"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}