Skip to content

Commit dc91a42

Browse files
matteiusclaude
andcommitted
fix: catch-all session rollback and separate idle transaction timeout
The session dependency only caught ConnectionRefusedError and SQLAlchemyError, leaving connections dirty for any other exception type. This caused ROLLBACK queries to accumulate in PostgreSQL and never terminate, exhausting the connection pool. - Catch all Exception types in get_main_async_session with protected rollback (rollback failure is logged, not swallowed) - Add separate database_idle_in_transaction_timeout setting (15s) independent of statement_timeout (30s) - Add explicit pool_reset_on_return="rollback" to engine config Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 68f2c4d commit dc91a42

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

‎fief/db/engine.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def create_engine(
2323
"pool_size": settings.database_pool_size,
2424
"max_overflow": settings.database_pool_max_overflow,
2525
"pool_timeout": settings.database_pool_timeout,
26+
"pool_reset_on_return": "rollback",
2627
}
2728
)
2829
# Set statement timeout for PostgreSQL via asyncpg server_settings
@@ -32,7 +33,7 @@ def create_engine(
3233
settings.database_statement_timeout
3334
)
3435
connect_args["server_settings"]["idle_in_transaction_session_timeout"] = str(
35-
settings.database_statement_timeout
36+
settings.database_idle_in_transaction_timeout
3637
)
3738
engine = create_async_engine(database_url, **engine_params)
3839

‎fief/dependencies/db.py‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ async def get_main_async_session(
1414
async with request.state.main_async_session_maker() as session:
1515
try:
1616
yield session
17-
except ConnectionRefusedError as e:
18-
await session.rollback()
19-
raise HTTPException(
20-
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
21-
detail=APIErrorCode.SERVER_DATABASE_NOT_AVAILABLE,
22-
) from e
23-
except SQLAlchemyError as e:
24-
logger.error("Database error, rolling back session", error=str(e))
25-
await session.rollback()
26-
raise HTTPException(
27-
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
28-
detail=APIErrorCode.SERVER_DATABASE_NOT_AVAILABLE,
29-
) from e
17+
except Exception as e:
18+
try:
19+
await session.rollback()
20+
except Exception:
21+
logger.error(
22+
"Failed to rollback session during error handling",
23+
exc_info=True,
24+
)
25+
if isinstance(e, (ConnectionRefusedError, SQLAlchemyError)):
26+
raise HTTPException(
27+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
28+
detail=APIErrorCode.SERVER_DATABASE_NOT_AVAILABLE,
29+
) from e
30+
raise

‎fief/settings_class.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class Settings(BaseSettings):
7272
database_pool_max_overflow: int = 10
7373
database_pool_timeout: int = 30
7474
database_statement_timeout: int = 30000
75+
database_idle_in_transaction_timeout: int = 15000
7576
database_table_prefix: str = "fief_"
7677

7778
redis_url: str = "redis://localhost:6379"

0 commit comments

Comments
 (0)