{"record":{"id":"03f035fdf53ea527","repo":"nats-io/nats-server","slug":"enforcemsgpersubjectlimit-took-v","errorCode":null,"errorMessage":"enforceMsgPerSubjectLimit took %v","messagePattern":"enforceMsgPerSubjectLimit took (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/filestore.go","lineNumber":5925,"sourceCode":"\t\t}\n\t\tif removed, err := fs.deleteFirstMsg(); err != nil {\n\t\t\treturn err\n\t\t} else if !removed {\n\t\t\treturn fs.rebuildFirst()\n\t\t}\n\t}\n\treturn nil\n}\n\n// Will make sure we have limits honored for max msgs per subject on recovery or config update.\n// We will make sure to go through all msg blocks etc. but in practice this\n// will most likely only be the last one, so can take a more conservative approach.\n// Lock should be held.\nfunc (fs *fileStore) enforceMsgPerSubjectLimit(fireCallback bool) error {\n\tstart := time.Now()\n\tdefer func() {\n\t\tif took := time.Since(start); took > time.Minute {\n\t\t\tfs.warn(\"enforceMsgPerSubjectLimit took %v\", took.Round(time.Millisecond))\n\t\t}\n\t}()\n\n\tmaxMsgsPer := uint64(fs.cfg.MaxMsgsPer)\n\n\t// We may want to suppress callbacks from remove during this process\n\t// since these should have already been deleted and accounted for.\n\tif !fireCallback {\n\t\tcb := fs.scb\n\t\tfs.scb = nil\n\t\tdefer func() { fs.scb = cb }()\n\t}\n\n\tvar numMsgs uint64\n\n\t// collect all that are not correct.\n\tneedAttention := stree.NewSubjectTree[uint64]()\n\tfblk, lblk := uint32(math.MaxUint32), uint32(0)","sourceCodeStart":5907,"sourceCodeEnd":5943,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/filestore.go#L5907-L5943","documentation":"A performance watchdog, not an error: enforceMsgPerSubjectLimit (re-applying max msgs per subject across all blocks, typically on recovery or config update) exceeded 1 minute. It indicates a very large subject-space being swept under lock.","triggerScenarios":"Thrown at server/filestore.go:5925 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Review the MaxMsgsPerSubject limit against the number of distinct subjects","Batch or reduce the stream's subject cardinality","Take a CPU profile during recovery if the slowness is unexpected"],"exampleFix":null,"handlingStrategy":"validation","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"}