From bbcb79cc1ece6446593254366538f86549310d51 Mon Sep 17 00:00:00 2001 From: giles Date: Tue, 3 Feb 2026 23:01:43 +0000 Subject: [PATCH] Fix database connection pool leak in init_db() init_db() was creating new pools without checking if one already exists, causing "too many clients already" errors under load. Added early return if pool is already initialized and set explicit pool limits (min=2, max=10). Co-Authored-By: Claude Opus 4.5 --- database.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/database.py b/database.py index 162c3bd..398c8b7 100644 --- a/database.py +++ b/database.py @@ -168,7 +168,9 @@ CREATE INDEX IF NOT EXISTS idx_friendly_names_latest ON friendly_names(actor_id, async def init_db(): """Initialize database connection pool and create schema.""" global pool - pool = await asyncpg.create_pool(DATABASE_URL) + if pool is not None: + return # Already initialized + pool = await asyncpg.create_pool(DATABASE_URL, min_size=2, max_size=10) async with pool.acquire() as conn: await conn.execute(SCHEMA_SQL)