Repository navigation
Fix runTx refusing a call from work its callback started, once that transaction has ended - #500
Merged
isoos merged 2 commits intoNov 21, 2026
Conversation
The reentrancy check marked the connection in the callback's zone and never cleared the mark, so work started inside the callback that called `runTx` after the transaction had ended was refused, although the connection was free again. Mark the transaction session instead, and refuse the call only while the `runTx` that created a session of this connection has not returned. That `runTx` holds the operation lock until then, also after a `rollback()` from the callback.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #500 +/- ##
=======================================
Coverage 86.20% 86.21%
=======================================
Files 36 36
Lines 4068 4092 +24
=======================================
+ Hits 3507 3528 +21
- Misses 561 564 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#468 made
runTxfail fast when it is called from inside its own callback, by marking the connection in the callback's zone. The mark is never cleared, though. Anything started inside the callback keeps the zone (a future, a timer, a stream listener), so if it callsrunTxon the same connection after the transaction has ended, the call is refused although the connection is free again, and that stays so for as long as the zone lives:The same happens through a
Poolwhenever it hands the later call the connection the first transaction used: always withmaxConnectionCount: 1, and also with more connections when that one is idle. We hit it in a test that starts a second piece of work from inside a transaction callback, through Serverpod's pool. Fire-and-forget work started after a write inside a transaction callback fails the same way, depending on which connection the pool picks.This change puts the transaction session in the zone instead of the connection, and refuses the call only while the
runTxthat created one of this connection's sessions has not returned. ThatrunTxholds_operationLockuntil then, also afterrollback()was called from the callback, so the check follows the lock rather than the transaction state.runTxfrom inside its own callback, awaited (also afterrollback())runTxfrom the callback's zone while thatrunTxhas not returnedrunTxfrom the callback's zone after thatrunTxreturnedrunTxfrom outside, while another transaction is activeThe second row is unchanged on purpose: while
runTxholds the lock, a call from its zone cannot be told apart from an awaited reentrant call, which would deadlock. So work started in the callback now gets an error if it callsrunTxbefore the transaction ends and waits if it calls after; before, it always got an error, including hours later from a timer or listener on an idle connection.#493 asks for
executeto wait duringrunTxin the same way; if that is done with a zone check too, it would need the same distinction.Tests in
test/transaction/transaction_test.dart: a call after the transaction ended succeeds (fails on master), a call afterrollback()from inside the callback still fails fast, and a call while the transaction is active still fails fast (both pass on master and guard the existing behaviour). Intest/pool/pool_test.dart: the same late call through aPoolwith a single connection (fails on master).dart analyzeanddart formatare clean; the full suite passes locally exceptText encoders Encode DateTime, which fails the same way on master in a UTC+3 time zone.