Path: redb/redb.Core/Data/DeadlockRetryHelper.cs
Snapshot: redb, 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
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Data/DeadlockRetryHelper.cs
----
namespace redb.Core.Data;
///
/// Retries an async operation on SQL deadlock (MsSql error 1205, Postgres state 40P01).
/// Cluster-safe: each connection independently detects and retries.
///
/// The unit that owns the transaction retries it, and only that unit: the server has already rolled the deadlock
/// victim's transaction back, so a command run again inside it fails with a second error (PostgreSQL 25P02, an aborted
/// transaction on SQL Server) that replaces the deadlock. So the helper never retries inside an ambient
/// , and the overloads taking an never retry
/// when that context is already in a transaction (an explicit one or ExecuteAtomicAsync's) when the operation
/// starts. The original exception then leaves at once, for the owner of the transaction to run its unit again.
///
///
/// The operation is run again as a whole, so it must open its own transaction (or run as single statements) and be
/// safe to repeat: wrap the unit that owns the transaction, for example a whole ExecuteAtomicAsync call made
/// outside any transaction. After the last attempt the deadlock leaves unchanged.
///
///
public static class DeadlockRetryHelper
{
private const int DefaultMaxRetries = 3;
private const int DefaultBaseDelayMs = 50;
public static Task ExecuteWithRetryAsync(Func operation, int maxRetries = DefaultMaxRetries, int baseDelayMs = DefaultBaseDelayMs, System.Threading.CancellationToken cancellationToken = default)
{
return ExecuteWithRetryInternalAsync(operation, owned: true, maxRetries, baseDelayMs, cancellationToken);
}
public static Task ExecuteWithRetryAsync(Func> operation, int maxRetries = DefaultMaxRetries, int baseDelayMs = DefaultBaseDelayMs, System.Threading.CancellationToken cancellationToken = default)
{
return ExecuteWithRetryInternalAsync(operation, owned: true, maxRetries, baseDelayMs, cancellationToken);
}
///
/// Retries on a deadlock unless is in a transaction when it
/// starts: then the transaction belongs to the caller, and the deadlock leaves at once.
///
public static Task ExecuteWithRetryAsync(IRedbContext context, Func operation, int maxRetries = DefaultMaxRetries, int baseDelayMs = DefaultBaseDelayMs, System.Threading.CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(context);
return ExecuteWithRetryInternalAsync(operation, owned: !context.IsInTransaction, maxRetries, baseDelayMs, cancellationToken);
}
///
/// Retries on a deadlock unless is in a transaction when it
/// starts: then the transaction belongs to the caller, and the deadlock leaves at once.
///
public static Task ExecuteWithRetryAsync(IRedbContext context, Func> operation, int maxRetries = DefaultMaxRetries, int baseDelayMs = DefaultBaseDelayMs, System.Threading.CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(context);
return ExecuteWithRetryInternalAsync(operation, owned: !context.IsInTransaction, maxRetries, baseDelayMs, cancellationToken);
}
private static async Task ExecuteWithRetryInternalAsync(Func operation, bool owned, int maxRetries, int baseDelayMs, System.Threading.CancellationToken cancellationToken)
{
for (int attempt = 0; ; attempt++)
{
// Cancellation checks before each attempt and inside the backoff delay (never
// mid-transaction: the operation itself owns its rollback path).
cancellationToken.ThrowIfCancellationRequested();
try
{
await operation();
return;
}
catch (Exception ex) when (owned && attempt < maxRetries && IsDeadlock(ex) && !InAmbientTransaction())
{
var delay = baseDelayMs * (1 << attempt);
var jitter = Random.Shared.Next(delay);
await Task.Delay(delay + jitter, cancellationToken);
}
}
}
private static async Task ExecuteWithRetryInternalAsync(Func> operation, bool owned, int maxRetries, int baseDelayMs, System.Threading.CancellationToken cancellationToken)
{
for (int attempt = 0; ; attempt++)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
return await operation();
}
catch (Exception ex) when (owned && attempt < maxRetries && IsDeadlock(ex) && !InAmbientTransaction())
{
var delay = baseDelayMs * (1 << attempt);
var jitter = Random.Shared.Next(delay);
await Task.Delay(delay + jitter, cancellationToken);
}
}
}
///
/// Deadlock detection lives in with every other code check;
/// this is kept as the name the retry loop and its tests use.
///
internal static bool IsDeadlock(Exception ex) => DbErrorClassifier.IsDeadlock(ex);
// Read only after a deadlock, inside the exception filter: between TransactionScope.Complete() and Dispose() the
// getter throws, and a throwing filter counts as "no match", so the original deadlock still leaves unretried.
private static bool InAmbientTransaction() => System.Transactions.Transaction.Current != null;
}