From 8646aebaab3440f4b1bbfb07a8366d00bced6bf1 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 10 Jan 2026 00:16:04 +0400 Subject: [PATCH] refac/fix: DATABASE_ENABLE_SESSION_SHARING env var --- backend/open_webui/env.py | 5 +++++ backend/open_webui/internal/db.py | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/env.py b/backend/open_webui/env.py index d746978c26..ff48a3abfe 100644 --- a/backend/open_webui/env.py +++ b/backend/open_webui/env.py @@ -341,6 +341,11 @@ if DATABASE_USER_ACTIVE_STATUS_UPDATE_INTERVAL is not None: except Exception: DATABASE_USER_ACTIVE_STATUS_UPDATE_INTERVAL = 0.0 +# When enabled, get_db_context reuses existing sessions; set to False to always create new sessions +DATABASE_ENABLE_SESSION_SHARING = ( + os.environ.get("DATABASE_ENABLE_SESSION_SHARING", "False").lower() == "true" +) + # Enable public visibility of active user count (when disabled, only admins can see it) ENABLE_PUBLIC_ACTIVE_USERS_COUNT = ( os.environ.get("ENABLE_PUBLIC_ACTIVE_USERS_COUNT", "True").lower() == "true" diff --git a/backend/open_webui/internal/db.py b/backend/open_webui/internal/db.py index ee83586596..6050e37fa3 100644 --- a/backend/open_webui/internal/db.py +++ b/backend/open_webui/internal/db.py @@ -14,6 +14,7 @@ from open_webui.env import ( DATABASE_POOL_SIZE, DATABASE_POOL_TIMEOUT, DATABASE_ENABLE_SQLITE_WAL, + DATABASE_ENABLE_SESSION_SHARING, ENABLE_DB_MIGRATIONS, ) from peewee_migrate import Router @@ -164,7 +165,7 @@ get_db = contextmanager(get_session) @contextmanager def get_db_context(db: Optional[Session] = None): - if isinstance(db, Session): + if isinstance(db, Session) and DATABASE_ENABLE_SESSION_SHARING: yield db else: with get_db() as session: