From c9be447a38265c1dad3f84809526ffe320e97b4f Mon Sep 17 00:00:00 2001 From: anti Date: Mon, 13 Apr 2026 19:17:53 -0400 Subject: [PATCH] fix: set busy_timeout and WAL pragmas on every async SQLite connection --- decnet/web/db/sqlite/database.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/decnet/web/db/sqlite/database.py b/decnet/web/db/sqlite/database.py index 22ca549..9cddf9d 100644 --- a/decnet/web/db/sqlite/database.py +++ b/decnet/web/db/sqlite/database.py @@ -1,5 +1,5 @@ from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker, create_async_engine -from sqlalchemy import create_engine, Engine +from sqlalchemy import create_engine, Engine, event from sqlmodel import SQLModel from typing import AsyncGenerator @@ -11,7 +11,21 @@ def get_async_engine(db_path: str) -> AsyncEngine: prefix = "sqlite+aiosqlite:///" if db_path.startswith(":memory:"): prefix = "sqlite+aiosqlite://" - return create_async_engine(f"{prefix}{db_path}", echo=False, connect_args={"uri": True}) + engine = create_async_engine( + f"{prefix}{db_path}", + echo=False, + connect_args={"uri": True, "timeout": 30}, + ) + + @event.listens_for(engine.sync_engine, "connect") + def _set_sqlite_pragmas(dbapi_conn, _conn_record): + cursor = dbapi_conn.cursor() + cursor.execute("PRAGMA journal_mode=WAL") + cursor.execute("PRAGMA synchronous=NORMAL") + cursor.execute("PRAGMA busy_timeout=30000") + cursor.close() + + return engine def get_sync_engine(db_path: str) -> Engine: prefix = "sqlite:///"