Skip to content

[18.0][FIX] base_exception: don't hang when the ongoing transaction locks the rows - #3739

Open
sergio-teruel wants to merge 2 commits into
OCA:18.0from
Tecnativa:18.0-fix-base_exception-self-lock
Open

sergio-teruel wants to merge 2 commits into
OCA:18.0from
Tecnativa:18.0-fix-base_exception-self-lock

Conversation

@sergio-teruel

Copy link
Copy Markdown
Contributor

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.

cc @Tecnativa TT64644

ping @carlosdauden @carlos-lopez-tecnativa @grindtildeath @hparfr

@OCA-git-bot

Copy link
Copy Markdown
Contributor

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

sergio-teruel added a commit to Tecnativa/sale-workflow that referenced this pull request Sep 24, 2026
… order

Adding a line to a confirmed order so that a line exception rule matches raised `MissingError` instead of the exception popup: the independent connection writing the line exceptions cannot see the uncommitted line. base_exception now writes them in the ongoing transaction in that case (OCA/server-tools#3739).

Adds a regression test for it. The tests of this file are split into one class each: test cursors use REPEATABLE READ and share one transaction per class, so a second test would not see the order committed for it through the independent connection.

@carlosdauden carlosdauden 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.

LGTM

sergio-teruel added a commit to Tecnativa/sale-workflow that referenced this pull request Sep 24, 2026
… order

Adding a line to a confirmed order so that a line exception rule matches raised `MissingError` instead of the exception popup: the independent connection writing the line exceptions cannot see the uncommitted line. base_exception now writes them in the ongoing transaction in that case (OCA/server-tools#3739).

Adds a regression test for it. The tests of this file are split into one class each: test cursors use REPEATABLE READ and share one transaction per class, so a second test would not see the order committed for it through the independent connection.

The tests run post_install, with the full registry as in CI. Their cleanup removes the test rule first and on its own, removes the deliveries of the cancelled order when sale_stock is installed, and archives the product if other modules still reference it, so a failing cleanup cannot leave a committed rule that breaks the following tests.

@carlos-lopez-tecnativa carlos-lopez-tecnativa 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.

Code review

@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.

Looks good but there seems to be an issue while testing, here's my use case

  1. On a demo DB activate only the exception rule "A product is sold cheaper than his cost. "
  2. Create a product with a cost higher than its sale price
  3. Create a sale order with any other product and confirm.
  4. Edit the sale order to add a line with the product whose cost is higher than its sale price.

Somehow the exception got stored, but the line creation was rollbacked:

Image

# in this same transaction even before they are committed.
main_records = self._get_main_records()

def write_exceptions(env):

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.

Can't we avoid having to define this embedded function and use instead a default write function inside _write_exceptions_independently ❓

I'd assume _write_exceptions_independently could take rules_to_add and rules_to_remove as arguments and we could build corresponding args in sale_exception module, but I' may be wrong?

@gal-adhoc

Copy link
Copy Markdown

Heads-up, to avoid duplicated work: I opened #3741 a few hours after this one, for a different half of the problem.

This PR fixes the hang for any caller, and keeps raising BaseExceptionError so the backend popup still works. #3741 lets the caller ask for no rollback, which the customer portal needs: there 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 lives in web.assets_backend only). Consumer: OCA/sale-workflow#4600.

I checked on an Odoo 18 database that #3741 does not fix the hang this PR fixes, so they are complementary rather than competing. Both touch the same lines of detect_exceptions(), so I am happy to rebase mine on top of this one once it lands.

@sergio-teruel

Copy link
Copy Markdown
Contributor Author

Ok, thanks @gal-adhoc I still need to respond to the comment from @grindtildeath. I will try soon..

CarlosRoca13 pushed a commit to Tecnativa/sale-workflow that referenced this pull request Oct 26, 2026
… order

Adding a line to a confirmed order so that a line exception rule matches raised `MissingError` instead of the exception popup: the independent connection writing the line exceptions cannot see the uncommitted line. base_exception now writes them in the ongoing transaction in that case (OCA/server-tools#3739).

Adds a regression test for it. The tests of this file are split into one class each: test cursors use REPEATABLE READ and share one transaction per class, so a second test would not see the order committed for it through the independent connection.

The tests run post_install, with the full registry as in CI. Their cleanup removes the test rule first and on its own, removes the deliveries of the cancelled order when sale_stock is installed, and archives the product if other modules still reference it, so a failing cleanup cannot leave a committed rule that breaks the following tests.
sergio-teruel added a commit to Tecnativa/sale-workflow that referenced this pull request Oct 30, 2026
… order

Adding a line to a confirmed order so that a line exception rule matches raised `MissingError` instead of the exception popup: the independent connection writing the line exceptions cannot see the uncommitted line. base_exception now writes them in the ongoing transaction in that case (OCA/server-tools#3739).

Adds a regression test for it. The tests of this file are split into one class each: test cursors use REPEATABLE READ and share one transaction per class, so a second test would not see the order committed for it through the independent connection.

The tests run post_install, with the full registry as in CI. Their cleanup removes the test rule first and on its own, removes the deliveries of the cancelled order when sale_stock is installed, and archives the product if other modules still reference it, so a failing cleanup cannot leave a committed rule that breaks the following tests.
…he 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.
…oing 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.
@sergio-teruel
sergio-teruel force-pushed the 18.0-fix-base_exception-self-lock branch from 02e5087 to 398df8e Compare October 30, 2026 11:47
sergio-teruel added a commit to Tecnativa/sale-workflow that referenced this pull request Nov 3, 2026
… order

Adding a line to a confirmed order so that a line exception rule matches raised `MissingError` instead of the exception popup: the independent connection writing the line exceptions cannot see the uncommitted line. base_exception now writes them in the ongoing transaction in that case (OCA/server-tools#3739).

Adds a regression test for it. The tests of this file are split into one class each: test cursors use REPEATABLE READ and share one transaction per class, so a second test would not see the order committed for it through the independent connection.

The tests run post_install, with the full registry as in CI. Their cleanup removes the test rule first and on its own, removes the deliveries of the cancelled order when sale_stock is installed, and archives the product if other modules still reference it, so a failing cleanup cannot leave a committed rule that breaks the following tests.
sergio-teruel added a commit to Tecnativa/sale-workflow that referenced this pull request Nov 4, 2026
… order

Adding a line to a confirmed order so that a line exception rule matches raised `MissingError` instead of the exception popup: the independent connection writing the line exceptions cannot see the uncommitted line. base_exception now writes them in the ongoing transaction in that case (OCA/server-tools#3739).

Adds a regression test for it. The tests of this file are split into one class each: test cursors use REPEATABLE READ and share one transaction per class, so a second test would not see the order committed for it through the independent connection.

The tests run post_install, with the full registry as in CI. Their cleanup removes the test rule first and on its own, removes the deliveries of the cancelled order when sale_stock is installed, and archives the product if other modules still reference it, so a failing cleanup cannot leave a committed rule that breaks the following tests.
sergio-teruel added a commit to Tecnativa/sale-workflow that referenced this pull request Nov 16, 2026
… order

Adding a line to a confirmed order so that a line exception rule matches raised `MissingError` instead of the exception popup: the independent connection writing the line exceptions cannot see the uncommitted line. base_exception now writes them in the ongoing transaction in that case (OCA/server-tools#3739).

Adds a regression test for it. The tests of this file are split into one class each: test cursors use REPEATABLE READ and share one transaction per class, so a second test would not see the order committed for it through the independent connection.

The tests run post_install, with the full registry as in CI. Their cleanup removes the test rule first and on its own, removes the deliveries of the cancelled order when sale_stock is installed, and archives the product if other modules still reference it, so a failing cleanup cannot leave a committed rule that breaks the following tests.
sergio-teruel added a commit to Tecnativa/sale-workflow that referenced this pull request Nov 18, 2026
… order

Adding a line to a confirmed order so that a line exception rule matches raised `MissingError` instead of the exception popup: the independent connection writing the line exceptions cannot see the uncommitted line. base_exception now writes them in the ongoing transaction in that case (OCA/server-tools#3739).

Adds a regression test for it. The tests of this file are split into one class each: test cursors use REPEATABLE READ and share one transaction per class, so a second test would not see the order committed for it through the independent connection.

The tests run post_install, with the full registry as in CI. Their cleanup removes the test rule first and on its own, removes the deliveries of the cancelled order when sale_stock is installed, and archives the product if other modules still reference it, so a failing cleanup cannot leave a committed rule that breaks the following tests.
@bosd

bosd commented Nov 24, 2026

Copy link
Copy Markdown

#3739 and #3866 both fix the hang when the second cursor of detect_exceptions() waits on rows locked by the ongoing transaction. Only one of them should land.

Combining the two would work best, I think:

@sergio-teruel @gal-adhoc could you agree on one PR and close the other?

cc @hparfr @sebastienbeau (maintainers)

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.

7 participants