{"record":{"id":"6d3db3a4201f9c17","repo":"crowdsecurity/crowdsec","slug":"machine-q-w","errorCode":null,"errorMessage":"machine %q: %w","messagePattern":"machine %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/database/alerts.go","lineNumber":749,"sourceCode":"\t\towner, err = c.QueryMachineByID(ctx, machineID)\n\t\tif err != nil {\n\t\t\tif !errors.Is(err, UserNotExists) {\n\t\t\t\treturn nil, fmt.Errorf(\"machine '%s': %w\", machineID, err)\n\t\t\t}\n\n\t\t\tc.Log.Debugf(\"creating alert: machine %s doesn't exist\", machineID)\n\n\t\t\towner = nil\n\t\t}\n\t}\n\n\tc.Log.Debugf(\"writing %d items\", len(alertList))\n\n\talertIDs := []string{}\n\tif err := slicetools.Batch(ctx, alertList, alertCreateBulkSize, func(ctx context.Context, part []*models.Alert) error {\n\t\tids, err := c.createAlertBatch(ctx, machineID, owner, part)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"machine %q: %w\", machineID, err)\n\t\t}\n\t\talertIDs = append(alertIDs, ids...)\n\t\treturn nil\n\t}); err != nil {\n\t\treturn nil, err\n\t}\n\n\tif owner != nil {\n\t\terr = owner.Update().SetLastPush(time.Now().UTC()).Exec(ctx)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"machine '%s': %w\", machineID, err)\n\t\t}\n\t}\n\n\treturn alertIDs, nil\n}\n\nfunc (c *Client) AlertsCountPerScenario(ctx context.Context, filter map[string][]string) (map[string]int, error) {","sourceCodeStart":731,"sourceCodeEnd":767,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/alerts.go#L731-L767","documentation":"During bulk alert creation, CreateAlert wraps any error from createAlertBatch (via slicetools.Batch) with the machine ID. It indicates one or more alert rows in the batch failed to insert in the database.","triggerScenarios":"createAlertBatch returns an insert error — DB unavailable, constraint violation on an alert row, context cancelled mid-batch, or a batch size limit hit.","commonSituations":"Bouncer/parser pushing many alerts at once during DB contention; SQLite 'database is locked'; oversized batch failing on MySQL max_allowed_packet.","solutions":["Inspect the wrapped error for the real cause (lock timeout, packet size, constraint)","Reduce alert burst volume or batch size (alertCreateBulkSize) if hitting driver limits","Check DB health: for SQLite ensure no other process holds the lock; for MySQL tune max_allowed_packet","Retry the push — batches are not partially committed from the caller's perspective"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"if len(alerts) == 0 {\n    return nil // nothing to push, skip batch path\n}","typeGuard":null,"tryCatchPattern":"err := retry.Do(func() error {\n    _, err := client.CreateAlert(ctx, alert)\n    return err\n}, retry.Attempts(3), retry.Delay(time.Second))","preventionTips":["Throttle alert production to avoid DB write storms","For MySQL, raise max_allowed_packet if batches are large","Monitor DB health before bulk pushes"],"tags":["database","bulk-insert","alerts"],"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"}