Skip to content

Commit

Permalink
Cherry-pick #7945: Close database connections after exiting from the …
Browse files Browse the repository at this point in the history
…db_session to avoid access violation on Python 3.10

(cherry picked from commit a84ba12)
  • Loading branch information
kozlovsky committed Mar 27, 2024
1 parent 30c40b1 commit e13ee42
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 @@ -338,6 +338,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 e13ee42

Please sign in to comment.