Bug 57680 - sqlalchemy sessions not correctly closed for UMC session database
Summary: sqlalchemy sessions not correctly closed for UMC session database
Status: CLOSED FIXED
Alias: None
Product: UCS
Classification: Unclassified
Component: UMC (Generic)
Version: UCS 5.0
Hardware: Other Linux
: P5 normal
Target Milestone: UCS 5.0-9-errata
Assignee: Marius Meschter
QA Contact: Felix Botner
URL: https://git.knut.univention.de/univen...
Keywords:
Depends on:
Blocks:
 
Reported: 2024-10-21 10:59 CEST by Marius Meschter
Modified: 2025-03-21 13:53 CET (History)
2 users (show)

See Also:
What kind of report is it?: Bug Report
What type of bug is this?: 5: Major Usability: Impairs usability in key scenarios
Who will be affected by this bug?: 1: Will affect a very few installed domains
How will those affected feel about the bug?: 5: Blocking further progress on the daily work
User Pain: 0.143
Enterprise Customer affected?: Yes
School Customer affected?:
ISV affected?:
Waiting Support: Yes
Flags outvoted (downgraded) after PO Review:
Ticket number:
Bug group (optional): Large environments
Customer ID: 02149
Max CVSS v3 score:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marius Meschter univentionstaff 2024-10-21 10:59:17 CEST
The `get_session` context manager is used for retrieving sqlalchemy database sessions. However it does not close the session after usage.
This can lead to errors like 'sqlalchemy.exc.InvalidRequestError: This session is in 'prepared' state; no further SQL can be emitted within this transaction.'.

SQLAlchemy tries to re-use a session that 'commit' was already called on which causes this error.
This can be fixed by properly closing the session by calling session.close in the session_db:py:112 (get_session) context manager.

Furthermore the current code uses `scoped_session` in DbRegistry.__create which is not correct. 
The `scoped_session` by default uses a thread local scope, meaning one session is created per thread. However that is not how tornado works which rather is single threaded using an event loop.
Comment 2 Marius Meschter univentionstaff 2024-10-23 16:32:42 CEST
univention-management-console.yaml
d9f9a3a2b9f3 | Bug #57680: Fix session handling in UMC and run perf tests with psql

univention-management-console (12.0.35-8)
d9f9a3a2b9f3 | Bug #57680: Fix session handling in UMC and run perf tests with psql

Successful build
Package: univention-management-console
Version: 12.0.35-8
Branch: 5.0-0
Scope: errata5.0-9

Successful build
Package: univention-management-console
Version: 13.0.28
Branch: 5.1-0

Successful build
Package: univention-management-console
Version: 14.0.39
Branch: 5.2-0
Comment 3 Felix Botner univentionstaff 2024-10-24 12:39:45 CEST
OK - changes
OK - yaml
OK - 5.0-9, 5.1 and 5.2