open-webui/backend/open_webui/utils
Classic298 3f133fad56
fix: release database connections immediately after auth instead of holding during LLM calls (#20545)
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.
2026-01-10 15:34:36 +04:00
..
db refac 2025-12-30 18:28:57 +04:00
images refac/fix: comfyui filter output node type 2025-12-30 12:24:48 +04:00
mcp chore: format 2025-10-26 19:33:39 -07:00
telemetry fix: use efficient COUNT queries in telemetry metrics to prevent connection pool exhaustion (#20542) 2026-01-10 15:33:42 +04:00
access_control.py refac/enh: db session sharing 2025-12-29 00:21:18 +04:00
audit.py fix: audit 2025-12-01 10:59:01 -05:00
auth.py fix: release database connections immediately after auth instead of holding during LLM calls (#20545) 2026-01-10 15:34:36 +04:00
channels.py feat: channel/thread @ model 2025-09-17 00:49:44 -05:00
chat.py chore: format 2026-01-09 02:46:04 +04:00
code_interpreter.py chore/perf: Remove old SRC level log env vars with no impact (#20045) 2025-12-20 08:16:14 -05:00
embeddings.py chore/perf: Remove old SRC level log env vars with no impact (#20045) 2025-12-20 08:16:14 -05:00
files.py refac 2025-12-22 00:39:05 +04:00
filter.py chore/perf: Remove old SRC level log env vars with no impact (#20045) 2025-12-20 08:16:14 -05:00
groups.py refac/enh: db session sharing 2025-12-29 00:21:18 +04:00
headers.py refac: images 2025-11-04 13:30:59 -05:00
logger.py refac 2026-01-01 01:51:37 +04:00
middleware.py enh: kb metadata search 2026-01-09 22:21:00 +04:00
misc.py fix/refac: stt default content type 2025-12-22 09:45:55 +04:00
models.py refac 2026-01-05 03:46:46 +04:00
oauth.py refac/fix: oauth discovery urls 2026-01-01 14:01:18 +04:00
payload.py refac 2025-10-25 23:01:13 -07:00
pdf_generator.py chore: remove unnecessary Path conversions 2025-03-04 19:53:52 +02:00
plugin.py refac 2026-01-03 18:43:12 +04:00
rate_limit.py feat: signin rate limit 2025-12-02 03:52:38 -05:00
redis.py properly handle async-generator Redis methods in SentinelRedisProx to fix changed YDocManager's remove_user_from_all_documents (#20145) 2025-12-31 17:15:24 -05:00
response.py fix: ollama tool call 2025-07-18 06:11:53 +08:00
security_headers.py chore: format 2024-11-30 23:36:30 -08:00
task.py chore/perf: Remove old SRC level log env vars with no impact (#20045) 2025-12-20 08:16:14 -05:00
tools.py enh: kb metadata search 2026-01-09 22:21:00 +04:00
webhook.py refac 2026-01-08 00:42:29 +04:00