{"record":{"id":"28f9721422345af8","repo":"crowdsecurity/crowdsec","slug":"while-initializing-cache-w","errorCode":null,"errorMessage":"while initializing cache: %w","messagePattern":"while initializing cache: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/parser/stash.go","lineNumber":95,"sourceCode":"\t}\n\n\trs.TTLVal, err = time.ParseDuration(s.TTL)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"while parsing stash ttl: %w\", err)\n\t}\n\n\t// init the cache, does it make sense to create it here just to be sure everything is fine ?\n\n\tcacheCfg := cache.CacheCfg{\n\t\tSize:     s.MaxMapSize,\n\t\tTTL:      rs.TTLVal,\n\t\tName:     s.Name,\n\t\tStrategy: s.Strategy,\n\t\tLogLevel: logger.Logger.GetLevel(),\n\t}\n\n\tif err = cache.CacheInit(cacheCfg, cacheCfg.NewLogger()); err != nil {\n\t\treturn nil, fmt.Errorf(\"while initializing cache: %w\", err)\n\t}\n\n\treturn rs, nil\n}\n\nfunc (rs *RuntimeStash) Apply(idx int, cachedExprEnv map[string]any, logger *log.Entry, debug bool) {\n\tvar (\n\t\tkey   string\n\t\tvalue string\n\t)\n\n\tif rs.ValueExpression == nil {\n\t\tlogger.Warningf(\"Stash %d has no value expression, skipping\", idx)\n\t\treturn\n\t}\n\n\tif rs.KeyExpression == nil {\n\t\tlogger.Warningf(\"Stash %d has no key expression, skipping\", idx)","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/parser/stash.go#L77-L113","documentation":"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.","triggerScenarios":"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`.","commonSituations":"Setting `strategy: FIFO` or `strategy: lfu` in the stash config; setting max_cache_size to 0 or a negative number.","solutions":["Set `strategy: LRU` (the only supported value; it is also the default).","Ensure `max_cache_size` (if set) is a positive integer.","Check logs for the nested cache error to rule out memory/logger issues."],"exampleFix":"// before\nstrategy: FIFO\n// after\nstrategy: LRU","handlingStrategy":"validation","validationCode":"allowed := map[string]bool{\"LRU\": true}\nif s.Strategy != \"\" && !allowed[strings.ToUpper(s.Strategy)] {\n    return fmt.Errorf(\"stash %q: unsupported strategy %q (use LRU)\", s.Name, s.Strategy)\n}\nif s.MaxMapSize <= 0 { return fmt.Errorf(\"stash %q: max_cache_size must be > 0\", s.Name) }","typeGuard":null,"tryCatchPattern":"rs, err := stash.Compile()\nif err != nil {\n    if strings.Contains(err.Error(), \"initializing cache\") { /* check strategy/size */ }\n    return err\n}","preventionTips":["Only use `strategy: LRU` (or omit strategy entirely).","Keep max_cache_size a positive integer; test stash configs in staging."],"tags":["parser","stash","cache","config"],"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-14T00:17:10.932Z"}