crowdsecurity/crowdsec · error · QueryFail
listing machines: %w: %w
Error message
listing machines: %w: %w
What it means
ListMachines wraps the generic QueryFail sentinel plus the underlying error when `c.Ent.Machine.Query().All(ctx)` fails. This is a read of the whole machines table, so the failure is at the database layer, not a per-record issue. The wrapped err carries the ent/SQL detail.
Source
Thrown at pkg/database/machines.go:133
}
func (c *Client) QueryMachineByID(ctx context.Context, machineID string) (*ent.Machine, error) {
machine, err := c.Ent.Machine.
Query().
Where(machine.MachineIdEQ(machineID)).
Only(ctx)
if err != nil {
c.Log.Warningf("QueryMachineByID : %s", err)
return &ent.Machine{}, fmt.Errorf("user '%s': %w", machineID, UserNotExists)
}
return machine, nil
}
func (c *Client) ListMachines(ctx context.Context) ([]*ent.Machine, error) {
machines, err := c.Ent.Machine.Query().All(ctx)
if err != nil {
return nil, fmt.Errorf("listing machines: %w: %w", err, QueryFail)
}
return machines, nil
}
func (c *Client) ValidateMachine(ctx context.Context, machineID string) error {
rets, err := c.Ent.Machine.Update().Where(machine.MachineIdEQ(machineID)).SetIsValidated(true).Save(ctx)
if err != nil {
return fmt.Errorf("validating machine: %w: %w", err, UpdateFail)
}
if rets == 0 {
return errors.New("machine not found")
}
return nil
}
View on GitHub (pinned to 909b515798)
Solutions
- Read the wrapped %w error for the root cause (connection refused / table missing / etc.)
- Confirm DB is up and reachable: `cscli machines list` will fail the same way
- Run pending migrations so the schema matches the binary: `cscli migration`
- Restore file permissions/ownership on the SQLite data dir (/var/lib/crowdsec/data)
- Check db_config settings in /etc/crowdsec/config.yaml (host, port, user, type)
Defensive patterns
Strategy: retry
Validate before calling
// probe DB reachability before listing
if err := db.Ent.Machine.Query().Limit(1).FirstErr(ctx) == nil {} // use as a health probe pattern Try / catch
machines, err := db.ListMachines(ctx)
if err != nil {
// wrapped: cause := err; log with errors.Unwrap / %v, retry with backoff
return fmt.Errorf("listing machines: %v", err)
} Prevention
- Health-check the DB before starting LAPI (depends_on/healthcheck)
- Pin crowdsec version to the schema version; run migrations on upgrade, never downgrade blindly
- Watch SQLite file permissions when switching run users
- Alert on DB connectivity from the LAPI host
When it happens
Trigger: Any call to ListMachines (LAPI /machines list controller, validMachineID checks, metrics collection, prune) while the DB is unreachable, the schema is out of sync, or the SQLite file is locked/corrupt.
Common situations: Postgres/MySQL container down in docker-compose; SQLite DB file permissions changed after running as different user; DB migrated by a newer crowdsec then downgraded; network cut to a remote DB.
Understand the failure class
Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.
Related errors
- unable to expire decisions for list %s : %w
- %s: %w
- bulk creating alert: %w: %w
- committing alert transaction: %w: %w
- unable to add values to allowlist: %w
AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06).
Data as JSON: /api/errors/e8167b8e69e9033e.
Report an issue: GitHub.