Skip to content

fix(ai): opening the spend ledger from several threads at once fails with database is locked #1820

Closed

Description

@dan-petty

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.

Activity

  1. added this to the v0.2.39 milestone on Oct 22, 2026
  2. added
    type/bug
    scope/ai
    priority/p1-high
    source/agent
    on Oct 22, 2026
  3. added a commit that references this issue on Oct 22, 2026
    f417763
  4. dan-petty commented on Oct 22, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1821, merged into release/v0.2.39 as f417763.

    Opening the spend ledger from several threads at once no longer fails with database is locked.

    4 file(s) changed.

    How it was verified

    Check runs at b1d6eae:

    • Prune PR DevContainer Images: pass
    • Audit Defect PR Hardening: skipping
    • zizmor: pass
    • devops-cli: pass
    • bandit: pass
    • CodeQL: pass
    • Fail-to-Pass Self-Check: pass
    • Rule Pack Tests: pass
    • Changed-Test Independence: pass
    • Tests & Coverage: pass
    • Analyze (python): pass
    • Fail-to-Pass Check: pass
    • Analyze (actions): pass
    • Static Analysis: pass
    • DevContainer Image: skipping
    • Service Image: skipping
    • Dependency Freshness: skipping

    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).
  5. added a commit that references this issue on Oct 23, 2026
    f209f6c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/aisource/agenttype/bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions