crowdsecurity/crowdsec · error

unable to update machine IP in database: %w

Error message

unable to update machine IP in database: %w

What it means

UpdateMachineIP returns the raw ent error when Machine.UpdateOneID(id).SetIpAddress(ipAddr).Save(ctx) fails. Like the scenarios updater, it runs during Authenticator; ent returns a not-found error if the row id no longer exists, or a driver error if the DB is unavailable.

Source

Thrown at pkg/database/machines.go:218

func (c *Client) UpdateMachineScenarios(ctx context.Context, scenarios string, id int) error {
	_, err := c.Ent.Machine.UpdateOneID(id).
		SetUpdatedAt(time.Now().UTC()).
		SetScenarios(scenarios).
		Save(ctx)
	if err != nil {
		return fmt.Errorf("unable to update machine in database: %w", err)
	}

	return nil
}

func (c *Client) UpdateMachineIP(ctx context.Context, ipAddr string, id int) error {
	_, err := c.Ent.Machine.UpdateOneID(id).
		SetIpAddress(ipAddr).
		Save(ctx)
	if err != nil {
		return fmt.Errorf("unable to update machine IP in database: %w", err)
	}

	return nil
}

func (c *Client) UpdateMachineVersion(ctx context.Context, ipAddr string, id int) error {
	_, err := c.Ent.Machine.UpdateOneID(id).
		SetVersion(ipAddr).
		Save(ctx)
	if err != nil {
		return fmt.Errorf("unable to update machine version in database: %w", err)
	}

	return nil
}

func (c *Client) QueryMachinesInactiveSince(ctx context.Context, t time.Time) ([]*ent.Machine, error) {
	return c.Ent.Machine.Query().Where(

View on GitHub (pinned to 909b515798)

Solutions

  1. Read the wrapped ent error to distinguish not-found from driver failure
  2. Treat ent not-found as benign (machine gone) and return/refresh state
  3. Re-query the machine by machine_id and retry the update with fresh data
  4. Check DB connectivity and lock contention if errors are frequent
Defensive patterns

Strategy: try-catch

Validate before calling

m, err := db.QueryMachineByID(ctx, machineID)
if err != nil { return err }
_ = m.ID // fresh id guaranteed present before UpdateMachineIP

Try / catch

if err := db.UpdateMachineIP(ctx, ip, id); err != nil {
    // unwrap and log; if ent not-found, refresh the machine row and retry once
    return fmt.Errorf("ip update for machine %d: %v", id, err)
}

Prevention

When it happens

Trigger: Authenticator updates the watcher's IP on each successful auth; fails when the machine row was deleted concurrently, the DB is locked/down, or the new ipAddr violates a column constraint.

Common situations: Prune removing unvalidated machines mid-auth; watcher IP changed behind NAT/proxy triggering an update against a stale id; SQLite write contention.

Related errors


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