crowdsecurity/crowdsec · error · QueryFail

machine '%s': %w: %w

Error message

machine '%s': %w: %w

What it means

A machine-related DB operation failed and the machine ID is double-wrapped with a sentinel error class (e.g. HashError from bcrypt in CreateMachine, or a similar machine sentinel). The %w:%w shape lets callers errors.Is-match the sentinel while still seeing the driver error; the machine name identifies the registration involved.

Source

Thrown at pkg/database/machines.go:79

		return fmt.Errorf("unable to update base machine metrics in database: %w", err)
	}

	return nil
}

func (c *Client) CreateMachine(ctx context.Context, machineID *string, password *strfmt.Password, ipAddress string, isValidated bool, force bool, authType string) (*ent.Machine, error) {
	hashPassword, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)
	if err != nil {
		c.Log.Warningf("CreateMachine: %s", err)
		return nil, HashError
	}

	machineExist, err := c.Ent.Machine.
		Query().
		Where(machine.MachineIdEQ(*machineID)).
		Select(machine.FieldMachineId).Strings(ctx)
	if err != nil {
		return nil, fmt.Errorf("machine '%s': %w: %w", *machineID, err, QueryFail)
	}

	if len(machineExist) > 0 {
		if force {
			_, err := c.Ent.Machine.Update().Where(machine.MachineIdEQ(*machineID)).SetPassword(string(hashPassword)).Save(ctx)
			if err != nil {
				c.Log.Warningf("CreateMachine : %s", err)
				return nil, fmt.Errorf("machine '%s': %w", *machineID, UpdateFail)
			}

			machine, err := c.QueryMachineByID(ctx, *machineID)
			if err != nil {
				return nil, fmt.Errorf("machine '%s': %w: %w", *machineID, err, QueryFail)
			}

			return machine, nil
		}

View on GitHub (pinned to 909b515798)

Solutions

  1. Ensure DB schema exists — start crowdsec once or run the migrate step before `cscli machines add`
  2. Check DB connectivity and credentials in /etc/crowdsec/config.yaml
  3. If SQLite, run `PRAGMA integrity_check` on the db file
  4. Retry the machine creation once the DB is healthy

Example fix

// caller-side retry
for i := 0; i < 3; i++ {
    m, err := client.CreateMachine(ctx, &id, pass, ip, true, false, authType)
    if err == nil || !errors.Is(err, database.QueryFail) { return m, err }
    time.Sleep(time.Second)
}
Defensive patterns

Strategy: try-catch

Validate before calling

// before creating, confirm DB is initialized
if _, err := os.Stat(dbPath); os.IsNotExist(err) { runSchemaInit() }

Try / catch

m, err := client.CreateMachine(ctx, &id, pass, ip, true, false, auth)
if err != nil && errors.Is(err, database.QueryFail) {
    return fmt.Errorf("db not ready for machine registration: %w", err)
}

Prevention

When it happens

Trigger: The `Machine.Query().Where(machine.MachineIdEQ(...)).Strings(ctx)` lookup errors — DB down, ctx canceled, or the machines table missing/corrupt. Reached via `cscli machines add`, TLS auth, or LAPI registration.

Common situations: Database not initialized (`cscli machines add` on a fresh install before `crowdsec` first run created the schema); MySQL/Postgres outage; corrupted SQLite file.

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/8489dcc538c78dbf. Report an issue: GitHub.