crowdsecurity/crowdsec · error

while listing machines: %w

Error message

while listing machines: %w

What it means

FetchScenariosListFromDB builds the list of scenarios to share by reading all enrolled machines from the local database via ListMachines. This error wraps any database failure during that listing — schema problems, connection loss, or backend errors — so callers like Authenticate and Pull can propagate the root cause.

Source

Thrown at pkg/apiserver/apic.go:94

}

// randomDuration returns a duration value between d-delta and d+delta
func randomDuration(d time.Duration, delta time.Duration) time.Duration {
	ret := d + time.Duration(rand.Int63n(int64(2*delta))) - delta
	// ticker interval must be > 0 (nanoseconds)
	if ret <= 0 {
		return 1
	}

	return ret
}

func (a *apic) FetchScenariosListFromDB(ctx context.Context) ([]string, error) {
	scenarios := make([]string, 0)

	machines, err := a.dbClient.ListMachines(ctx)
	if err != nil {
		return nil, fmt.Errorf("while listing machines: %w", err)
	}

	// merge all scenarios together
	for _, v := range machines {
		if v.Scenarios == "" {
			log.Debugf("No scenarios for machine %d", v.ID)
			continue
		}

		machineScenarios := strings.Split(v.Scenarios, ",")
		log.Debugf("%d scenarios for machine %d", len(machineScenarios), v.ID)

		for _, sv := range machineScenarios {
			if !slices.Contains(scenarios, sv) && sv != "" {
				scenarios = append(scenarios, sv)
			}
		}
	}

View on GitHub (pinned to 909b515798)

Solutions

  1. Check DB connectivity and that crowdsec is configured to reach it (cscli machines list exercises the same path)
  2. Verify the database schema is up to date (cscli db migrate / restart to trigger migrations)
  3. Inspect the wrapped error via errors.As for *sql errors / no-such-table to identify backend cause
  4. Restore or recreate crowdsec.db from backup if corrupted
Defensive patterns

Strategy: try-catch

Validate before calling

if _, err := dbClient.ListMachines(ctx); err != nil {
	return fmt.Errorf("DB unavailable, cannot fetch scenarios: %w", err)
}

Try / catch

machines, err := a.dbClient.ListMachines(ctx)
if err != nil {
	var sqliteErr sqlite3.Error
	if errors.As(err, &sqliteErr) {
		log.Errorf("sqlite failure (code %d): %v", sqliteErr.Code, sqliteErr)
	}
	return nil, fmt.Errorf("while listing machines: %w", err)
}

Prevention

When it happens

Trigger: a.dbClient.ListMachines(ctx) returns an error while a watcher authenticates or pulls — the DB is unreachable, the machines table is missing/corrupt, or the SQLite/Postgres/MySQL backend rejects the query.

Common situations: SQLite data file deleted or corrupted after an aborted upgrade; DB credentials changed in crowdsec.db.yaml; Postgres/MySQL container down; schema migration not applied after upgrading crowdsec; read-only filesystem.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


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