Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(db): enable pool_pre_ping to survive PostgreSQL failovers (#1311)

Tiago Fortunato Linob778472b30e7
diff --git a/backend/app/core/database.py b/backend/app/core/database.py
index cdb96478..5c590c3e 100644
--- a/backend/app/core/database.py
+++ b/backend/app/core/database.py
@@ -7,7 +7,15 @@ from app.core.config import get_settings
settings = get_settings()
-engine = create_async_engine(settings.database_url, echo=settings.debug)
+# pool_pre_ping validates a pooled connection before handing it to the
+# application. Without it, a PostgreSQL failover leaves dead connections in
+# the pool and the first request that borrows one fails with
+# "connection is closed" instead of transparently reconnecting.
+engine = create_async_engine(
+ settings.database_url,
+ echo=settings.debug,
+ pool_pre_ping=True,
+)
async_session_maker = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
diff --git a/backend/tests/test_database_pool.py b/backend/tests/test_database_pool.py
new file mode 100644
index 00000000..2499d571
--- /dev/null
+++ b/backend/tests/test_database_pool.py
@@ -0,0 +1,66 @@
+import os
+import subprocess
+import sys
+import textwrap
+
+import pytest
+
+# conftest imports app.core.database, which builds the engine from
+# DATABASE_URL at import time. Run the application engine in a fresh
+# interpreter so the test observes production code, not a private engine.
+RECONNECT_SCRIPT = textwrap.dedent(
+ """
+ import asyncio
+
+ from sqlalchemy import text
+ from sqlalchemy.ext.asyncio import create_async_engine
+
+ from app.core.database import engine
+
+
+ async def main() -> None:
+ url = engine.url.render_as_string(hide_password=False)
+ killer = create_async_engine(url, pool_size=1, max_overflow=0)
+
+ async with engine.connect() as connection:
+ backend_pid = await connection.scalar(text("select pg_backend_pid()"))
+
+ async with killer.connect() as connection:
+ await connection.execute(
+ text("select pg_terminate_backend(:pid)"), {"pid": backend_pid}
+ )
+
+ async with engine.connect() as connection:
+ assert await connection.scalar(text("select 1")) == 1
+
+ await engine.dispose()
+ await killer.dispose()
+
+
+ asyncio.run(main())
+ """
+)
+
+
+def test_pool_recovers_from_a_dropped_connection():
+ """A pooled connection killed server-side must not fail the next request.
+
+ This is what happens during a PostgreSQL failover: the primary goes away
+ and every connection the pool still holds is dead. ``pool_pre_ping``
+ detects that before handing the connection out and transparently opens a
+ new one.
+ """
+ url = os.environ.get("POSTGRES_TEST_URL")
+
+ if not url:
+ pytest.skip("isolated PostgreSQL not configured")
+
+ result = subprocess.run(
+ [sys.executable, "-c", RECONNECT_SCRIPT],
+ env={**os.environ, "DATABASE_URL": url},
+ capture_output=True,
+ text=True,
+ check=False,
+ )
+
+ assert result.returncode == 0, result.stderr