Skip to content

fix(gateway): limit concurrent usage queries to prevent SQLite starvation - #101581

Closed
tzy-17 wants to merge 2 commits into
openclaw:mainfrom
tzy-17:fix/101552-usage-concurrency
Closed

tzy-17 wants to merge 2 commits into
openclaw:mainfrom
tzy-17:fix/101552-usage-concurrency

Conversation

@tzy-17

@tzy-17 tzy-17 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes #101552: Promise.all over all agent usage queries created an unbounded array of concurrent database operations. For deployments with 100+ agents, this instantly saturated the SQLite WAL lock and connection pool, causing SQLITE_BUSY timeouts or memory spikes when the usage dashboard loaded.

Why This Change Was Made

Replaced Promise.all with runTasksWithConcurrency({ limit: 8 }) to cap simultaneous usage queries against the shared state database. This prevents SQLite starvation while keeping dashboard load times reasonable.

User Impact

  • Large multi-agent deployments no longer crash or timeout when opening the usage dashboard
  • Dashboard load time slightly increases for very large agent counts (expected and acceptable)

Evidence

  • pnpm test src/gateway/server-methods/usage.test.ts — 60 tests pass
  • oxlint passes
  • runTasksWithConcurrency is already imported and used elsewhere in the codebase

🤖 Generated with Claude Code

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS labels Jul 7, 2026
@tzy-17
tzy-17 force-pushed the fix/101552-usage-concurrency branch from b2de7c1 to aa06606 Compare July 7, 2026 11:06
@clawsweeper

clawsweeper Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Close as superseded: an open, mergeable, proof-positive sibling PR owns the same linked gateway bug with the same bounded-concurrency approach plus regression coverage, while this branch remains a weaker competing landing candidate without real behavior proof.

Root-cause cluster
Relationship: superseded
Canonical: #101589
Summary: This PR, sibling fix PRs, and the linked source issue share the same all-agent usage.cost fan-out root cause; the proof-positive sibling is the safest canonical landing path.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Canonical path: Close this duplicate branch, continue review on #101589, keep #101552 open until that PR merges, and leave #86718 for the broader Gateway usage/cost availability work.

So I’m closing this here and keeping the remaining discussion on #101589, #101552, and #86718.

Review details

Best possible solution:

Close this duplicate branch, continue review on #101589, keep #101552 open until that PR merges, and leave #86718 for the broader Gateway usage/cost availability work.

Do we have a high-confidence way to reproduce the issue?

Yes at source level: current main and v2026.6.11 still start one usage.cost cache-load promise per configured agent through raw Promise.all. I did not run a live 100-agent SQLite starvation replay.

Is this the best way to solve the issue?

No as this branch's landing path. The implementation is plausible, but #101589 is the better canonical solution because it covers the same root cause with regression coverage and sufficient terminal proof.

Security review:

Security review cleared: No security or supply-chain concern found; the diff only changes gateway usage aggregation code and adds no dependencies, workflows, permissions, downloads, package metadata, or secret handling.

AGENTS.md: found and applied where relevant.

What I checked:

  • Repository policy read and applied: Read the full root AGENTS.md plus scoped gateway/server-methods AGENTS.md; gateway hot-path and whole-path review guidance drove the sibling-code, helper-contract, test, and duplicate-path checks. (AGENTS.md:1, e591dcfa8213)
  • Current main still has the underlying all-agent fan-out: loadAllAgentCostUsageSummary on current main still maps every configured agent into loadCostUsageSummaryFromCache and awaits the whole array with raw Promise.all. (src/gateway/server-methods/usage.ts:867, e591dcfa8213)
  • Latest release remains affected: The latest release tag v2026.6.11 contains the same raw all-agent Promise.all(agentIds.map(...)) shape, so the linked bug is not already shipped fixed. (src/gateway/server-methods/usage.ts:811, e085fa1a3ffd)
  • This PR is a plausible but weaker same-root fix: The live diff changes only usage.ts, adding bounded task factories, errorMode: "stop", and firstError propagation, but it adds no regression test and the PR body has no after-fix real behavior proof. (src/gateway/server-methods/usage.ts:869, 9f6bc45c2fe9)
  • Canonical sibling is viable and proof-positive: The sibling PR is open, non-draft, mergeable, maintainer-editable, labeled proof: sufficient, and touches both the implementation and a focused regression test for the same source issue. (0dace0fa35f7)
  • Canonical sibling includes regression coverage: The sibling PR adds a usage.cost all-agent test that creates 25 agents and asserts peak in-flight loads stay at or below the 12-agent concurrency limit while aggregation still succeeds. (src/gateway/server-methods/usage.test.ts:519, 0dace0fa35f7)

