Skip to content

Commit

Permalink
Use AsyncAdaptedQueuePool for sqlite
Browse files Browse the repository at this point in the history
Due to aiosqlite's default, NullPool was used for sqlite.
This is a suboptimal setting when spawning many sessions as we do.
(See bluesky/tiled#663)
Also increase busy_timeout to 30s.
It allows getting rid of "database is locked" when stopping many runs (e.g. 20 at a time).
  • Loading branch information
r4victor committed Jan 16, 2025
1 parent 63629be commit 5c7cc75
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/dstack/_internal/server/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional

from alembic import command, config
from sqlalchemy import event
from sqlalchemy import AsyncAdaptedQueuePool, event
from sqlalchemy.engine.interfaces import DBAPIConnection
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
Expand All @@ -19,7 +19,11 @@ def __init__(self, url: str, engine: Optional[AsyncEngine] = None):
if engine is not None:
self.engine = engine
else:
self.engine = create_async_engine(self.url, echo=settings.SQL_ECHO_ENABLED)
self.engine = create_async_engine(
self.url,
echo=settings.SQL_ECHO_ENABLED,
poolclass=AsyncAdaptedQueuePool,
)
self.session_maker = sessionmaker(
bind=self.engine,
expire_on_commit=False,
Expand All @@ -34,7 +38,7 @@ def set_sqlite_pragma(dbapi_connection: DBAPIConnection, _: ConnectionPoolEntry)
cursor.execute("PRAGMA journal_mode=WAL;")
cursor.execute("PRAGMA foreign_keys=ON;")
cursor.execute("PRAGMA synchronous=NORMAL;")
cursor.execute("PRAGMA busy_timeout=10000;")
cursor.execute("PRAGMA busy_timeout=30000;")
cursor.close()

@property
Expand Down

0 comments on commit 5c7cc75

Please sign in to comment.