diff --git a/backend/open_webui/internal/db.py b/backend/open_webui/internal/db.py index 7c890e0ca2..acce09ed6d 100644 --- a/backend/open_webui/internal/db.py +++ b/backend/open_webui/internal/db.py @@ -370,6 +370,8 @@ if sys.platform == 'win32' and _is_postgres_url(DATABASE_URL): if 'sqlite' in ASYNC_SQLALCHEMY_DATABASE_URL: # Generous default — async coroutines + no session sharing = high connection demand. + # No pool_pre_ping: a local SQLite file cannot drop connections, and the + # ping costs a worker-thread hop plus a SELECT 1 on every checkout. _sqlite_pool_size = DATABASE_POOL_SIZE if isinstance(DATABASE_POOL_SIZE, int) and DATABASE_POOL_SIZE > 0 else 512 async_engine = create_async_engine( ASYNC_SQLALCHEMY_DATABASE_URL, @@ -377,7 +379,6 @@ if 'sqlite' in ASYNC_SQLALCHEMY_DATABASE_URL: pool_size=_sqlite_pool_size, pool_timeout=DATABASE_POOL_TIMEOUT, pool_recycle=DATABASE_POOL_RECYCLE, - pool_pre_ping=True, ) @event.listens_for(async_engine.sync_engine, 'connect') diff --git a/backend/open_webui/utils/asgi_middleware.py b/backend/open_webui/utils/asgi_middleware.py index 7189bfcad8..2c3412df48 100644 --- a/backend/open_webui/utils/asgi_middleware.py +++ b/backend/open_webui/utils/asgi_middleware.py @@ -100,14 +100,20 @@ class CommitSessionMiddleware: # Downstream did not complete successfully. Roll back any # pending sync writes, release the connection, and let the # exception propagate. - try: - ScopedSession.rollback() - except Exception: - log.exception('CommitSessionMiddleware: rollback failed after downstream error') - finally: - ScopedSession.remove() + if ScopedSession.registry.has(): + try: + ScopedSession.rollback() + except Exception: + log.exception('CommitSessionMiddleware: rollback failed after downstream error') + finally: + ScopedSession.remove() raise + # Nothing in this request touched the sync session: committing would + # only instantiate one to run an empty transaction. + if not ScopedSession.registry.has(): + return + # Downstream completed. Commit pending sync work. try: ScopedSession.commit()