From c7189ebe9111192a67e22cf86a805a0c73ea8cf6 Mon Sep 17 00:00:00 2001 From: Suherdy Yacob Date: Thu, 11 Jun 2026 13:41:44 +0700 Subject: [PATCH] fix: rollback transaction on sanitize_context_cids failure to prevent InFailedSqlTransaction state --- models/ir_http.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/models/ir_http.py b/models/ir_http.py index 3d98084..1e8b0fa 100644 --- a/models/ir_http.py +++ b/models/ir_http.py @@ -33,7 +33,10 @@ class IrHttp(models.AbstractModel): if not valid_cids: valid_cids = [default_cid] if valid_cids != cids: - _logger.warning("Sanitized allowed_company_ids in context from %s to %s for user %s", cids, valid_cids, request.env.uid) + _logger.warning( + "Sanitized allowed_company_ids in context from %s to %s for user %s", + cids, valid_cids, request.env.uid, + ) context['allowed_company_ids'] = valid_cids return True return False @@ -59,8 +62,19 @@ class IrHttp(models.AbstractModel): kwargs_context = kwargs.get('context') if isinstance(kwargs_context, dict): sanitize_context_cids(kwargs_context, user_cids, default_cid) + except Exception as e: - _logger.warning("IrHttp: could not sanitize company context in _pre_dispatch: %s", e, exc_info=True) + _logger.warning( + "IrHttp: could not sanitize company context in _pre_dispatch: %s", e, + ) + # Roll back the aborted transaction so subsequent DB queries in this + # request can still execute. Without this PostgreSQL keeps the + # connection in an InFailedSqlTransaction state and rejects every + # further query in the same request. + try: + request.env.cr.rollback() + except Exception: + pass return super()._pre_dispatch(rule, args) @@ -87,7 +101,10 @@ class IrHttp(models.AbstractModel): if not valid_cids: valid_cids = [default_cid] if valid_cids != cids: - _logger.warning("Sanitized allowed_company_ids in _dispatch from %s to %s for user %s", cids, valid_cids, request.env.uid) + _logger.warning( + "Sanitized allowed_company_ids in _dispatch from %s to %s for user %s", + cids, valid_cids, request.env.uid, + ) context['allowed_company_ids'] = valid_cids return True return False @@ -104,7 +121,18 @@ class IrHttp(models.AbstractModel): kwargs_context = kwargs.get('context') if isinstance(kwargs_context, dict): sanitize_context_cids(kwargs_context, user_cids, default_cid) + except Exception as e: - _logger.warning("IrHttp: could not sanitize company context in _dispatch: %s", e, exc_info=True) + _logger.warning( + "IrHttp: could not sanitize company context in _dispatch: %s", e, + ) + # Roll back the aborted transaction so subsequent DB queries in this + # request can still execute. Without this PostgreSQL keeps the + # connection in an InFailedSqlTransaction state and rejects every + # further query in the same request. + try: + request.env.cr.rollback() + except Exception: + pass return super()._dispatch(endpoint)