{"record":{"id":"19c789ae081e7ec6","repo":"crowdsecurity/crowdsec","slug":"max-items-can-t-be-zero-or-negative","errorCode":null,"errorMessage":"max_items can't be zero or negative","messagePattern":"max_items can't be zero or negative","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/database/flush.go","lineNumber":42,"sourceCode":"\t\"github.com/crowdsecurity/crowdsec/pkg/types\"\n)\n\nconst (\n\t// how long to keep metrics in the local database\n\tdefaultMetricsMaxAge = 7 * 24 * time.Hour\n\tflushInterval        = 1 * time.Minute\n\t// orphan metas are deleted in bounded batches: an install upgrading with a\n\t// backlog can carry hundreds of millions of them, and a single unbounded\n\t// DELETE would hold the table for minutes.\n\torphanMetaBatchSize = 5_000\n\torphanMetaMaxPerRun = 500_000\n)\n\nfunc (c *Client) StartFlushScheduler(ctx context.Context, config *csconfig.FlushDBCfg) (gocron.Scheduler, error) {\n\tmaxItems := 0\n\n\tif config.MaxItems != nil && *config.MaxItems <= 0 {\n\t\treturn nil, errors.New(\"max_items can't be zero or negative\")\n\t}\n\n\tif config.MaxItems != nil {\n\t\tmaxItems = *config.MaxItems\n\t}\n\n\t// Init & Start cronjob every minute for alerts\n\tscheduler, err := gocron.NewScheduler(\n\t\tgocron.WithLocation(time.UTC),\n\t\tgocron.WithLogger(logging.GoCronLoggerAdapter{Logger: c.Log}),\n\t)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\t_, err = scheduler.NewJob(\n\t\tgocron.DurationJob(1*time.Minute),\n\t\tgocron.NewTask(c.FlushAlerts, ctx, time.Duration(config.MaxAge), maxItems),","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/flush.go#L24-L60","documentation":"StartFlushScheduler validates the flush configuration before starting the periodic DB flush job. If config.MaxItems is set (non-nil pointer) but its value is <= 0, no sane flush threshold exists, so it refuses to start. The zero-value semantics of MaxItems (nil = unset) mean the check only applies when the operator explicitly configured max_items.","triggerScenarios":"Calling StartFlushScheduler with a csconfig.FlushDBCfg whose MaxItems points to 0 or a negative integer, typically from a config file containing `max_items: 0` or `max_items: -5`.","commonSituations":"Operator sets max_items: 0 in config.yaml thinking it disables flush-based deletion; templating tools render an empty variable to 0; a migration script writes a negative default.","solutions":["Set max_items to a positive integer in the flush configuration, e.g. max_items: 100000","Remove the max_items key entirely (nil MaxItems is allowed) to use the default behavior","Guard the config value before calling StartFlushScheduler"],"exampleFix":"// before (config.yaml)\nmax_items: 0\n// after (config.yaml)\nmax_items: 100000","handlingStrategy":"validation","validationCode":"if cfg.Flush.MaxItems != nil && *cfg.Flush.MaxItems <= 0 {\n    return fmt.Errorf(\"max_items must be > 0, got %d\", *cfg.Flush.MaxItems)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never set max_items to 0; omit the key to use defaults","Add config validation at startup before constructing the DB client","Template/render configs with explicit positive defaults"],"tags":["config","database","crowdsec"],"backgroundTag":"invalid-config-value","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}