From f055b361aaeed449136d184cea20c20a66cee86c Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Tue, 24 Feb 2026 18:39:53 -0500 Subject: [PATCH] Add pool_pre_ping and pool_recycle to SQLAlchemy engine Without pool_pre_ping, stale pooled connections (left over after a Postgres restart or network blip) cause immediate query failures. SQLAlchemy then propagates the error rather than transparently reconnecting, which crashes the Quart request handler and triggers a Swarm restart loop. - pool_pre_ping=True: issues a lightweight SELECT 1 before each checkout; discards and replaces stale connections silently - pool_recycle=1800: recycles connections every 30 minutes to prevent long-idle connections from going stale at the TCP/firewall level Co-Authored-By: Claude Sonnet 4.6 --- src/fabledassistant/models/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/fabledassistant/models/__init__.py b/src/fabledassistant/models/__init__.py index 4c77e2a..bada508 100644 --- a/src/fabledassistant/models/__init__.py +++ b/src/fabledassistant/models/__init__.py @@ -3,7 +3,12 @@ from sqlalchemy.orm import DeclarativeBase from fabledassistant.config import Config -engine = create_async_engine(Config.DATABASE_URL, echo=False) +engine = create_async_engine( + Config.DATABASE_URL, + echo=False, + pool_pre_ping=True, + pool_recycle=1800, +) async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)