Likely related people:

  • Takhoffman: Authored and merged the token usage dashboard PR that introduced the gateway usage analytics surface containing the affected all-agent usage path. (role: feature owner; confidence: high; commits: 8a352c8f9dfd; files: src/gateway/server-methods/usage.ts, src/infra/session-cost-usage.ts)
  • steipete: Authored early usage-cost summary/cache work and the recent current-main file carry-forward that now owns the blamed lines in usage.ts. (role: recent area contributor; confidence: high; commits: 3ce1ee84ace4, 6e792f08f88f; files: src/gateway/server-methods/usage.ts, src/infra/session-cost-usage.ts)
  • NianJiuZst: Authored the merged sessions.usage bounded-cache-load change that provides the closest existing same-module concurrency pattern. (role: adjacent fix contributor; confidence: medium; commits: efe4a59ceb75; files: src/gateway/server-methods/usage.ts, src/gateway/server-methods/usage.sessions-usage.test.ts)

Codex review notes: model internal, reasoning high; reviewed against e591dcfa8213.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 7, 2026
@tzy-17
tzy-17 force-pushed the fix/101552-usage-concurrency branch from aa06606 to 26c028d Compare July 7, 2026 11:18
@clawsweeper clawsweeper Bot removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 7, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. labels Jul 7, 2026
@tzy-17
tzy-17 force-pushed the fix/101552-usage-concurrency branch from 26c028d to da4da0a Compare July 7, 2026 11:48
@tzy-17
tzy-17 force-pushed the fix/101552-usage-concurrency branch from da4da0a to 3f78915 Compare July 7, 2026 11:56
@clawsweeper clawsweeper Bot added the merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. label Jul 7, 2026
tzy-17 and others added 2 commits July 7, 2026 20:46
…tion

Promise.all over all agent usage queries exhausted the SQLite
connection pool for deployments with 100+ agents, causing
SQLITE_BUSY timeouts or memory spikes when loading the usage
dashboard.

Replace Promise.all with runTasksWithConcurrency(limit=8) to cap
simultaneous queries against the shared state database.

Fixes openclaw#101552
Preserve the fail-fast semantics of the original Promise.all by setting
errorMode: "stop" and explicitly throwing firstError, matching the
existing runTasksWithConcurrency usage in the same file at line 1290.

Co-Authored-By: Claude <[email protected]>
@tzy-17
tzy-17 force-pushed the fix/101552-usage-concurrency branch from 2e551b2 to 9f6bc45 Compare July 7, 2026 13:04
@clawsweeper clawsweeper Bot removed the merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. label Jul 7, 2026
@vincentkoc vincentkoc self-assigned this Jul 7, 2026
@vincentkoc

Copy link
Copy Markdown
Member

Thanks @tzy-17. This is now superseded by the canonical repair in #101589, landed as e5c2247.

The underlying all-agent fan-out problem was valid. The landed version uses the existing sibling policy of 12 concurrent usage-cache loads instead of introducing a second arbitrary limit of 8, shares one generic constant across both handlers, preserves the original all-or-nothing error behavior, and includes deterministic regression coverage.

Direct source inspection also showed filesystem-backed usage-cache and transcript work rather than SQLite connection-pool starvation. Closing this weaker duplicate now that the complete fix is on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: XS status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unbounded concurrency in usage DB queries causes SQLite starvation

2 participants