Skip to content

Fix #606: stagger persona boot to prevent DB thundering herd - #607

Merged
joelteply merged 1 commit into
mainfrom
fix/persona-stagger-boot
Mar 30, 2026
Merged

joelteply merged 1 commit into
mainfrom
fix/persona-stagger-boot

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

15 personas booting simultaneously = 45+ DB queries at once = everything times out = nobody responds. 2s stagger between each persona init.

…ng herd

15 personas all initializing simultaneously overwhelmed SQLite with 45+
concurrent queries. Everything timed out, personas couldn't respond.
Now boots with 2s delay between each persona.
Copilot AI review requested due to automatic review settings March 30, 2026 05:07
@joelteply
joelteply merged commit 93aca22 into main Mar 30, 2026
@joelteply
joelteply deleted the fix/persona-stagger-boot branch March 30, 2026 05:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Reduces database load spikes from persona client initialization by adding a stagger delay between persona client creations during the user-daemon reconciliation loop.

Changes:

  • Converted persona iteration to index-based looping to enable per-persona staggering.
  • Added a 2s delay after creating each missing persona client (except the last).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 498 to +503
if (!this.personaClients.has(persona.id)) {
await this.createPersonaClient(persona);
// Stagger persona boot — 2s between each to avoid thundering herd on DB
if (i < personas.length - 1) {
await new Promise(resolve => setTimeout(resolve, 2000));
}

Copilot AI Mar 30, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 2s sleep can make runStateReconciliationLoop take longer than the 30s interval. DaemonBase.registerInterval uses setInterval and does not await completion, so a long reconciliation run will overlap with the next tick, potentially creating multiple concurrent reconciliation loops and reintroducing the DB thundering-herd problem. Add a guard/mutex (e.g., skip if a reconciliation is already in progress) or change scheduling so the next run is queued only after the prior run finishes.

Copilot uses AI. Check for mistakes.
Comment on lines +500 to +503
// Stagger persona boot — 2s between each to avoid thundering herd on DB
if (i < personas.length - 1) {
await new Promise(resolve => setTimeout(resolve, 2000));
}

Copilot AI Mar 30, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description/title mentions staggering persona boot, but this change only affects the periodic state reconciliation path; the main boot path (ensurePersonaClients) still initializes personas concurrently in batches. If the reported thundering herd happens during initial boot, consider moving/adding the staggering (or making batch size/spacing configurable) to the initialization path, or update the PR description to match the actual behavior change.

Copilot uses AI. Check for mistakes.
Comment on lines +501 to +503
if (i < personas.length - 1) {
await new Promise(resolve => setTimeout(resolve, 2000));
}

Copilot AI Mar 30, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Promise-based setTimeout creates a real timer per persona. During shutdown, DaemonBase.cleanupIntervals() clears the interval but does not cancel in-flight reconciliation or pending timeouts, so the process may stay alive until these timers elapse. Consider using a cancellable sleep (tied to daemon lifecycle/AbortSignal) and/or checking lifecycleState between iterations so shutdown can short-circuit the remaining stagger delays.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants