{"record":{"id":"984b79c12ce44559","repo":"nats-io/nats-server","slug":"stream-state-detected-prior-state-could-not-locat","errorCode":null,"errorMessage":"Stream state detected prior state, could not locate msg block %d","messagePattern":"Stream state detected prior state, could not locate msg block (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/filestore.go","lineNumber":2222,"sourceCode":"\t\t}\n\t\tif n, err := fmt.Sscanf(fi.Name(), blkScan, &index); err == nil && n == 1 {\n\t\t\tif index > blkIndex {\n\t\t\t\tfs.warn(\"Stream state outdated, found extra blocks, will rebuild\")\n\t\t\t\treturn errPriorState\n\t\t\t} else if mb, ok := fs.bim[index]; ok {\n\t\t\t\tmb.closed = false\n\t\t\t}\n\t\t}\n\t}\n\n\tvar rebuild bool\n\tfor _, mb := range fs.blks {\n\t\tif mb.closed {\n\t\t\trebuild = true\n\t\t\tif ld, _, _ := mb.rebuildState(); ld != nil {\n\t\t\t\tfs.addLostData(ld)\n\t\t\t}\n\t\t\tfs.warn(\"Stream state detected prior state, could not locate msg block %d\", mb.index)\n\t\t}\n\t}\n\tif rebuild {\n\t\treturn errPriorState\n\t}\n\n\t// We check first and last seq and number of msgs and bytes. If there is a difference,\n\t// return and error so we rebuild from the message block state on disk.\n\tif !trackingStatesEqual(&fs.state, &mstate) {\n\t\t_ = os.Remove(fn)\n\t\tfs.warn(\"Stream state encountered internal inconsistency on recover\")\n\t\treturn errCorruptState\n\t}\n\n\treturn nil\n}\n\n// Recover per-message state like TTLs and schedules. Doing a linear scan through the stream","sourceCodeStart":2204,"sourceCodeEnd":2240,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/filestore.go#L2204-L2240","documentation":"During stream recovery, a message block marked closed (its file was missing on disk even though the block index expected it, e.g. after a crash or manual deletion) could not be located. The store rebuilds state from remaining blocks, records any lost data, and returns errPriorState so callers know prior state was superseded.","triggerScenarios":"Thrown at server/filestore.go:2222 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check for manually deleted or moved message block files under the msg dir","Let the rebuild complete; lost data is tracked via addLostData","Restore block files from backup if data loss is unacceptable"],"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"}