From 6b2ca9639795ac5aa3581cda774162793014a018 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Wed, 9 Sep 2026 20:25:36 -0400 Subject: [PATCH 1/3] fix(db): recycle pooled connections before the server drops them The engine sets `pool_pre_ping` but no `pool_recycle`, so a connection the database closed for being idle is still handed out, and the ping on that half-closed socket blocks the worker until TCP gives up: tens of seconds to minutes of an unresponsive API for every request that draws it, while a fresh connection to the same server answers instantly. Default to 300s, under the lowest `wait_timeout` MariaDB is commonly configured with, and make it settable. Co-Authored-By: Claude Opus 5 (1M context) --- backend/config/__init__.py | 3 +++ backend/handler/database/base_handler.py | 10 ++++++++-- docs/BACKEND_ARCHITECTURE.md | 18 ++++++++++-------- env.template | 1 + 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/backend/config/__init__.py b/backend/config/__init__.py index ed32dde1b..bed9e9e59 100644 --- a/backend/config/__init__.py +++ b/backend/config/__init__.py @@ -72,6 +72,9 @@ DB_PASSWD: Final[str | None] = _get_env("DB_PASSWD") DB_NAME: Final[str] = _get_env("DB_NAME", "romm") DB_QUERY_JSON: Final[str | None] = _get_env("DB_QUERY_JSON") ROMM_DB_DRIVER: Final[str] = _get_env("ROMM_DB_DRIVER", "mariadb") +# Retire a pooled connection before the server can drop it for being idle. +# Under MariaDB's lowest common `wait_timeout` (600s); 0 disables recycling. +DB_POOL_RECYCLE_SECONDS: Final[int] = safe_int(_get_env("DB_POOL_RECYCLE_SECONDS"), 300) # REDIS REDIS_HOST: Final[str | None] = _get_env("REDIS_HOST") diff --git a/backend/handler/database/base_handler.py b/backend/handler/database/base_handler.py index 8e079dff8..a727b678f 100644 --- a/backend/handler/database/base_handler.py +++ b/backend/handler/database/base_handler.py @@ -4,11 +4,17 @@ import time from sqlalchemy import create_engine, event from sqlalchemy.orm import sessionmaker -from config import DEV_SQL_ECHO +from config import DB_POOL_RECYCLE_SECONDS, DEV_SQL_ECHO from config.config_manager import ConfigManager +# `pool_pre_ping` alone still hands out a connection the server closed while it +# sat idle, and the ping on a half-closed socket blocks the worker until TCP +# gives up. Recycling retires it first. sync_engine = create_engine( - ConfigManager.get_db_engine(), pool_pre_ping=True, echo=False + ConfigManager.get_db_engine(), + pool_pre_ping=True, + pool_recycle=DB_POOL_RECYCLE_SECONDS if DB_POOL_RECYCLE_SECONDS > 0 else -1, + echo=False, ) sync_session = sessionmaker(bind=sync_engine, expire_on_commit=False) diff --git a/docs/BACKEND_ARCHITECTURE.md b/docs/BACKEND_ARCHITECTURE.md index 93f5e6830..7c45bc13a 100644 --- a/docs/BACKEND_ARCHITECTURE.md +++ b/docs/BACKEND_ARCHITECTURE.md @@ -1545,14 +1545,16 @@ Falls back to `FakeRedis` in test mode. #### Database -| Variable | Default | Description | -| ---------------- | --------- | ----------------------------------- | -| `ROMM_DB_DRIVER` | `mariadb` | `mariadb`, `mysql`, or `postgresql` | -| `DB_HOST` | | Database host | -| `DB_PORT` | `3306` | Database port | -| `DB_USER` | | Database user | -| `DB_PASSWD` | | Database password | -| `DB_NAME` | `romm` | Database name | +| Variable | Default | Description | +| ------------------------- | --------- | ----------------------------------------------------------------- | +| `ROMM_DB_DRIVER` | `mariadb` | `mariadb`, `mysql`, or `postgresql` | +| `DB_HOST` | | Database host | +| `DB_PORT` | `3306` | Database port | +| `DB_USER` | | Database user | +| `DB_PASSWD` | | Database password | +| `DB_NAME` | `romm` | Database name | +| `DB_QUERY_JSON` | | Extra connection parameters, as JSON | +| `DB_POOL_RECYCLE_SECONDS` | `300` | Retire a pooled connection after this long (`0` to never recycle) | #### Redis diff --git a/env.template b/env.template index 2eca619de..897e1df79 100644 --- a/env.template +++ b/env.template @@ -14,6 +14,7 @@ DB_USER= # Database username (should match MARIADB_USER in MariaDB) [REQUIRED] DB_PASSWD= # Database password (should match MARIADB_PASSWORD in MariaDB) [REQUIRED] DB_ROOT_PASSWD= # Database root user password (only used by the bundled MariaDB container) DB_QUERY_JSON= # Extra query parameters for the database connection, as JSON +DB_POOL_RECYCLE_SECONDS=300 # Retire a pooled connection after this long, before the server drops it for being idle (0 to never recycle) # Redis/Valkey REDIS_HOST=127.0.0.1 # Host name of the Redis/Valkey instance From 21d9a7f3c613db52e00727ca1e4689192096fada Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Thu, 10 Sep 2026 10:48:54 -0400 Subject: [PATCH 2/3] refactor(db): give the pool recycle sentinel one home An operator's `0` means "off", but SQLAlchemy gates recycling on `_recycle > -1`, so a literal 0 recycles on every checkout: a fresh connect and auth handshake per pool checkout. Mapping it to -1 is therefore load-bearing, but doing it at the engine left the value defined in `config` and corrected in `base_handler`, so a second consumer could reasonably pass the raw constant and reintroduce the footgun. Fold the mapping into the constant. Also drop the `wait_timeout` figure the comment attributed to MariaDB. Its default is 28800s; 600s is a value some hosts lower it to, not a MariaDB one. Co-Authored-By: Claude Opus 5 (1M context) --- backend/config/__init__.py | 8 +++++--- backend/handler/database/base_handler.py | 7 +++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/backend/config/__init__.py b/backend/config/__init__.py index bed9e9e59..1280365ee 100644 --- a/backend/config/__init__.py +++ b/backend/config/__init__.py @@ -72,9 +72,11 @@ DB_PASSWD: Final[str | None] = _get_env("DB_PASSWD") DB_NAME: Final[str] = _get_env("DB_NAME", "romm") DB_QUERY_JSON: Final[str | None] = _get_env("DB_QUERY_JSON") ROMM_DB_DRIVER: Final[str] = _get_env("ROMM_DB_DRIVER", "mariadb") -# Retire a pooled connection before the server can drop it for being idle. -# Under MariaDB's lowest common `wait_timeout` (600s); 0 disables recycling. -DB_POOL_RECYCLE_SECONDS: Final[int] = safe_int(_get_env("DB_POOL_RECYCLE_SECONDS"), 300) +# Kept under the idle `wait_timeout` a host may impose. A user's `0` means "off", +# which is SQLAlchemy's -1; its own 0 recycles on every checkout instead. +DB_POOL_RECYCLE_SECONDS: Final[int] = ( + safe_int(_get_env("DB_POOL_RECYCLE_SECONDS"), 300) or -1 +) # REDIS REDIS_HOST: Final[str | None] = _get_env("REDIS_HOST") diff --git a/backend/handler/database/base_handler.py b/backend/handler/database/base_handler.py index a727b678f..0fdc95cd4 100644 --- a/backend/handler/database/base_handler.py +++ b/backend/handler/database/base_handler.py @@ -7,13 +7,12 @@ from sqlalchemy.orm import sessionmaker from config import DB_POOL_RECYCLE_SECONDS, DEV_SQL_ECHO from config.config_manager import ConfigManager -# `pool_pre_ping` alone still hands out a connection the server closed while it -# sat idle, and the ping on a half-closed socket blocks the worker until TCP -# gives up. Recycling retires it first. +# A ping on a socket the server already closed blocks the worker until TCP gives +# up, so `pool_pre_ping` alone is not enough. sync_engine = create_engine( ConfigManager.get_db_engine(), pool_pre_ping=True, - pool_recycle=DB_POOL_RECYCLE_SECONDS if DB_POOL_RECYCLE_SECONDS > 0 else -1, + pool_recycle=DB_POOL_RECYCLE_SECONDS, echo=False, ) sync_session = sessionmaker(bind=sync_engine, expire_on_commit=False) From b9d2cb491e4cdf7a10b12cf7380eab88cf66311b Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Thu, 10 Sep 2026 10:57:17 -0400 Subject: [PATCH 3/3] refactor(db): pass the configured recycle value straight to the pool Drop the `0` to `-1` remap and hand SQLAlchemy whatever the operator set, so the knob means exactly what the pool means by it. `-1` is now the documented way to never recycle, in both `env.template` and the env var table. Co-Authored-By: Claude Opus 5 (1M context) --- backend/config/__init__.py | 7 ++----- docs/BACKEND_ARCHITECTURE.md | 20 ++++++++++---------- env.template | 2 +- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/backend/config/__init__.py b/backend/config/__init__.py index 1280365ee..b929dd9b5 100644 --- a/backend/config/__init__.py +++ b/backend/config/__init__.py @@ -72,11 +72,8 @@ DB_PASSWD: Final[str | None] = _get_env("DB_PASSWD") DB_NAME: Final[str] = _get_env("DB_NAME", "romm") DB_QUERY_JSON: Final[str | None] = _get_env("DB_QUERY_JSON") ROMM_DB_DRIVER: Final[str] = _get_env("ROMM_DB_DRIVER", "mariadb") -# Kept under the idle `wait_timeout` a host may impose. A user's `0` means "off", -# which is SQLAlchemy's -1; its own 0 recycles on every checkout instead. -DB_POOL_RECYCLE_SECONDS: Final[int] = ( - safe_int(_get_env("DB_POOL_RECYCLE_SECONDS"), 300) or -1 -) +# Kept under the idle `wait_timeout` a host may impose; -1 never recycles. +DB_POOL_RECYCLE_SECONDS: Final[int] = safe_int(_get_env("DB_POOL_RECYCLE_SECONDS"), 300) # REDIS REDIS_HOST: Final[str | None] = _get_env("REDIS_HOST") diff --git a/docs/BACKEND_ARCHITECTURE.md b/docs/BACKEND_ARCHITECTURE.md index 7c45bc13a..70eb042fb 100644 --- a/docs/BACKEND_ARCHITECTURE.md +++ b/docs/BACKEND_ARCHITECTURE.md @@ -1545,16 +1545,16 @@ Falls back to `FakeRedis` in test mode. #### Database -| Variable | Default | Description | -| ------------------------- | --------- | ----------------------------------------------------------------- | -| `ROMM_DB_DRIVER` | `mariadb` | `mariadb`, `mysql`, or `postgresql` | -| `DB_HOST` | | Database host | -| `DB_PORT` | `3306` | Database port | -| `DB_USER` | | Database user | -| `DB_PASSWD` | | Database password | -| `DB_NAME` | `romm` | Database name | -| `DB_QUERY_JSON` | | Extra connection parameters, as JSON | -| `DB_POOL_RECYCLE_SECONDS` | `300` | Retire a pooled connection after this long (`0` to never recycle) | +| Variable | Default | Description | +| ------------------------- | --------- | ------------------------------------------------------------------ | +| `ROMM_DB_DRIVER` | `mariadb` | `mariadb`, `mysql`, or `postgresql` | +| `DB_HOST` | | Database host | +| `DB_PORT` | `3306` | Database port | +| `DB_USER` | | Database user | +| `DB_PASSWD` | | Database password | +| `DB_NAME` | `romm` | Database name | +| `DB_QUERY_JSON` | | Extra connection parameters, as JSON | +| `DB_POOL_RECYCLE_SECONDS` | `300` | Retire a pooled connection after this long (`-1` to never recycle) | #### Redis diff --git a/env.template b/env.template index 897e1df79..0c2ec104e 100644 --- a/env.template +++ b/env.template @@ -14,7 +14,7 @@ DB_USER= # Database username (should match MARIADB_USER in MariaDB) [REQUIRED] DB_PASSWD= # Database password (should match MARIADB_PASSWORD in MariaDB) [REQUIRED] DB_ROOT_PASSWD= # Database root user password (only used by the bundled MariaDB container) DB_QUERY_JSON= # Extra query parameters for the database connection, as JSON -DB_POOL_RECYCLE_SECONDS=300 # Retire a pooled connection after this long, before the server drops it for being idle (0 to never recycle) +DB_POOL_RECYCLE_SECONDS=300 # Retire a pooled connection after this long, before the server drops it for being idle (-1 to never recycle) # Redis/Valkey REDIS_HOST=127.0.0.1 # Host name of the Redis/Valkey instance