{"record":{"id":"f5f8bb42a7cfa1b4","repo":"crowdsecurity/crowdsec","slug":"while-listing-machines-w","errorCode":null,"errorMessage":"while listing machines: %w","messagePattern":"while listing machines: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/apiserver/apic.go","lineNumber":94,"sourceCode":"}\n\n// randomDuration returns a duration value between d-delta and d+delta\nfunc randomDuration(d time.Duration, delta time.Duration) time.Duration {\n\tret := d + time.Duration(rand.Int63n(int64(2*delta))) - delta\n\t// ticker interval must be > 0 (nanoseconds)\n\tif ret <= 0 {\n\t\treturn 1\n\t}\n\n\treturn ret\n}\n\nfunc (a *apic) FetchScenariosListFromDB(ctx context.Context) ([]string, error) {\n\tscenarios := make([]string, 0)\n\n\tmachines, err := a.dbClient.ListMachines(ctx)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"while listing machines: %w\", err)\n\t}\n\n\t// merge all scenarios together\n\tfor _, v := range machines {\n\t\tif v.Scenarios == \"\" {\n\t\t\tlog.Debugf(\"No scenarios for machine %d\", v.ID)\n\t\t\tcontinue\n\t\t}\n\n\t\tmachineScenarios := strings.Split(v.Scenarios, \",\")\n\t\tlog.Debugf(\"%d scenarios for machine %d\", len(machineScenarios), v.ID)\n\n\t\tfor _, sv := range machineScenarios {\n\t\t\tif !slices.Contains(scenarios, sv) && sv != \"\" {\n\t\t\t\tscenarios = append(scenarios, sv)\n\t\t\t}\n\t\t}\n\t}","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/apiserver/apic.go#L76-L112","documentation":"FetchScenariosListFromDB builds the list of scenarios to share by reading all enrolled machines from the local database via ListMachines. This error wraps any database failure during that listing — schema problems, connection loss, or backend errors — so callers like Authenticate and Pull can propagate the root cause.","triggerScenarios":"a.dbClient.ListMachines(ctx) returns an error while a watcher authenticates or pulls — the DB is unreachable, the machines table is missing/corrupt, or the SQLite/Postgres/MySQL backend rejects the query.","commonSituations":"SQLite data file deleted or corrupted after an aborted upgrade; DB credentials changed in crowdsec.db.yaml; Postgres/MySQL container down; schema migration not applied after upgrading crowdsec; read-only filesystem.","solutions":["Check DB connectivity and that crowdsec is configured to reach it (cscli machines list exercises the same path)","Verify the database schema is up to date (cscli db migrate / restart to trigger migrations)","Inspect the wrapped error via errors.As for *sql errors / no-such-table to identify backend cause","Restore or recreate crowdsec.db from backup if corrupted"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if _, err := dbClient.ListMachines(ctx); err != nil {\n\treturn fmt.Errorf(\"DB unavailable, cannot fetch scenarios: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"machines, err := a.dbClient.ListMachines(ctx)\nif err != nil {\n\tvar sqliteErr sqlite3.Error\n\tif errors.As(err, &sqliteErr) {\n\t\tlog.Errorf(\"sqlite failure (code %d): %v\", sqliteErr.Code, sqliteErr)\n\t}\n\treturn nil, fmt.Errorf(\"while listing machines: %w\", err)\n}","preventionTips":["Run cscli machines list after upgrades to confirm DB schema and connectivity","Keep /var/lib/crowdsec/data on a writable, backed-up volume","Check DB backend health (Postgres/MySQL) before restarting crowdsec"],"tags":["database","crowdsec","db-client"],"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-14T00:17:10.932Z"}