Skip to content

fix(gateway): bound usage.cost all-agent fan-out concurrency to prevent SQLite starvation - #101578

Closed
chenxiaoyu209 wants to merge 1 commit into
openclaw:mainfrom
chenxiaoyu209:fix/issue-101552-usage-concurrency
Closed

chenxiaoyu209 wants to merge 1 commit into
openclaw:mainfrom
chenxiaoyu209:fix/issue-101552-usage-concurrency

Conversation

@chenxiaoyu209

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes #101552

loadAllAgentCostUsageSummary fans out loadCostUsageSummaryFromCache calls for every configured agent through an unbounded Promise.all. With 100+ agents this saturates the SQLite connection pool, causing SQLITE_BUSY timeouts or memory spikes that crash the gateway process.

Why This Change Was Made

The sibling sessions.usage path already uses runTasksWithConcurrency with SESSIONS_USAGE_AGENT_LOAD_CONCURRENCY = 12 to bound per-agent cache-load fan-out. This change applies the same pattern to usage.cost, reusing the same helper and concurrency limit that are already imported and proven in the same module.

User Impact

  • Large multi-agent deployments no longer risk gateway crashes when loading the usage dashboard
  • Aggregation semantics and results are unchanged — only peak in-flight load is capped
  • Error behavior: switched from Promise.all silent rejection to errorMode: "stop" fail-fast, consistent with sessions.usage

Evidence

  • pnpm test src/gateway/server-methods/usage.test.ts — 62 tests pass (2 files), including new concurrency-cap regression test
  • pnpm test src/gateway/server-methods/usage.sessions-usage.test.ts — 42 tests pass, no regression
  • New test creates 30 agents and verifies peak concurrency stays ≤ 12

🤖 Generated with Claude Code

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: M labels Jul 7, 2026
@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: this PR now contains the right narrow gateway fix, but an open proof-positive sibling PR owns the same linked issue with the same bounded-concurrency approach and stronger maintainer-ready evidence.

Root-cause cluster
Relationship: superseded
Canonical: #101589
Summary: This PR and two sibling PRs target the same open all-agent usage.cost fan-out bug; the proof-positive sibling is the safer 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 and continue review on https://github.com/openclaw/openclaw/pull/101589, keeping https://github.com/openclaw/openclaw/issues/101552 open until the canonical PR merges.

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

Review details

Best possible solution:

Close this duplicate branch and continue review on https://github.com/openclaw/openclaw/pull/101589, keeping https://github.com/openclaw/openclaw/issues/101552 open until the canonical PR merges.

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

Yes for source-level reproduction: current main and v2026.6.11 start one usage.cost cache load 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 the best landing path. The patch shape is correct, but the same linked issue is already owned by a proof-positive open sibling PR with equivalent bounded-concurrency behavior.

Security review:

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

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • Takhoffman: Authored the merged token usage dashboard work and later multi-agent sessions.usage discovery fixes in the same gateway usage surface. (role: feature owner; confidence: high; commits: 8a352c8f9dfd, 9271fcb3d49f; files: src/gateway/server-methods/usage.ts, src/infra/session-cost-usage.ts)
  • steipete: Authored early usage-cost summary/cache work and recent current-main file-split history that carries the affected all-agent function. (role: adjacent usage-cost contributor; confidence: high; commits: 3ce1ee84ace4, 6b76a306d472; files: src/gateway/server-methods/usage.ts, src/infra/session-cost-usage.ts)
  • NianJiuZst: Authored the recent merged sessions.usage usage-cache batching change that provides the closest current-main bounded-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 2ba622ca3019.

@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. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jul 7, 2026
@chenxiaoyu209
chenxiaoyu209 force-pushed the fix/issue-101552-usage-concurrency branch from 9e57e70 to 0df4412 Compare July 7, 2026 11:28
@chenxiaoyu209

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

…nt SQLite starvation

Replace unbounded Promise.all in loadAllAgentCostUsageSummary with
runTasksWithConcurrency capped at SESSIONS_USAGE_AGENT_LOAD_CONCURRENCY (12),
matching the existing sessions.usage pattern in the same module.

Fixes #101552

Co-Authored-By: Claude <[email protected]>
@chenxiaoyu209
chenxiaoyu209 force-pushed the fix/issue-101552-usage-concurrency branch from a56fe4e to b8c4f7f Compare July 7, 2026 11:55
@chenxiaoyu209

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jul 7, 2026
@vincentkoc vincentkoc self-assigned this Jul 7, 2026
@vincentkoc vincentkoc closed this Jul 7, 2026
@vincentkoc

Copy link
Copy Markdown
Member

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

Your version identified the same valid all-agent fan-out problem and reused the existing limit. The landed version keeps that useful direction, shares one generic usage-cache concurrency constant across both sibling handlers, preserves the original all-or-nothing error behavior, and adds a deterministic 13-agent regression without timer-based scheduling assumptions.

Direct source inspection also showed filesystem-backed usage-cache and transcript work rather than SQLite connection-pool starvation, so the canonical PR corrected that mechanism in its title and evidence. Closing this 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: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S 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