Path: redb-route/redb.Route/Transactions/DependentTransactionBranch.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Transactions/DependentTransactionBranch.cs ---- using System.Transactions; namespace redb.Route.Transactions; /// /// Runs one branch of a parallel fan-out (Splitter / Multicast in parallel mode) under its own /// clone of the ambient transaction. /// /// Parallel branches are dispatched with Task.Run, which flows the caller's /// . Because the route's /// is created with , every branch would /// otherwise observe — and concurrently enlist resources in — the same /// . does not allow concurrent /// use of a single transaction across threads: a second concurrent enlistment promotes to MSDTC or /// throws "transaction context in use by another thread". /// /// /// Each branch instead gets a private via /// . /// makes the parent transaction's commit wait until every branch has signalled completion, so the /// parent never commits work a branch is still writing. Broker transports that defer via /// TRANSACT_ACTION (Kafka/Rabbit/Redis/ASB/AMQP) do not enlist and are unaffected either way; /// this matters for resources that DO enlist (SQL/ADO.NET, redb DB work). /// /// No-op when there is no ambient transaction — the overwhelmingly common, non-transacted case. /// internal static class DependentTransactionBranch { /// /// Executes isolated under a dependent clone of the ambient transaction. /// When no ambient transaction is present the branch runs directly with zero overhead. /// internal static async Task RunAsync(Func branch) { var ambient = Transaction.Current; if (ambient is null) { await branch().ConfigureAwait(false); return; } // Fork a dependent clone so this branch's resource enlistment is private to this thread. var dependent = ambient.DependentClone(DependentCloneOption.BlockCommitUntilComplete); // New ambient for this branch is the dependent; resources opened inside branch() enlist in it. using (var scope = new TransactionScope(dependent, TransactionScopeAsyncFlowOption.Enabled)) { await branch().ConfigureAwait(false); scope.Complete(); } // Signal the dependent is finished so the parent's BlockCommitUntilComplete commit can proceed. // Reached only on the success path: if branch() throws, the scope above disposes without // Complete(), which aborts the (whole) transaction — the parent is then released by the abort, // not by this signal, so there is no deadlock. dependent.Complete(); } }