{"record":{"id":"2ee6ebb6fc5c8230","repo":"crowdsecurity/crowdsec","slug":"fail-to-count-decisions-w","errorCode":null,"errorMessage":"fail to count decisions: %w","messagePattern":"fail to count decisions: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/database/decisions.go","lineNumber":406,"sourceCode":"\tcontains := true\n\tdecisions := c.Ent.Decision.Query()\n\n\tdecisions, err = decisionIPFilter(decisions, contains, rng)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"fail to apply StartIpEndIpFilter: %w\", err)\n\t}\n\n\tif since != nil {\n\t\tdecisions = decisions.Where(decision.CreatedAtGT(*since))\n\t}\n\n\tif onlyActive {\n\t\tdecisions = decisions.Where(decision.UntilGT(time.Now().UTC()))\n\t}\n\n\tcount, err := decisions.Count(ctx)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"fail to count decisions: %w\", err)\n\t}\n\n\treturn count, nil\n}\n\nfunc (c *Client) GetActiveDecisionsTimeLeftByValue(ctx context.Context, decisionValue string) (time.Duration, error) {\n\trng, err := csnet.NewRange(decisionValue)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"unable to convert '%s' to int: %w\", decisionValue, err)\n\t}\n\n\tcontains := true\n\tdecisions := c.Ent.Decision.Query().Where(\n\t\tdecision.UntilGT(time.Now().UTC()),\n\t)\n\n\tdecisions, err = decisionIPFilter(decisions, contains, rng)\n\tif err != nil {","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/decisions.go#L388-L424","documentation":"CountDecisionsByValue failed while executing the ent ORM COUNT query against the decisions table. The error wraps the underlying database/driver error after building the query (optionally filtered to active decisions via `until > now`). It indicates the count could not be retrieved from the database backend (SQLite, MySQL, PostgreSQL, etc.).","triggerScenarios":"Calling CountDecisionsByValue with a decision value/type scope when the underlying ent Count(ctx) query fails: DB unreachable, table/schema missing or corrupted, driver error, or query timeout.","commonSituations":"CrowdSec's DB is down or restarting; SQLite file corrupted or on a read-only/full disk; MySQL/Postgres credentials or network changed after startup; schema migration not applied after upgrade.","solutions":["Verify the database is reachable and credentials in crowdsec.db / /etc/crowdsec/config.yaml are correct.","Check disk space and file permissions if using SQLite.","Run `cscli hubtool` / check crowdsec logs for the wrapped driver error to identify the root cause.","Apply pending migrations: `cscli migrations` / restart crowdsec to run schema migrations."],"exampleFix":"// before: assuming DB is always up\nexpiredAlerts, err := client.CountDecisionsByValue(ctx, \"1.2.3.4\", \"\", \"\", false)\n// after: handle DB failure gracefully\nexpiredAlerts, err := client.CountDecisionsByValue(ctx, \"1.2.3.4\", \"\", \"\", false)\nif err != nil {\n\tlog.Errorf(\"counting decisions: %v\", err)\n\t// fall back to fail-open or retry later\n}","handlingStrategy":"try-catch","validationCode":"// Go: check DB connectivity before counting\nif err := client.Healthcheck(ctx); err != nil {\n\treturn fmt.Errorf(\"database unavailable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"count, err := client.CountDecisionsByValue(ctx, value, scope, kind, active)\nif err != nil {\n\tlog.Warnf(\"decision count failed (DB issue): %v\", err)\n\t// degrade gracefully: treat as 0 or retry with backoff\n}","preventionTips":["Monitor database health/disk space in production.","Run migrations after every crowdsec upgrade.","Avoid placing the SQLite DB on network or removable drives."],"tags":["database","ent","crowdsec","query-failed"],"backgroundTag":"database-query-failed","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"}