crowdsecurity/crowdsec · error · InsertFail

creating machine '%s': %w

Error message

creating machine '%s': %w

What it means

CreateMachine wraps the sentinel InsertFail (errors.New("failed insert query")) when the ent client's machine .Save() returns an error. The row could not be written to the machines table. The original ent/SQL error is logged as a warning but replaced here, so check the LAPI logs for the underlying cause.

Source

Thrown at pkg/database/machines.go:111

			}

			return machine, nil
		}

		return nil, fmt.Errorf("user '%s': %w", *machineID, UserExists)
	}

	machine, err := c.Ent.Machine.
		Create().
		SetMachineId(*machineID).
		SetPassword(string(hashPassword)).
		SetIpAddress(ipAddress).
		SetIsValidated(isValidated).
		SetAuthType(authType).
		Save(ctx)
	if err != nil {
		c.Log.Warningf("CreateMachine : %s", err)
		return nil, fmt.Errorf("creating machine '%s': %w", *machineID, InsertFail)
	}

	return machine, nil
}

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
}

View on GitHub (pinned to 909b515798)

Solutions

  1. Inspect the LAPI log line 'CreateMachine : <err>' for the underlying ent/SQL error
  2. Verify database connectivity and credentials in /etc/crowdsec/config.yaml (db_config)
  3. Run `cscli migration` / upgrade steps so the schema matches the binary version
  4. If SQLite, check the file exists, is writable, and no stale lock: `ls -l /var/lib/crowdsec/data/crowdsec.db`
  5. Retry the registration; if a unique constraint raced, the second attempt is a duplicate (see UserExists)
Defensive patterns

Strategy: retry

Validate before calling

// ping the DB before registering
if err := db.Client.Database.SQLClient.Ping(); err != nil {
    return fmt.Errorf("DB unavailable: %w", err)
}

Try / catch

if _, err := db.CreateMachine(ctx, id, pw, ip, valid, auth); err != nil {
    if errors.Is(err, database.InsertFail) {
        // check LAPI log 'CreateMachine : <err>' for the ent root cause, retry after fixing
    }
    return err
}

Prevention

When it happens

Trigger: Client.CreateMachine calls c.Ent.Machine.Create()...Save(ctx) and the insert fails: DB connection down, schema mismatch (run `cscli migration` / version drift), unique constraint from a concurrent insert of the same machine_id, or disk full on the SQLite file.

Common situations: SQLite database file locked by another process or deleted/corrupted; LAPI started before the DB schema was migrated; MySQL/Postgres credentials changed after install; two LAPI instances writing concurrently.

Related errors


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