{"record":{"id":"9bf24f089fe382f9","repo":"crowdsecurity/crowdsec","slug":"unable-to-update-machine-ip-in-database-w","errorCode":null,"errorMessage":"unable to update machine IP in database: %w","messagePattern":"unable to update machine IP in database: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/database/machines.go","lineNumber":218,"sourceCode":"\nfunc (c *Client) UpdateMachineScenarios(ctx context.Context, scenarios string, id int) error {\n\t_, err := c.Ent.Machine.UpdateOneID(id).\n\t\tSetUpdatedAt(time.Now().UTC()).\n\t\tSetScenarios(scenarios).\n\t\tSave(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unable to update machine in database: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (c *Client) UpdateMachineIP(ctx context.Context, ipAddr string, id int) error {\n\t_, err := c.Ent.Machine.UpdateOneID(id).\n\t\tSetIpAddress(ipAddr).\n\t\tSave(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unable to update machine IP in database: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (c *Client) UpdateMachineVersion(ctx context.Context, ipAddr string, id int) error {\n\t_, err := c.Ent.Machine.UpdateOneID(id).\n\t\tSetVersion(ipAddr).\n\t\tSave(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unable to update machine version in database: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (c *Client) QueryMachinesInactiveSince(ctx context.Context, t time.Time) ([]*ent.Machine, error) {\n\treturn c.Ent.Machine.Query().Where(","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/machines.go#L200-L236","documentation":"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.","triggerScenarios":"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.","commonSituations":"Prune removing unvalidated machines mid-auth; watcher IP changed behind NAT/proxy triggering an update against a stale id; SQLite write contention.","solutions":["Read the wrapped ent error to distinguish not-found from driver failure","Treat ent not-found as benign (machine gone) and return/refresh state","Re-query the machine by machine_id and retry the update with fresh data","Check DB connectivity and lock contention if errors are frequent"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"m, err := db.QueryMachineByID(ctx, machineID)\nif err != nil { return err }\n_ = m.ID // fresh id guaranteed present before UpdateMachineIP","typeGuard":null,"tryCatchPattern":"if err := db.UpdateMachineIP(ctx, ip, id); err != nil {\n    // unwrap and log; if ent not-found, refresh the machine row and retry once\n    return fmt.Errorf(\"ip update for machine %d: %v\", id, err)\n}","preventionTips":["Fetch rows via QueryMachineByID immediately before id-based writes","Serialize prune with authentication traffic or tolerate benign not-found","Use a server DB to reduce SQLite lock failures on busy LAPIs","Log the underlying ent error, not just the wrapper"],"tags":["database","update-failed","crowdsec","authenticator"],"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"}