Roll back DB session in error handler to fix cascading InFailedSqlTransaction#2100
Draft
Roll back DB session in error handler to fix cascading InFailedSqlTransaction#2100
Conversation
Documentation build overview
Show files changed (2 files in total): 📝 2 modified | ➕ 0 added | ➖ 0 deleted
|
…ror 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>
Agent-Logs-Url: https://github.com/FlexMeasures/flexmeasures/sessions/3c38ba54-fc92-47ed-9a5c-e63721076a99 Co-authored-by: joshuaunity <45713692+joshuaunity@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix programming error on /assets page
Roll back DB session in error handler to fix cascading InFailedSqlTransaction
Apr 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a DB error (e.g.
UndefinedColumnfrom a missing migration likeaccount.attributes) aborts a PostgreSQL transaction mid-request, every subsequent query in that request fails withInFailedSqlTransaction— including the queries made byrender_flexmeasures_templatewhile building the error page. This caused the error page to fall back to barerender_template, losing all navigation/styling.Changes
flexmeasures/utils/error_utils.py: Roll back the DB session inerror_handling_routerbefore rendering the HTML error response. The rollback is guarded with a try/except that logs a warning on failure rather than silently swallowing it.documentation/changelog.rst: Add bugfix entry for v0.32.0.Note on the root cause
The specific error reported (
column account.attributes does not exist) is caused by not runningflexmeasures db upgradeafter pulling PR #2093, which added anattributescolumn to theaccounttable via migrationa3b1c2d4e5f6. The v0.32.0 changelog already includes the upgrade warning. This PR improves resilience so that any DB error in a view produces a properly rendered error page instead of a degraded fallback.Original prompt
i am gettgint eh belwo error, when i try to load up my /assets page
[FLEXMEASURES][2026-04-13 12:05:06,021] INFO: 127.0.0.1 - - [13/Apr/2026 12:05:06] "GET /favicon.ico HTTP/1.1" 304 -
[FLEXMEASURES][2026-04-13 12:05:06,031] INFO: 127.0.0.1 - - [13/Apr/2026 12:05:06] "GET /favicon.ico HTTP/1.1" 304 -
[FLEXMEASURES][2026-04-13 13:43:58,728] ERROR: ProgrammingError - URL was: /assets - "(psycopg2.errors.UndefinedColumn) column account.attributes does not exist
LINE 1: ...dary_color, account.logo_url AS account_logo_url, account.at...
^
[SQL: SELECT account.id AS account_id, account.name AS account_name, account.primary_color AS account_primary_color, account.secondary_color AS account_secondary_color, account.logo_url AS account_logo_url, account.attributes AS account_attributes, account.consultancy_account_id AS account_consultancy_account_id
FROM account
WHERE account.id = %(pk_1)s]
[parameters: {'pk_1': 1}]
(Background on this error at: https://sqlalche.me/e/20/f405)" [occurred at /home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py (in do_execute, line 952)
Traceback (most recent call last):
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/flask/app.py", line 917, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/flask/app.py", line 902, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/flask_classful.py", line 303, in proxy
response = view(**request.view_args)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/flask_classful.py", line 271, in inner
return fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/flask_login/utils.py", line 290, in decorated_view
return current_app.ensure_sync(func)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/flexmeasures/ui/views/assets/views.py", line 77, in index
user_can_create_assets=user_can_create_assets(),
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/flexmeasures/ui/views/assets/utils.py", line 27, in user_can_create_assets
account = current_user.account
^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 569, in get
return self.impl.get(state, dict_) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1096, in get
value = self._fire_loader_callables(state, key, passive)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1131, in fire_loader_callables
return self.callable(state, passive)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/strategies.py", line 978, in _load_for_state
return self._emit_lazyload(
^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/strategies.py", line 1079, in _emit_lazyload
return loading.load_on_pk_identity(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/loading.py", line 695, in load_on_pk_identity
session.execute(
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2351, in execute
return self._execute_internal(
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2249, in _execute_internal
result: Result[Any] = compile_state_cls.orm_execute_statement(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/...
Created from VS Code.