Skip to content

Commit

Permalink
Close database connections after exiting from the db_session to avoid…
Browse files Browse the repository at this point in the history
… access violation on Python 3.10
  • Loading branch information
kozlovsky committed Mar 27, 2024
1 parent f941b89 commit 1d4c49b
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/tribler/core/utilities/pony_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1d4c49b

Please sign in to comment.