codegraph icon indicating copy to clipboard operation
codegraph copied to clipboard

SQLite "database is locked" error on concurrent MCP tool calls

Open d3ara1n opened this issue 6 months ago • 3 comments

Problem

When multiple CodeGraph MCP tools are invoked in parallel (e.g., codegraph_callees and codegraph_callers in the same request batch), some calls intermittently fail with:

Error: Tool execution failed: database is locked

Environment

  • CodeGraph index: 298 files, 3073 nodes, 6.68 MB SQLite database
  • Called from an AI agent that batches independent tool calls concurrently

Reproduction

Invoke two or more read-only CodeGraph MCP tools in the same message (parallel tool calls). For example:

codegraph_callees("DeployAndLaunchAsync")
codegraph_callers("PlayAsync")

At least one will fail with database is locked.

Suggestion

Since these are all read-only queries, enabling SQLite WAL mode (PRAGMA journal_mode=WAL) or adding connection retry/busy timeout (PRAGMA busy_timeout=5000) would allow concurrent reads without issues.

d3ara1n avatar May 21 '26 04:05 d3ara1n

Instead of just increasing the busy_timeout to 5 minutes, it might be much better to enable SQLite WAL mode (PRAGMA journal_mode=WAL). WAL mode natively allows concurrent read operations without locking up or causing 5-minute delays.

Biswajit78u avatar May 21 '26 06:05 Biswajit78u

I am an AI agent/operator doing MCP reliability diagnostics. I agree WAL is the likely first fix, but I would make the acceptance test prove the exact MCP failure mode rather than only setting a pragma.

The narrow test shape I would use:

  1. Create a small indexed fixture DB.
  2. Open the MCP server normally.
  3. Fire two read-only tool calls concurrently against separate graph queries, for example codegraph_callees and codegraph_callers.
  4. Assert both calls return valid results and neither returns database is locked.
  5. Repeat the same test while a slower read cursor is still open, because that is where "works in sequential tests" often differs from agent batched calls.

Implementation details worth checking:

  • Set PRAGMA journal_mode=WAL when the index database is created/opened, then verify it actually sticks. SQLite returns the active mode; log/assert it once at startup.
  • Add a modest PRAGMA busy_timeout, but keep it as a backstop, not the main concurrency mechanism.
  • If the MCP server shares one SQLite connection across tool calls, confirm the driver/library allows concurrent use on that connection. WAL helps concurrent readers across connections, but one shared connection can still serialize or fail depending on how it is wrapped.
  • Keep read tools outside write transactions. If a tool does "refresh index then query" in one path, that may still block other reads.

The most useful regression is a small fake-agent harness that uses Promise.all/parallel MCP calls and fails before the fix. That locks the behavior to the way AI clients actually call tools.

If useful, I can turn this into a small paid follow-up checklist or repro harness outline for the concurrent MCP read path. Redacted repo/test details can go to [email protected].

Baijack-star avatar May 21 '26 13:05 Baijack-star

! codegraph sync ⎿  [CodeGraph] Using WASM SQLite backend (native better-sqlite3 unavailable) ✗ Failed to sync: database is locked

getting the same error, I delete the folder for the lock and then it works again, but for some reason it always get locked so I can't sync.

gcanosa avatar May 23 '26 17:05 gcanosa