Skip to content

Fix runTx refusing a call from work its callback started, once that transaction has ended - #500

Merged
isoos merged 2 commits into
isoos:masterfrom
BiAksoy:fix/runtx-zone-mark-after-transaction
Nov 21, 2026
Merged

isoos merged 2 commits into
isoos:masterfrom
BiAksoy:fix/runtx-zone-mark-after-transaction

Conversation

@BiAksoy

@BiAksoy BiAksoy commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

#468 made runTx fail 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 calls runTx on 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:

final ended = Completer<void>();
late Future<Result> later;
await conn.runTx((tx) async {
  later = ended.future.then((_) => conn.runTx((tx2) => tx2.execute('SELECT 2')));
});
ended.complete();
await later; // PgException: Attempting to call `runTx` on a connection while inside another `runTx` call on the same connection.

The same happens through a Pool whenever it hands the later call the connection the first transaction used: always with maxConnectionCount: 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 runTx that created one of this connection's sessions has not returned. That runTx holds _operationLock until then, also after rollback() was called from the callback, so the check follows the lock rather than the transaction state.

Case 3.5.17 to 3.6.0 This PR
runTx from inside its own callback, awaited (also after rollback()) throws throws
runTx from the callback's zone while that runTx has not returned throws throws
runTx from the callback's zone after that runTx returned throws waits for the connection like any other call
runTx from outside, while another transaction is active waits waits

The second row is unchanged on purpose: while runTx holds 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 calls runTx before 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 execute to wait during runTx in 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 after rollback() 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). In test/pool/pool_test.dart: the same late call through a Pool with a single connection (fails on master). dart analyze and dart format are clean; the full suite passes locally except Text encoders Encode DateTime, which fails the same way on master in a UTC+3 time zone.

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-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.21%. Comparing base (6873feb) to head (2623d03).
⚠️ Report is 3 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@isoos
isoos merged commit 47357e2 into isoos:master Nov 21, 2026
1 check passed
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.

3 participants