{"record":{"id":"0a8d4e56982e6002","repo":"nats-io/nats-server","slug":"detected-skew-in-subject-based-total-d-vs-raw-t","errorCode":null,"errorMessage":"Detected skew in subject-based total (%d) vs raw total (%d), rebuilding","messagePattern":"Detected skew in subject-based total \\((.+?)\\) vs raw total \\((.+?)\\), rebuilding","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/filestore.go","lineNumber":5961,"sourceCode":"\tfblk, lblk := uint32(math.MaxUint32), uint32(0)\n\tfs.psim.IterFast(func(subj []byte, psi *psi) bool {\n\t\tnumMsgs += psi.total\n\t\tif psi.total > maxMsgsPer {\n\t\t\tneedAttention.Insert(subj, psi.total)\n\t\t\tif psi.fblk < fblk {\n\t\t\t\tfblk = psi.fblk\n\t\t\t}\n\t\t\tif psi.lblk > lblk {\n\t\t\t\tlblk = psi.lblk\n\t\t\t}\n\t\t}\n\t\treturn true\n\t})\n\n\t// We had an issue with a use case where psim (and hence fss) were correct but idx was not and was not properly being caught.\n\t// So do a quick sanity check here. If we detect a skew do a rebuild then re-check.\n\tif numMsgs != fs.state.Msgs {\n\t\tfs.warn(\"Detected skew in subject-based total (%d) vs raw total (%d), rebuilding\", numMsgs, fs.state.Msgs)\n\t\t// Clear any global subject state.\n\t\tfs.psim, fs.tsl = fs.psim.Empty(), 0\n\t\tfor _, mb := range fs.blks {\n\t\t\tld, _, err := mb.rebuildState()\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t} else if ld != nil {\n\t\t\t\tfs.addLostData(ld)\n\t\t\t}\n\t\t\tif err = fs.populateGlobalPerSubjectInfo(mb); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\t// Rebuild fs state too.\n\t\tfs.rebuildStateLocked(nil)\n\t\t// Need to redo blocks that need attention.\n\t\tneedAttention.Empty()\n\t\tfblk, lblk = uint32(math.MaxUint32), uint32(0)","sourceCodeStart":5943,"sourceCodeEnd":5979,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/filestore.go#L5943-L5979","documentation":"A consistency check found the sum of per-subject message totals (psim) differs from the stream's raw message count — index/subject bookkeeping has diverged (a known past bug class). The store rebuilds its state from the message blocks to re-sync the counters.","triggerScenarios":"Thrown at server/filestore.go:5961 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Let the automatic rebuildState repair the skew","Run stream and message integrity checks (JS API) after recovery","Report recurring skew as a bug with stream details"],"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-08T15:18:49.778Z"}