Repository navigation
Conversation
…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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
FXschwartz
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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>()), |
There was a problem hiding this comment.
Same here, I think we can check the code against SqliteErrorCode.invalidSavepointSpecification like the Postgres version does, so the two tests stay in step.
ROLLBACK TO SAVEPOINTundoes the work after a savepoint but keeps the savepoint established. OnlyRELEASE SAVEPOINTends 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 existingtry, so if either fails the caller still gets aRollbackToSavepointFailedException, as before.TransactionManager.rollbackToPreviousSavepointandensureTransactionIsUnlocked(serverpod_test) already pop the savepoint from their own stack. They now also release it in the database, so the two stacks stay aligned.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 inrate_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 theinnerExceptionwording in therunInTransactionOrSavepointdartdoc: it carries the rollback/release error, not the error fromf.Savepoint.rollback()itself is unchanged. Rolling back to the same savepoint twice is valid and already tested insavepoint_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 failingrelease→RollbackToSavepointFailedException; a failingrollback→ norelease.release()fails once: the helper rolls back, releases, and rethrows the release error.packages/serverpod_test/test/transaction_manager_test.dart(new, no database):rollbackToPreviousSavepointandensureTransactionIsUnlockednow roll back and then release.releasePreviousSavepointonly releases. This also fixeswithServerpod'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 bothserverpod_test_serverandserverpod_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 (3B001on 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.
The full
serverpod_test_serverintegration 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_servertest_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
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.Breaking changes
None. On the error path, the helpers issue one extra
RELEASE SAVEPOINTstatement. 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