From 03c954b5b0938206a1ea41205ddcc093262e0c4f Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Fri, 22 Apr 2022 14:28:33 +0000 Subject: [PATCH] [FIX] core: always close cursors at the exit of context managers The call to `commit()` can fail during the flush of the environment and raise an exception before the actual `COMMIT;`, leaving the cursor unclosed. This leaked cursor may hold locks that only be released when the GC collects it. On low-traffic workers like the CronWorker, this can lock usual database usages. X-original-commit: 3cb3cec67c28ca147288103adb219e6da8694248 Part-of: odoo/odoo#93993 --- odoo/sql_db.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/odoo/sql_db.py b/odoo/sql_db.py index 6a015165d87..85dae5c92a3 100644 --- a/odoo/sql_db.py +++ b/odoo/sql_db.py @@ -198,9 +198,11 @@ class BaseCursor: return self def __exit__(self, exc_type, exc_value, traceback): - if exc_type is None: - self.commit() - self.close() + try: + if exc_type is None: + self.commit() + finally: + self.close() class Cursor(BaseCursor):