Skip to content

fix(client): stop a late nested commit from committing the outer transaction - #31332

Open
benjibunbun wants to merge 1 commit into
prisma:v7from
benjibunbun:fix/nested-transaction-late-commit
Open

benjibunbun wants to merge 1 commit into
prisma:v7from
benjibunbun:fix/nested-transaction-late-commit

Conversation

@benjibunbun

@benjibunbun benjibunbun commented Dec 2, 2026 •

Copy link
Copy Markdown

Linked issue

Refs #31301

This fixes the two cases where the outer callback throws or returns while a nested tx.$transaction is still running. The third case in the issue isn't covered, see the notes below.

Summary

If the outer callback finishes while a nested transaction is still running a query, the outer scope rolls back every level, but it only reset the scope stack after those rollbacks. The first rollback waits for the running query, so the nested callback could finish in between, still see itself as the active scope and send commit, which committed the top-level transaction. The stack is now reset before the rollbacks, so the nested callback rejects instead.

Testing performed

  • Added two PostgreSQL tests to interactive-transactions: the outer callback throws or returns while the nested transaction is running pg_sleep. On v7 both leave the outer write committed, and they pass with this change.
  • pnpm --filter @prisma/client test:functional:code interactive-transactions with --adapter js_pg, --adapter js_better_sqlite3 and --provider postgresql --remote-executor, plus test:functional --types-only for the suite.
  • The other transaction-related functional suites with js_pg and js_better_sqlite3.
  • eslint and prettier on the changed files.

Skill update

n/a — internal only

Checklist

  • All commits are signed off (git commit -s) per the DCO. The DCO status check will block merge if any commit is missing a Signed-off-by: trailer.
  • Tests are updated (or n/a if the change is doc-only / refactor with no behavioural delta).
  • For a bug fix: I searched for the same bug elsewhere and fixed every occurrence, per Fix the cause, everywhere it occurs (or n/a if this is not a bug fix).
  • The Skill update section above is filled in (or stated n/a — internal only).

Notes for the reviewer

  • The nested callback now rejects with Nested transactions must be closed in reverse order of creation., the error a scope already gets once it's no longer on the stack. Before, it sent commit and resolved.
  • The third case (a write through tx while a nested transaction is open, then the nested one fails) isn't fixed. That write runs in the nested level because both share one transaction id, which fix(client): a statement can no longer run after its transaction has closed #30995 kept on purpose. Changing it needs separate ids per level in the transaction manager, so I think it's better as its own change.

Assisted by Claude Code.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of nested interactive transactions when an outer transaction finishes before a nested transaction. The outer transaction now rolls back its changes, and the nested transaction is rejected when transactions do not close in reverse order. This applies whether the outer transaction fails or completes normally.

…saction

If an interactive transaction's callback finished while a nested
transaction was still running a query, the outer scope rolled back every
level but only reset the scope stack after those rollbacks. The first
rollback waits for the running query, so the nested callback could finish
in between, still see itself as the active scope and send a commit, which
committed the top-level transaction. Reset the stack before rolling back.

Signed-off-by: ben <226082140+benjibunbun@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The runtime now clears the nested-scope stack before attempting rollback after a transaction-order violation. Two PostgreSQL tests cover outer transactions settling while a nested transaction is still running.

Changes: Interactive transaction order handling

Layer / File(s) Summary
Clear scopes and validate rollback behavior
packages/client/src/runtime/getPrismaClient.ts, packages/client/tests/functional/interactive-transactions/tests.ts
The runtime clears the scope stack before rollback attempts after an order violation. PostgreSQL tests check rollback and nested transaction rejection when the outer transaction throws or returns while a nested transaction is active.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: hassanabrar202, lazerg

Merge Risk: Minimal · up to 897a0

This change stops a nested transaction that finishes late from committing the outer transaction after the outer callback has returned or thrown. The runtime change is small and has regression tests for both cases. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the fix and its main effect: a late nested commit no longer commits the outer transaction. The raw summary and objectives both describe a scope-stack reset before rollback in the inter…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the fix and its main effect: a late nested commit no longer commits the outer transaction. The raw summary and objectives both describe a scope-stack reset before rollback in the interactive transaction path, which matches the title. The title is 76 characters and uses the conventional fix(client) prefix, so it is concise and specific.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: prisma/orm
  • Branch: v7
  • File: .coderabbit.yml
  • Commit: 92d8025
  • Layers: Repository YAML
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: bf416a6c-aa5a-448f-a7ce-dd48bbe4e791
Commits

Reviewing files that changed from the base of the PR and between 92d8025 and 897a0df.

Files selected for processing: 2

  • packages/client/src/runtime/getPrismaClient.ts
  • packages/client/tests/functional/interactive-transactions/tests.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant