Skip to content

[18.0][FIX] base_exception: allow detecting exceptions without rollback - #3741

Merged
OCA-git-bot merged 1 commit into
OCA:18.0from
adhoc-dev:18.0-t-75454-gal
Oct 30, 2026
Merged

OCA-git-bot merged 1 commit into
OCA:18.0from
adhoc-dev:18.0-t-75454-gal

Conversation

@gal-adhoc

@gal-adhoc gal-adhoc commented Sep 24, 2026 •

Copy link
Copy Markdown

Follow-up of #3590.

Problem

detect_exceptions() stores the exceptions through a second cursor, so the ongoing transaction can be rolled back. When the ongoing transaction already wrote on the main records, the second cursor waits on the row lock that the same thread holds: the request never ends (until limit_time_real kills the worker).

Real case (Odoo 18, sale_exception): signing a quotation in the customer portal. portal_quote_accept writes the signature and flushes, then _validate_order() → action_confirm() → detect_exceptions(). pg_stat_activity shows the main connection idle in transaction and the second one waiting on Lock / transactionid for UPDATE sale_order SET main_exception_id ....

Change

_must_rollback_on_exception(), true by default. It is false for the main records listed in the context base_exception_no_rollback={model: ids}, and then detect_exceptions() stores the exceptions with the current cursor and does not raise BaseExceptionError. The caller decides with _must_raise_exception_after_detection().

The flag is scoped by model and ids on purpose: other exception models reached from the same call (e.g. purchase or stock exceptions triggered by the sale confirmation) keep the default behavior.

Nothing changes when the context key is not set.

The consumer is the sale-workflow PR for sale_exception, which will reference this one.

Relation with #3739

The two PRs solve different halves of the problem. #3739 fixes the hang itself, for any caller that wrote earlier in the transaction, and still raises BaseExceptionError so the backend popup keeps working. This PR does not fix that hang: with it applied, confirming an order from the backend after an earlier write on the same row still waits on the row lock forever. What it adds is a way for the caller to ask for no rollback, which #3739 does not cover, and which the portal needs to keep the signature it already wrote.

They compose: with this context set, new_env is self.env, which #3739 writes with directly. Both touch the same lines of detect_exceptions(), so whichever lands second needs a rebase. Happy to rebase this one on top of #3739.

@OCA-git-bot

Copy link
Copy Markdown
Contributor

Hi @sebastienbeau, @hparfr,
some modules you are maintaining are being modified, check this out!

@grindtildeath grindtildeath left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR, I've seen it's WIP but here's a small comment.

However, did you check #3739 yet?

):
raise_exception = True
if raise_exception:
if raise_exception and not no_rollback:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking a little bit, but couldn't you name the function _exceptions_rollback and default to True instead of using a negative and having a double negation here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, done: _exceptions_rollback() is true by default and the double negation is gone.

The context key keeps the negative name (base_exception_no_rollback), so that not setting it means the current behavior.

@gal-adhoc gal-adhoc changed the title [WIP] [18.0][FIX] base_exception: allow detecting exceptions without rollback [18.0][FIX] base_exception: allow detecting exceptions without rollback Sep 29, 2026
@gal-adhoc

Copy link
Copy Markdown
Author

Thanks for the review, and for pointing at #3739 — I had missed it, it was opened a few hours before this one.

The two PRs solve different halves of the problem and do not overlap:

  • [18.0][FIX] base_exception: don't hang when the ongoing transaction locks the rows #3739 fixes the hang itself, for any caller that wrote earlier in the transaction, and still raises BaseExceptionError so the backend popup keeps working. This PR does not fix that: with it applied, confirming an order from the backend after an earlier write on the same row still hangs (main connection idle in transaction, independent one waiting on Lock / transactionid for UPDATE sale_order SET main_exception_id ...). I checked it on an Odoo 18 database before writing this.
  • This PR lets the caller ask for no rollback, which [18.0][FIX] base_exception: don't hang when the ongoing transaction locks the rows #3739 does not cover. In the customer portal the controller writes the signature and flushes it before confirming, so a rollback loses it, and the error reaches a client with no handler for it (the handler is declared in web.assets_backend only, so the portal shows the raw payload). The free order flow of website_sale goes through the same method.

So #3739 alone would turn the portal hang into a lost signature after 2 seconds, and this PR alone leaves the backend hang unfixed. Together they cover both.

They compose cleanly: with the context of this PR set, new_env is self.env, which #3739 writes with directly. They do touch the same lines of detect_exceptions(), so whichever lands second needs a rebase — happy to rebase this one on top of #3739.

The consumer is OCA/sale-workflow#4600, where _validate_order() leaves the blocked orders unconfirmed, keeps the signature and posts an internal note with the exceptions.

@lef-adhoc

Copy link
Copy Markdown
Contributor

@simahawk Hi, can you check this?

Comment on lines +150 to +152
"""Store exceptions in a separate transaction and raise to roll back the
ongoing one. False for the records in context
``base_exception_no_rollback={model: ids}``."""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Store exceptions in a separate transaction and raise to roll back the
ongoing one. False for the records in context
``base_exception_no_rollback={model: ids}``."""
"""Establish if transaction must be rolled back in case of exception.
You can control this by setting a ctx key `base_exception_no_rollback={model: ids}`.
See example in `sale_exception`.
"""

# line) using the current cursor, which sees records created earlier
# in this same transaction even before they are committed.
main_records = self._get_main_records()
rollback = self._exceptions_rollback()

@simahawk simahawk Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rollback = self._exceptions_rollback()
must_rollback = self._must_rollback_on_exception()

)
return all_exception_ids

def _exceptions_rollback(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _exceptions_rollback(self):
def _must_rollback_on_exception(self):

@rousseldenis

Copy link
Copy Markdown
Contributor

@lef-adhoc

@gal-adhoc

Copy link
Copy Markdown
Author

Thanks @simahawk, all three applied: _must_rollback_on_exception(), the must_rollback variable and your docstring. I only wrapped the ctx key line, which was over the line length limit.

OCA/sale-workflow#4600 is updated too, as it calls the method.

@SparkDrago05 SparkDrago05 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on Odoo 18.0 (official image) with this branch. The change works as described, and the existing base_exception tests still pass.

The new context path has no test yet. These two cover it, using the existing fake purchase models:

    @patch_base_exception_method_env
    def test_no_rollback_context(self):
        po = self.po.with_context(
            base_exception_no_rollback={self.po._name: self.po.ids}
        )
        # No BaseExceptionError: the exceptions are stored with the current
        # cursor and returned.
        self.assertEqual(po.detect_exceptions(), self.exception_rule.ids)
        self.assertEqual(self.po.exception_ids, self.exception_rule)

    @patch_base_exception_method_env
    def test_no_rollback_context_other_records(self):
        other_po = self.po.copy()
        po = self.po.with_context(
            base_exception_no_rollback={self.po._name: other_po.ids}
        )
        with self.assertRaises(BaseExceptionError):
            po.detect_exceptions()

With this branch: 0 failed, 0 error(s) of 16 tests. On 18.0 without it, test_no_rollback_context raises BaseExceptionError, as expected.

Side note: the tests above don't use swallow_base_exception_error, because it currently never runs the decorated test body (#3784).

Add _must_rollback_on_exception(), true by default: it is false for the
main records listed in the base_exception_no_rollback context
({model: ids}), and then the exceptions are stored in the ongoing
transaction and detect_exceptions() does not raise.

Callers that already wrote on those records in the transaction cannot
use the second cursor: it waits forever on the row lock held by the
ongoing transaction.
@gal-adhoc

Copy link
Copy Markdown
Author

Thanks @SparkDrago05, both tests added as they are.

I confirmed #3784 independently: with the decorator library, swallow_base_exception_error returns wrapper instead of calling it, so the decorated test body never runs and the test always passes. It currently decorates 16 tests across base_exception, sale_exception and sale_exception_product_sale_manufactured_for, so they are not testing what they look like they test. Happy to review a fix for it.

@simahawk when you have a moment: your three suggestions were applied in the previous push, so this is ready for another look.

@simahawk simahawk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@grindtildeath good for you?

@matiasperalta1

Copy link
Copy Markdown
Contributor

@grindtildeath Hi, Is it ready to merge?

@OCA-git-bot

Copy link
Copy Markdown
Contributor

This PR has the approved label and has been created more than 5 days ago. It should therefore be ready to merge by a maintainer (or a PSC member if the concerned addon has no declared maintainer). 🤖

@hparfr hparfr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ocabot merge major

@OCA-git-bot

Copy link
Copy Markdown
Contributor

On my way to merge this fine PR!
Prepared branch 18.0-ocabot-merge-pr-3741-by-hparfr-bump-major, awaiting test results.

@OCA-git-bot
OCA-git-bot merged commit 8d62a5b into OCA:18.0 Oct 30, 2026
7 checks passed
@OCA-git-bot

Copy link
Copy Markdown
Contributor

Congratulations, your PR was merged at 044425c. Thanks a lot for contributing to OCA. ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants