Skip to content

fix: Release savepoints after rolling back to them in transaction helpers - #6019

Open
yeegz wants to merge 2 commits into
serverpod:mainfrom
yeegz:fix/5999-release-rolled-back-savepoints
Open

yeegz wants to merge 2 commits into
serverpod:mainfrom
yeegz:fix/5999-release-rolled-back-savepoints

Conversation

@yeegz

@yeegz yeegz commented Nov 17, 2026

Copy link
Copy Markdown

ROLLBACK TO SAVEPOINT undoes the work after a savepoint but keeps the savepoint established. Only RELEASE SAVEPOINT ends that nesting level (PostgreSQL docs; SQLite behaves the same). Two helpers rolled back without releasing, so every caught error inside an outer transaction left one extra savepoint level behind until the transaction ended:

  • DatabaseUtil.runInTransactionOrSavepoint (serverpod_database) now releases the savepoint after rolling back on error. Both statements stay inside the existing try, so if either fails the caller still gets a RollbackToSavepointFailedException, as before.
  • TransactionManager.rollbackToPreviousSavepoint and ensureTransactionIsUnlocked (serverpod_test) already pop the savepoint from their own stack. They now also release it in the database, so the two stacks stay aligned.
  • The dartdoc of Savepoint.rollback() now says that the savepoint stays established and should be released when it is no longer needed. The docs page lives in serverpod_docs; I can open a separate PR there if you want one.

The only other Savepoint.rollback() call outside tests is in rate_limiter.dart. It rolls back and then immediately ends its own top-level transaction, so nothing piles up there and I left it alone. I also corrected the innerException wording in the runInTransactionOrSavepoint dartdoc: it carries the rollback/release error, not the error from f. Savepoint.rollback() itself is unchanged. Rolling back to the same savepoint twice is valid and already tested in savepoint_test.dart.

Fixes #5999

Tests

  • packages/serverpod_database/test/util/database_util_test.dart (new, no database): records the savepoint calls. It checks success → release; callback error → rollback, release; a failing release → RollbackToSavepointFailedException; a failing rollback → no release.
  • It also covers the success path where release() fails once: the helper rolls back, releases, and rethrows the release error.
  • packages/serverpod_test/test/transaction_manager_test.dart (new, no database): rollbackToPreviousSavepoint and ensureTransactionIsUnlocked now roll back and then release. releasePreviousSavepoint only releases. This also fixes withServerpod's per-test teardown, which left one savepoint level behind after every test in a group.
  • run_in_transaction_or_savepoint_test.dart (new, in both serverpod_test_server and serverpod_test_sqlite_server): runs the helper on a real transaction with a callback that throws. It then tries to release the helper's savepoint again; that has to fail (3B001 on PostgreSQL, "no such savepoint" on SQLite). Before this change that release succeeded on both backends, which shows the savepoint was still established. The test also checks that the transaction can still be used and that only the later write is committed.

I confirmed that the new tests fail with the library change reverted, and pass with it.

dart test test/util/database_util_test.dart                       # in packages/serverpod_database
SERVERPOD_TEST_TAG= util/run_tests_integration_embedded tests/serverpod_test_server \
  test_integration/database_operations/transactions/run_in_transaction_or_savepoint_test.dart
# SQLite: reset DB + apply migrations as in util/run_tests_sqlite_integration, then
dart test test_integration/database_operations/transactions/run_in_transaction_or_savepoint_test.dart

The full serverpod_test_server integration suite (embedded PostgreSQL, -x redis) was also run with this change; see the results below.

Local results (macOS arm64, Dart 3.14.0, embedded PostgreSQL): serverpod_test_server test_integration (301 files, -x redis): +2213 ~6, all other tests passed. New unit tests: 5/5 (serverpod_database) and 3/3 (serverpod_test). New SQLite test: 1/1. Redis-tagged tests and the e2e/Flutter suites were not run locally.

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

Breaking changes

None. On the error path, the helpers issue one extra RELEASE SAVEPOINT statement. Callers never received the helper's savepoint, so nothing outside could have been relying on it still existing.


I used an AI coding assistant to help write and run this change. I reviewed every line, reproduced the leak against PostgreSQL and SQLite myself, and will handle review comments personally.

🤖 Generated with Claude Code

yeegz and others added 2 commits November 17, 2026 10:27
…pers

ROLLBACK TO SAVEPOINT keeps the savepoint established; only RELEASE ends
that nesting level. DatabaseUtil.runInTransactionOrSavepoint and the
serverpod_test TransactionManager rolled back without releasing, so every
caught error inside an outer transaction left one more savepoint level
behind until the transaction ended.

Release the savepoint after rolling back to it. In DatabaseUtil both steps
stay inside the existing try, so a failure still surfaces as
RollbackToSavepointFailedException. Document on Savepoint.rollback() that
the savepoint stays established.

Fixes serverpod#5999

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@yeegz

yeegz commented Nov 17, 2026

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@FXschwartz FXschwartz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for picking this up @yeegz! I traced the fix through runInTransactionOrSavepoint and the serverpod_test transaction manager and it looks right to me, including the teardown path in withServerpod. Just a few small things:

And yes please on the docs PR you offered for the Transactions page in serverpod_docs. Happy to take another look once these are in.

isA<DatabaseQueryException>().having(
(e) => e.code,
'code',
'3B001', // invalid_savepoint_specification

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could this use PgErrorCode.invalidSavepointSpecification instead of the '3B001' literal? That's what savepoint_test.dart next to it does.

await expectLater(
spy.captured.single.release(),
// SQLite: "no such savepoint".
throwsA(isA<DatabaseQueryException>()),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same here, I think we can check the code against SqliteErrorCode.invalidSavepointSpecification like the Postgres version does, so the two tests stay in step.

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.

A rolled-back savepoint stays established: runInTransactionOrSavepoint and serverpod_test never release it, and the docs do not say it must be released

2 participants