crowdsecurity/crowdsec · warning

unable to update base machine metrics in database: %w

Error message

unable to update base machine metrics in database: %w

What it means

The ent UPDATE persisting a machine's base metrics (version, OS name/family/version, feature flags, hub state, datasources) failed in MachineUpdateBaseMetrics. The machine is known; only the metrics refresh write failed at the DB level.

Source

Thrown at pkg/database/machines.go:61

				Status:  item.Status,
				Version: item.Version,
			})
		}
	}

	_, err := c.Ent.Machine.
		Update().
		Where(machine.MachineIdEQ(machineID)).
		SetNillableVersion(baseMetrics.Version).
		SetOsname(*os.Name).
		SetOsfamily(os.Family).
		SetOsversion(*os.Version).
		SetFeatureflags(features).
		SetHubstate(hubState).
		SetDatasources(datasources).
		Save(ctx)
	if err != nil {
		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)

View on GitHub (pinned to 909b515798)

Solutions

  1. Verify the machine still exists: `cscli machines list`
  2. Check DB connectivity and disk space
  3. Re-register the machine if its row vanished: `cscli machines add <name>`
  4. Run schema/migration check after upgrading crowdsec

Example fix

// before
_, err := c.Ent.Machine.Update().Where(...).SetOsversion(...).Save(ctx)
// after: tolerate missing machine
if ent.IsNotFound(err) { log.Debug("machine gone, skipping metrics update"); return nil }
Defensive patterns

Strategy: try-catch

Validate before calling

exists, _ := c.Ent.Machine.Query().Where(machine.MachineIdEQ(id)).Exist(ctx) // guard before update

Try / catch

if err := c.MachineUpdateBaseMetrics(ctx, m); err != nil {
    if ent.IsNotFound(err) { return nil } // machine unregistered concurrently
    return err
}

Prevention

When it happens

Trigger: `c.Ent.Machine.Update()....Save(ctx)` returns an error — the machine row was deleted concurrently, DB connection lost, or a column/size constraint failed (e.g. oversized features string).

Common situations: Machine unregistered while LAPI still updating its metrics; external DB temporarily unreachable; schema mismatch after partial upgrade.

Related errors


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