{"record":{"id":"e5990d6674a926ff","repo":"crowdsecurity/crowdsec","slug":"unable-to-update-base-machine-metrics-in-database","errorCode":null,"errorMessage":"unable to update base machine metrics in database: %w","messagePattern":"unable to update base machine metrics in database: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"pkg/database/machines.go","lineNumber":61,"sourceCode":"\t\t\t\tStatus:  item.Status,\n\t\t\t\tVersion: item.Version,\n\t\t\t})\n\t\t}\n\t}\n\n\t_, err := c.Ent.Machine.\n\t\tUpdate().\n\t\tWhere(machine.MachineIdEQ(machineID)).\n\t\tSetNillableVersion(baseMetrics.Version).\n\t\tSetOsname(*os.Name).\n\t\tSetOsfamily(os.Family).\n\t\tSetOsversion(*os.Version).\n\t\tSetFeatureflags(features).\n\t\tSetHubstate(hubState).\n\t\tSetDatasources(datasources).\n\t\tSave(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unable to update base machine metrics in database: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (c *Client) CreateMachine(ctx context.Context, machineID *string, password *strfmt.Password, ipAddress string, isValidated bool, force bool, authType string) (*ent.Machine, error) {\n\thashPassword, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)\n\tif err != nil {\n\t\tc.Log.Warningf(\"CreateMachine: %s\", err)\n\t\treturn nil, HashError\n\t}\n\n\tmachineExist, err := c.Ent.Machine.\n\t\tQuery().\n\t\tWhere(machine.MachineIdEQ(*machineID)).\n\t\tSelect(machine.FieldMachineId).Strings(ctx)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"machine '%s': %w: %w\", *machineID, err, QueryFail)","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/machines.go#L43-L79","documentation":"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.","triggerScenarios":"`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).","commonSituations":"Machine unregistered while LAPI still updating its metrics; external DB temporarily unreachable; schema mismatch after partial upgrade.","solutions":["Verify the machine still exists: `cscli machines list`","Check DB connectivity and disk space","Re-register the machine if its row vanished: `cscli machines add <name>`","Run schema/migration check after upgrading crowdsec"],"exampleFix":"// before\n_, err := c.Ent.Machine.Update().Where(...).SetOsversion(...).Save(ctx)\n// after: tolerate missing machine\nif ent.IsNotFound(err) { log.Debug(\"machine gone, skipping metrics update\"); return nil }","handlingStrategy":"try-catch","validationCode":"exists, _ := c.Ent.Machine.Query().Where(machine.MachineIdEQ(id)).Exist(ctx) // guard before update","typeGuard":null,"tryCatchPattern":"if err := c.MachineUpdateBaseMetrics(ctx, m); err != nil {\n    if ent.IsNotFound(err) { return nil } // machine unregistered concurrently\n    return err\n}","preventionTips":["Don't delete machines while LAPI is updating metrics","Keep metrics payloads within column limits","Verify schema after upgrades"],"tags":["database","metrics","ent"],"backgroundTag":"database-write-failed","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}