crowdsecurity/crowdsec · error

max_items can't be zero or negative

Error message

max_items can't be zero or negative

What it means

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.

Source

Thrown at pkg/database/flush.go:42

	"github.com/crowdsecurity/crowdsec/pkg/types"
)

const (
	// how long to keep metrics in the local database
	defaultMetricsMaxAge = 7 * 24 * time.Hour
	flushInterval        = 1 * time.Minute
	// orphan metas are deleted in bounded batches: an install upgrading with a
	// backlog can carry hundreds of millions of them, and a single unbounded
	// DELETE would hold the table for minutes.
	orphanMetaBatchSize = 5_000
	orphanMetaMaxPerRun = 500_000
)

func (c *Client) StartFlushScheduler(ctx context.Context, config *csconfig.FlushDBCfg) (gocron.Scheduler, error) {
	maxItems := 0

	if config.MaxItems != nil && *config.MaxItems <= 0 {
		return nil, errors.New("max_items can't be zero or negative")
	}

	if config.MaxItems != nil {
		maxItems = *config.MaxItems
	}

	// Init & Start cronjob every minute for alerts
	scheduler, err := gocron.NewScheduler(
		gocron.WithLocation(time.UTC),
		gocron.WithLogger(logging.GoCronLoggerAdapter{Logger: c.Log}),
	)
	if err != nil {
		return nil, err
	}

	_, err = scheduler.NewJob(
		gocron.DurationJob(1*time.Minute),
		gocron.NewTask(c.FlushAlerts, ctx, time.Duration(config.MaxAge), maxItems),

View on GitHub (pinned to 909b515798)

Solutions

  1. Set max_items to a positive integer in the flush configuration, e.g. max_items: 100000
  2. Remove the max_items key entirely (nil MaxItems is allowed) to use the default behavior
  3. Guard the config value before calling StartFlushScheduler

Example fix

// before (config.yaml)
max_items: 0
// after (config.yaml)
max_items: 100000
Defensive patterns

Strategy: validation

Validate before calling

if cfg.Flush.MaxItems != nil && *cfg.Flush.MaxItems <= 0 {
    return fmt.Errorf("max_items must be > 0, got %d", *cfg.Flush.MaxItems)
}

Prevention

When it happens

Trigger: 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`.

Common situations: 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.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06). Data as JSON: /api/errors/19c789ae081e7ec6. Report an issue: GitHub.