Repository navigation
[18.0][FIX] base_exception: don't hang when the ongoing transaction locks the rows - #3739
sergio-teruel wants to merge 2 commits into
Conversation
|
Hi @sebastienbeau, @hparfr, |
… 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.
… 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.
grindtildeath
left a comment
There was a problem hiding this comment.
Looks good but there seems to be an issue while testing, here's my use case
- On a demo DB activate only the exception rule "A product is sold cheaper than his cost. "
- Create a product with a cost higher than its sale price
- Create a sale order with any other product and confirm.
- 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:
| # in this same transaction even before they are committed. | ||
| main_records = self._get_main_records() | ||
|
|
||
| def write_exceptions(env): |
There was a problem hiding this comment.
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?
|
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 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 |
|
Ok, thanks @gal-adhoc I still need to respond to the comment from @grindtildeath. I will try soon.. |
… 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.
… 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.
02e5087 to
398df8e
Compare
… 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.
… 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.
… 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.
… 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.
|
#3739 and #3866 both fix the hang when the second cursor of 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) |
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