From 9faee5d8b3157027c09fff28f33ebbd7c4f394a6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 13 Apr 2026 12:46:26 +0000 Subject: [PATCH 1/3] Initial plan From d15396b1f3818a1bb4421a55e51541ce6f4aaf93 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 13 Apr 2026 13:12:07 +0000 Subject: [PATCH 2/3] Fix cascading InFailedSqlTransaction by rolling back DB session in error handler Agent-Logs-Url: https://github.com/FlexMeasures/flexmeasures/sessions/3c38ba54-fc92-47ed-9a5c-e63721076a99 Co-authored-by: joshuaunity <45713692+joshuaunity@users.noreply.github.com> --- documentation/changelog.rst | 1 + flexmeasures/utils/error_utils.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/documentation/changelog.rst b/documentation/changelog.rst index 553b87a653..d5700f12fd 100644 --- a/documentation/changelog.rst +++ b/documentation/changelog.rst @@ -38,6 +38,7 @@ Infrastructure / Support Bugfixes ----------- +* Fix a cascading ``InFailedSqlTransaction`` error when a database programming error (e.g. a missing migration such as the one adding ``account.attributes``) leaves the PostgreSQL transaction in an aborted state, which prevented the error page from rendering properly [see `PR #2100 `_] v0.31.3 | April 11, 2026 diff --git a/flexmeasures/utils/error_utils.py b/flexmeasures/utils/error_utils.py index 1da731530d..3bf8013a21 100644 --- a/flexmeasures/utils/error_utils.py +++ b/flexmeasures/utils/error_utils.py @@ -89,6 +89,20 @@ def error_handling_router(error: HTTPException): else: log_error(error, getattr(error, "description", str(error))) + # Roll back any failed DB transaction so subsequent queries (e.g. in + # the error-page template) can run in a clean state. This is needed + # when a database error (e.g. UndefinedColumn due to a missing + # migration) leaves the PostgreSQL transaction in an aborted state; + # without the rollback every following query in the same request would + # fail with "InFailedSqlTransaction" and the error page itself could + # not be rendered properly. + try: + from flexmeasures.data import db + + db.session.rollback() + except Exception: + pass + error_text = getattr( error, "description", f"Something went wrong: {error.__class__.__name__}" ) From 7766d2e8d6830fb78728ca5a81abd369731923bf Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 13 Apr 2026 13:14:49 +0000 Subject: [PATCH 3/3] Address code review: log rollback failures and simplify changelog entry Agent-Logs-Url: https://github.com/FlexMeasures/flexmeasures/sessions/3c38ba54-fc92-47ed-9a5c-e63721076a99 Co-authored-by: joshuaunity <45713692+joshuaunity@users.noreply.github.com> --- documentation/changelog.rst | 2 +- flexmeasures/utils/error_utils.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/documentation/changelog.rst b/documentation/changelog.rst index d5700f12fd..15369977cd 100644 --- a/documentation/changelog.rst +++ b/documentation/changelog.rst @@ -38,7 +38,7 @@ Infrastructure / Support Bugfixes ----------- -* Fix a cascading ``InFailedSqlTransaction`` error when a database programming error (e.g. a missing migration such as the one adding ``account.attributes``) leaves the PostgreSQL transaction in an aborted state, which prevented the error page from rendering properly [see `PR #2100 `_] +* Fix error page rendering when a database error leaves the transaction in a failed state (e.g. when a missing migration such as the one adding ``account.attributes`` causes an ``InFailedSqlTransaction`` error) [see `PR #2100 `_] v0.31.3 | April 11, 2026 diff --git a/flexmeasures/utils/error_utils.py b/flexmeasures/utils/error_utils.py index 3bf8013a21..d768172591 100644 --- a/flexmeasures/utils/error_utils.py +++ b/flexmeasures/utils/error_utils.py @@ -100,8 +100,10 @@ def error_handling_router(error: HTTPException): from flexmeasures.data import db db.session.rollback() - except Exception: - pass + except Exception as rollback_exc: + current_app.logger.warning( + "Could not roll back DB session in error handler: %s", rollback_exc + ) error_text = getattr( error, "description", f"Something went wrong: {error.__class__.__name__}"