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

  1. Read the wrapped %w error for the root cause (connection refused / table missing / etc.)
  2. Confirm DB is up and reachable: `cscli machines list` will fail the same way
  3. Run pending migrations so the schema matches the binary: `cscli migration`
  4. Restore file permissions/ownership on the SQLite data dir (/var/lib/crowdsec/data)
  5. 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

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


AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06). Data as JSON: /api/errors/e8167b8e69e9033e. Report an issue: GitHub.