crowdsecurity/crowdsec · error

while initializing cache: %w

Error message

while initializing cache: %w

What it means

Once expressions and TTL are valid, Stash.Compile() creates the backing cache via cache.CacheInit with the configured size and strategy. If cache initialization fails (bad strategy, invalid size, internal logger error) the stash cannot be created and the cause is wrapped here.

Source

Thrown at pkg/parser/stash.go:95

	}

	rs.TTLVal, err = time.ParseDuration(s.TTL)
	if err != nil {
		return nil, fmt.Errorf("while parsing stash ttl: %w", err)
	}

	// init the cache, does it make sense to create it here just to be sure everything is fine ?

	cacheCfg := cache.CacheCfg{
		Size:     s.MaxMapSize,
		TTL:      rs.TTLVal,
		Name:     s.Name,
		Strategy: s.Strategy,
		LogLevel: logger.Logger.GetLevel(),
	}

	if err = cache.CacheInit(cacheCfg, cacheCfg.NewLogger()); err != nil {
		return nil, fmt.Errorf("while initializing cache: %w", err)
	}

	return rs, nil
}

func (rs *RuntimeStash) Apply(idx int, cachedExprEnv map[string]any, logger *log.Entry, debug bool) {
	var (
		key   string
		value string
	)

	if rs.ValueExpression == nil {
		logger.Warningf("Stash %d has no value expression, skipping", idx)
		return
	}

	if rs.KeyExpression == nil {
		logger.Warningf("Stash %d has no key expression, skipping", idx)

View on GitHub (pinned to 909b515798)

Solutions

  1. Set `strategy: LRU` (the only supported value; it is also the default).
  2. Ensure `max_cache_size` (if set) is a positive integer.
  3. Check logs for the nested cache error to rule out memory/logger issues.

Example fix

// before
strategy: FIFO
// after
strategy: LRU
Defensive patterns

Strategy: validation

Validate before calling

allowed := map[string]bool{"LRU": true}
if s.Strategy != "" && !allowed[strings.ToUpper(s.Strategy)] {
    return fmt.Errorf("stash %q: unsupported strategy %q (use LRU)", s.Name, s.Strategy)
}
if s.MaxMapSize <= 0 { return fmt.Errorf("stash %q: max_cache_size must be > 0", s.Name) }

Try / catch

rs, err := stash.Compile()
if err != nil {
    if strings.Contains(err.Error(), "initializing cache") { /* check strategy/size */ }
    return err
}

Prevention

When it happens

Trigger: Compile calls cache.CacheInit(cacheCfg, cacheCfg.NewLogger()) and it returns an error — typically an unsupported `strategy:` value (only LRU is supported) or an invalid/negative `max_cache_size`.

Common situations: Setting `strategy: FIFO` or `strategy: lfu` in the stash config; setting max_cache_size to 0 or a negative number.

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/28f9721422345af8. Report an issue: GitHub.