From 1d4c49b7a34c8d53089dfcdf30ba890c088f1ace Mon Sep 17 00:00:00 2001 From: Alexander Kozlovsky Date: Wed, 27 Mar 2024 08:34:27 +0100 Subject: [PATCH] Close database connections after exiting from the db_session to avoid access violation on Python 3.10 --- src/tribler/core/utilities/pony_utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/tribler/core/utilities/pony_utils.py b/src/tribler/core/utilities/pony_utils.py index 3e34d2f5009..05eaf4b809f 100644 --- a/src/tribler/core/utilities/pony_utils.py +++ b/src/tribler/core/utilities/pony_utils.py @@ -345,6 +345,15 @@ def release_lock(self): lock_hold_duration = time.time() - acquire_time info.lock_hold_total_duration += lock_hold_duration + def release(self, connection, cache=None): + super().release(connection, cache) + + # This method is called after exiting the db_session context manager. Usually, after db_session finishes, + # PonyORM releases the connection to the connection pool. However, if the thread finishes, the connection + # remains open. Later, if such a connection is garbage collected, it may lead to memory corruption errors. + # To avoid this, we close the connection after the db_session is over by calling the `disconnect` method. + self.pool.disconnect() + db_session = TriblerDbSession() orm.db_session = orm.core.db_session = db_session