From 061931696c8888c9304d20bd1e3656a75eedebb5 Mon Sep 17 00:00:00 2001 From: sergio-teruel Date: Thu, 24 Sep 2026 09:49:10 +0200 Subject: [PATCH 1/2] [FIX] base_exception: don't hang when the ongoing transaction locks the rows Exceptions are written through an independent connection so they survive a rollback of the ongoing transaction. When the ongoing transaction has already written the same rows (e.g. a confirmed sale order line edited and flushed earlier in the request), the independent connection waits for its lock while the ongoing transaction waits in Python for the independent one to finish. PostgreSQL cannot detect that deadlock, so the request hung until the worker was killed, and every other request touching the same records piled up behind it. The independent transaction now waits for row locks at most `_get_exception_lock_timeout()` milliseconds. When that expires, the exceptions are written in the ongoing transaction, and written again in a new transaction if the ongoing one is rolled back (as it is when the exception is raised to the user), once its locks are released, so the exception popup still finds them. The logic lives in `_write_exceptions_independently()` so modules writing exceptions through their own independent connection, like sale_exception for order lines, can reuse it. --- .../models/base_exception_method.py | 89 ++++++++++++++++--- 1 file changed, 76 insertions(+), 13 deletions(-) diff --git a/base_exception/models/base_exception_method.py b/base_exception/models/base_exception_method.py index 72791998bdf..5710f281f9b 100644 --- a/base_exception/models/base_exception_method.py +++ b/base_exception/models/base_exception_method.py @@ -8,11 +8,13 @@ import logging from collections import defaultdict +from psycopg2.errors import LockNotAvailable + from odoo import Command, _, api, models from odoo.api import Environment from odoo.exceptions import UserError from odoo.osv import expression -from odoo.tools import config +from odoo.tools import config, mute_logger from odoo.tools.safe_eval import safe_eval from ..exceptions import BaseExceptionError @@ -106,6 +108,15 @@ def detect_exceptions(self): main_records = self._get_main_records() must_rollback = self._must_rollback_on_exception() test_mode = test_mode or not must_rollback + + def write_exceptions(env): + for rule_id, records in rules_to_remove.items(): + records.with_env(env).write( + {"exception_ids": [Command.unlink(rule_id)]} + ) + for rule_id, records in rules_to_add.items(): + records.with_env(env).write({"exception_ids": [Command.link(rule_id)]}) + # Write exceptions in a new transaction to be committed so that we can # rollback the ongoing one while keeping the exceptions stored with self.env.registry.cursor() as new_cr: @@ -114,27 +125,20 @@ def detect_exceptions(self): if not test_mode else self.env ) - for rule_id, records in rules_to_remove.items(): - records.with_env(new_env).write( - {"exception_ids": [Command.unlink(rule_id)]} - ) - for rule_id, records in rules_to_add.items(): - records.with_env(new_env).write( - {"exception_ids": [Command.link(rule_id)]} - ) + write_env = self._write_exceptions_independently(new_env, write_exceptions) # In case we have new exception, or exceptions that were not ignored yet, or # blocking exceptions, we need to raise an exception to rollback the # ongoing transaction. - # Re-derive main_records through new_env rather than re-running - # self.with_env(new_env)._get_main_records(): when self are + # Re-derive main_records through write_env rather than re-running + # self.with_env(write_env)._get_main_records(): when self are # records just created earlier in the ongoing (not yet committed) # transaction (e.g. a line added while editing a confirmed sale # order), new_cr is a genuinely separate DB connection that # cannot see them yet, and _get_main_records() traversal # (e.g. sale.order.line -> order_id) would raise MissingError. # main_records itself was already resolved above through the - # current cursor, so only rebinding it to new_env is needed here. - main_records_new_env = main_records.with_env(new_env) + # current cursor, so only rebinding it to write_env is needed here. + main_records_new_env = main_records.with_env(write_env) if ( rules_to_add or main_records_new_env._must_raise_exception_after_detection() @@ -159,6 +163,65 @@ def _must_rollback_on_exception(self): allowed.get(main_records._name, ()) ) + def _get_exception_lock_timeout(self): + """Milliseconds the independent transaction waits for a row lock + before giving up on writing the exceptions on its own.""" + return 2000 + + def _write_exceptions_independently(self, new_env, write_func): + """Call ``write_func(env)`` to write exception data through + ``new_env``, whose independent transaction is committed so the data + survives a rollback of the ongoing one. + + The ongoing transaction may already hold locks on the rows to write + (e.g. an order line it has just written). The independent + transaction would then wait for the ongoing one, which is itself + waiting for this method to return: PostgreSQL cannot detect that + deadlock and the request would hang forever. So the independent + transaction only waits ``_get_exception_lock_timeout()``; then the + data is written in the ongoing transaction, and written again in a + new transaction if the ongoing one is rolled back, once its locks + are released. + + :return: the environment the data was written with + """ + if new_env is self.env: + write_func(new_env) + return new_env + try: + with mute_logger("odoo.sql_db"), new_env.cr.savepoint(): + new_env.cr.execute( + "SET LOCAL lock_timeout = %s", + (self._get_exception_lock_timeout(),), + ) + write_func(new_env) + except LockNotAvailable: + _logger.info( + "Rows locked, exceptions of %s are written in the ongoing " + "transaction and again if it is rolled back", + self, + ) + else: + return new_env + write_func(self.env) + registry = self.env.registry + uid = self.env.uid + context = dict(self.env.context) + + @self.env.cr.postrollback.add + def write_exceptions_after_rollback(): + # Records created by the rolled back transaction no longer exist, + # and an error here would break closing its cursor. + try: + with registry.cursor() as cr: + write_func(Environment(cr, uid, context)) + except Exception as error: + _logger.warning( + "Exceptions could not be written after a rollback: %s", error + ) + + return self.env + def _detect_exception_get_exc_class_values(self): return { "src_model": self._name, From 398df8e8a05ac0f13eeb7fce9a5b6be7f3ad76c7 Mon Sep 17 00:00:00 2001 From: sergio-teruel Date: Thu, 24 Sep 2026 10:32:03 +0200 Subject: [PATCH 2/2] [FIX] base_exception: raise exceptions for records created in the ongoing transaction The independent connection cannot see records created by the ongoing transaction, e.g. a line added to a confirmed sale order, so writing their exceptions through it raises `MissingError` and the user got that error instead of the exception popup. `_write_exceptions_independently()` now handles `MissingError` like a lock timeout: the exceptions are written in the ongoing transaction, so they are detected and raised, and written again after a rollback, skipping the records that no longer exist. --- .../models/base_exception_method.py | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/base_exception/models/base_exception_method.py b/base_exception/models/base_exception_method.py index 5710f281f9b..f9dae8f3142 100644 --- a/base_exception/models/base_exception_method.py +++ b/base_exception/models/base_exception_method.py @@ -12,7 +12,7 @@ from odoo import Command, _, api, models from odoo.api import Environment -from odoo.exceptions import UserError +from odoo.exceptions import MissingError, UserError from odoo.osv import expression from odoo.tools import config, mute_logger from odoo.tools.safe_eval import safe_eval @@ -178,9 +178,15 @@ def _write_exceptions_independently(self, new_env, write_func): transaction would then wait for the ongoing one, which is itself waiting for this method to return: PostgreSQL cannot detect that deadlock and the request would hang forever. So the independent - transaction only waits ``_get_exception_lock_timeout()``; then the - data is written in the ongoing transaction, and written again in a - new transaction if the ongoing one is rolled back, once its locks + transaction only waits ``_get_exception_lock_timeout()``. + + The records may also have been created by the ongoing transaction + (e.g. a line added to a confirmed order), which the independent + transaction cannot see yet. + + In both cases the data is written in the ongoing transaction, so + the exceptions are still detected and raised, and written again in + a new transaction if the ongoing one is rolled back, once its locks are released. :return: the environment the data was written with @@ -195,11 +201,12 @@ def _write_exceptions_independently(self, new_env, write_func): (self._get_exception_lock_timeout(),), ) write_func(new_env) - except LockNotAvailable: + except (LockNotAvailable, MissingError) as error: _logger.info( - "Rows locked, exceptions of %s are written in the ongoing " - "transaction and again if it is rolled back", + "Exceptions of %s are written in the ongoing transaction, and " + "again if it is rolled back, as the independent one failed: %s", self, + error, ) else: return new_env @@ -210,11 +217,15 @@ def _write_exceptions_independently(self, new_env, write_func): @self.env.cr.postrollback.add def write_exceptions_after_rollback(): - # Records created by the rolled back transaction no longer exist, - # and an error here would break closing its cursor. + # An error here would break closing the rolled back cursor. try: with registry.cursor() as cr: write_func(Environment(cr, uid, context)) + except MissingError: + _logger.debug( + "Records created by the rolled back transaction, no " + "exceptions to write" + ) except Exception as error: _logger.warning( "Exceptions could not be written after a rollback: %s", error