docir — design documents Graph

Documents / Issues / issue-88d8fcb20296

The embedding drain held the write lock for minutes, failing every other request

A fresh index embedded the whole corpus in one transaction, so every request needing the lock failed with database is locked until the drain committed.

issue-88d8fcb20296issueresolved#daemon#persistence
View as Markdown◉ View in graph

What is wrong

drain_dirty recomputed the whole dirty queue in one transaction. From the first vector it wrote to the commit it held SQLite's write lock, and it kept holding it through every model inference after that. On a freshly built index of this repository's 270 documents that was about 2m38s with the real model. Any request that needed the lock in that window waited out the five-second busy timeout and failed with database is locked.

Until the deleted-index fix (issue-d75df898a99a) the client hid this: it read the crash as a request that never landed, killed the daemon and resent. The resend answered, and the kill threw the whole drain away, so the next daemon started it again from the first document. Once a request the daemon took is never resent, the same moment surfaces as exit 7.

How it showed

On a copy of this store with its index removed, through the daemon with the real model: the first query rebuilt the index and started the drain, and a docir context a little later failed after 5.4s with "the daemon failed while running 'context' and closed the connection without answering". It hits the first minutes after any full re-embed: a fresh clone or worktree, a deleted index, a model or chunking change.

The fix

Each document is embedded with no transaction open, then written in a transaction of its own under the lock, and only if its inputs still hash to what the model read. A document edited while the model ran keeps its flag for the next drain instead of receiving vectors for text it no longer has, and a drain that stops keeps every document it finished. Pinned by TestTheDrainHoldsNoLockWhileTheModelRuns in tests/platform/test_infra_embedding.py.

To amend: Re-verify: