You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(ai): opening the spend ledger from several threads at once fails with database is locked #1820
priority/p1-highPriority P1: high-impact work for the next planned milestonescope/aiAI review pipeline, personas, prompts, LLM client, or RAGsource/agentFiled by an agenttype/bugType bug: the code does not do what it must
Opening the spend ledger from several threads at once fails about 4 to 9 percent of the time with sqlite3.OperationalError: database is locked, so a first AI call made in parallel raises AIClientError and a test that does so flakes.
Evidence
Remote CI on feat(k8s): the argo workflows controller runs in its own namespace under argo cd and deploy-stack #1819 (an unrelated change) failed tests/test_ai_client_security.py::test_ollama_max_parallel_concurrency_slots with OperationalError: database is locked at src/devops_cli/ai/spend/ledger.py:246, re-raised as AIClientError: OperationalError: database is locked from unified.py. Five threads call client.chat at once and each records its spend.
Line 246 is conn.execute("PRAGMA journal_mode=WAL;") in SpendLedger._get_connection. sqlite3.connect(..., timeout=10.0) does not help: switching a database to WAL needs an exclusive lock and SQLite returns SQLITE_BUSY at once instead of waiting for it.
A stand-alone script that starts five threads on a barrier, each constructing SpendLedger on the same fresh path, failed in 13 of 300 trials and 27 of 300 on a second run, always at that line.
Every connection sets journal_mode=WAL again, though WAL is a property of the database file and needs setting only once. get_spend_ledger also builds its module-level instance without a lock, so concurrent first calls build several.
Proposed remedy
Read the journal mode first and switch it only when it is not wal, with that check and switch under one module-level lock so threads of a process never convert at once. Add a test that opens a fresh ledger from five threads over many trials, which fails at the base. Two processes converting the same new file at the same moment can still race; that is the existing behaviour and this fix does not claim to remove it.
Acceptance Criteria of docs/agent/tasks/task-1820-spend-ledger-wal-lock.md:
Five threads opening the same new ledger together raise no OperationalError (test_a_new_ledger_opens_from_several_threads_at_once; it fails at the base).
priority/p1-highPriority P1: high-impact work for the next planned milestonescope/aiAI review pipeline, personas, prompts, LLM client, or RAGsource/agentFiled by an agenttype/bugType bug: the code does not do what it must
Activity
fix(ai): opening the spend ledger from several threads at once no lon…
dan-petty commentedon Oct 22, 2026
What changed
#1821, merged into
release/v0.2.39as f417763.4 file(s) changed.
How it was verified
Check runs at b1d6eae:
Acceptance Criteria of
docs/agent/tasks/task-1820-spend-ledger-wal-lock.md:OperationalError(test_a_new_ledger_opens_from_several_threads_at_once; it fails at the base).feat(release): v0.2.39 (#1947)