Skip to content

Enable WAL mode and fix TOCTOU race in migrations to prevent database locking errors - #1572

Open
ikatyal2110 wants to merge 1 commit into
simonw:mainfrom
ikatyal2110:fix/migrations-wal-mode-locking
Open

Enable WAL mode and fix TOCTOU race in migrations to prevent database locking errors#1572
ikatyal2110 wants to merge 1 commit into
simonw:mainfrom
ikatyal2110:fix/migrations-wal-mode-locking

Conversation

@ikatyal2110

Copy link
Copy Markdown
Contributor

When multiple llm processes start at the same time (e.g. via xargs -P), they each call migrate(), which left the database in the default rollback-journal mode where any exclusive write lock blocks all readers, and used a check-then-create pattern for _llm_migrations that creates a TOCTOU race. The fix enables WAL mode for file-backed databases so concurrent reads are never blocked by a writer, and uses CREATE TABLE IF NOT EXISTS (via if_not_exists=True) to atomically create the migrations table, eliminating the race. Regression tests confirm WAL mode is set after migration and that calling ensure_migrations_table twice is safe. Fixes #789


Generated by Claude Code

… errors

When multiple llm processes start simultaneously they all call migrate() which
previously used a check-then-create pattern for the migrations table (a TOCTOU
race) and left the database in the default rollback-journal mode where any
exclusive write lock blocks all readers. Switching to WAL mode eliminates the
reader-blocks-writer problem, and using if_not_exists=True removes the race.

Fixes simonw#789
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlite locking errors when invoking llm under xargs

2 participants