[PR #20545] [MERGED] fix: release database connections immediately after auth instead of holding during LLM calls #25664

Closed
opened 2026-04-20 06:03:48 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/20545
Author: @Classic298
Created: 1/10/2026
Status: Merged
Merged: 1/10/2026
Merged by: @tjbck

Base: devHead: db-perf


📝 Commits (1)

  • a18252f fix: release database connections immediately after auth instead of holding during LLM calls

📊 Changes

1 file changed (+10 additions, -8 deletions)

View changed files

📝 backend/open_webui/utils/auth.py (+10 -8)

📄 Description

fix: release database connections immediately after auth instead of holding during LLM calls

Authentication was using Depends(get_session) which holds a database connection for the entire request lifecycle. For chat completions, this meant connections were held for 30-60 seconds while waiting for LLM responses, despite only needing the connection for ~50ms of actual database work.

With a default pool of 15 connections, this limited concurrent chat users to ~15 before pool exhaustion and timeout errors:

sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached,
connection timed out, timeout 30.00

The fix removes Depends(get_session) from get_current_user. Each database operation now manages its own short-lived session internally:

    BEFORE: One session held for entire request
    ──────────────────────────────────────────────────
    │ auth │ queries │ LLM wait (30s) │ save         │
    │         CONNECTION HELD ENTIRE TIME            │
    ──────────────────────────────────────────────────

    AFTER: Short-lived sessions, released immediately  
    ┌──────┐ ┌───────┐                 ┌──────┐
    │ auth │ │ query │   LLM (30s)     │ save │
    │ 10ms │ │ 20ms  │  NO CONNECTION  │ 20ms │
    └──────┘ └───────┘                 └──────┘

This is safe because:

  • User model has no lazy-loaded relationships (all simple columns)
  • Pydantic conversion (UserModel.model_validate) happens while session is open
  • Returned object is pure Pydantic with no SQLAlchemy ties

Combined with the telemetry efficiency fix, this resolves connection pool exhaustion for high-concurrency deployments, particularly on network-attached databases like AWS Aurora where connection hold time is more impactful.

Contributor License Agreement

By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.

Note

Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/open-webui/pull/20545 **Author:** [@Classic298](https://github.com/Classic298) **Created:** 1/10/2026 **Status:** ✅ Merged **Merged:** 1/10/2026 **Merged by:** [@tjbck](https://github.com/tjbck) **Base:** `dev` ← **Head:** `db-perf` --- ### 📝 Commits (1) - [`a18252f`](https://github.com/open-webui/open-webui/commit/a18252ffb3c558858740093dbcded8f0c1a16d1a) fix: release database connections immediately after auth instead of holding during LLM calls ### 📊 Changes **1 file changed** (+10 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/utils/auth.py` (+10 -8) </details> ### 📄 Description fix: release database connections immediately after auth instead of holding during LLM calls Authentication was using Depends(get_session) which holds a database connection for the entire request lifecycle. For chat completions, this meant connections were held for 30-60 seconds while waiting for LLM responses, despite only needing the connection for ~50ms of actual database work. With a default pool of 15 connections, this limited concurrent chat users to ~15 before pool exhaustion and timeout errors: sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30.00 The fix removes Depends(get_session) from get_current_user. Each database operation now manages its own short-lived session internally: ``` BEFORE: One session held for entire request ────────────────────────────────────────────────── │ auth │ queries │ LLM wait (30s) │ save │ │ CONNECTION HELD ENTIRE TIME │ ────────────────────────────────────────────────── AFTER: Short-lived sessions, released immediately ┌──────┐ ┌───────┐ ┌──────┐ │ auth │ │ query │ LLM (30s) │ save │ │ 10ms │ │ 20ms │ NO CONNECTION │ 20ms │ └──────┘ └───────┘ └──────┘ ``` This is safe because: - User model has no lazy-loaded relationships (all simple columns) - Pydantic conversion (UserModel.model_validate) happens while session is open - Returned object is pure Pydantic with no SQLAlchemy ties Combined with the telemetry efficiency fix, this resolves connection pool exhaustion for high-concurrency deployments, particularly on network-attached databases like AWS Aurora where connection hold time is more impactful. ### Contributor License Agreement By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](https://github.com/open-webui/open-webui/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. > [!NOTE] > Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-20 06:03:48 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#25664