Skip to content

Commit

Permalink
handled table creation with double check pattern to prevent race cond…
Browse files Browse the repository at this point in the history
…ition in the fallback case
  • Loading branch information
dei79 committed Jan 7, 2024
1 parent 3bee61a commit 171d3a0
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,34 @@ public static async Task<Response<IReadOnlyList<Response>>> SubmitTransactionWit
// check the exception
if (allowAutoCreate && ex.ErrorCode.Equals("TableNotFound"))
{
// try to create the table
await tc.CreateAsync();

// This is a double check pattern to ensure that two independent processes
// who are trying to create the table in parallel do not end up in an unhandled
// situation.
try
{
// try to create the table
await tc.CreateAsync();
}
catch (TableTransactionFailedException doubleCheckEx)
{
// check if we have an errorCode if not the system throws the exception
// to the caller
if (String.IsNullOrEmpty(doubleCheckEx.ErrorCode))
{
ExceptionDispatchInfo.Capture(ex).Throw();
return null;
}

// Every error except the TableAlreadyExists is thrown to the caller but
// in the case the system is trying to create the table in parallel we
// ignore the error and execute the transaction!
if (!doubleCheckEx.ErrorCode.Equals("TableAlreadyExists"))
{
ExceptionDispatchInfo.Capture(ex).Throw();
return null;
}
}

// retry
return await tc.SubmitTransactionAsync(transactionActions, cancellationToken);
Expand Down

0 comments on commit 171d3a0

Please sign in to comment.