Skip to content

Fix nested multi transaction#18

Draft
doctorcorral wants to merge 2 commits intomainfrom
corral/THBK-950-fix-nested-multi-transaction
Draft

Fix nested multi transaction#18
doctorcorral wants to merge 2 commits intomainfrom
corral/THBK-950-fix-nested-multi-transaction

Conversation

@doctorcorral
Copy link
Copy Markdown

@doctorcorral doctorcorral commented Mar 24, 2026

The *_and_log functions (update_and_log, insert_and_log, etc.) internally create an Ecto.Multi and call repo.transaction(). When these functions are called from within an outer Ecto.Multi.run callback as happens with valiot-app's multiple_function_body for batch mutations like updateMultipleRecipe this creates a nested Multi-inside Multi pattern.

This error path has existed since Ecto 3.0 (PR #2867). The crash surfaced now because the DISABLE_OLD_RECIPES operation triggered the updateMultipleRecipe → Ecto.Multi → update_and_log → nested Ecto.Multi path for the first time in production.

▎ "operation :rollback is rolling back unexpectedly... Nested transactions are discouraged when using
Ecto.Multi. Consider flattening out the transaction instead."

Stack trace confirms the crash at EctoTrail.run_logging_transaction/5 (line 342) called from Ecto.Multi.apply_operation/5.

Fix

Each *_and_log function and log/5 now checks repo.in_transaction?():

  • Already in a transaction: executes the repo operation and audit logging inline — no nested Multi or
    repo.transaction()
  • Standalone (not in a transaction): uses the existing Ecto.Multi + repo.transaction() path (unchanged)

Both paths produce identical return types and audit log behavior.

Fixes: THBK-950

@doctorcorral doctorcorral self-assigned this Mar 24, 2026
@doctorcorral doctorcorral added the bug Something isn't working label Mar 24, 2026
@linear
Copy link
Copy Markdown

linear Bot commented Mar 24, 2026

@doctorcorral doctorcorral marked this pull request as ready for review March 24, 2026 21:56
@doctorcorral doctorcorral changed the title change conditional transaction Fix neste multi transaction Mar 25, 2026
@doctorcorral doctorcorral marked this pull request as draft March 26, 2026 01:07
@doctorcorral doctorcorral changed the title Fix neste multi transaction Fix nested multi transaction Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant