Skip to content

Commit 171d3a0

Browse files
committed
handled table creation with double check pattern to prevent race condition in the fallback case
1 parent 3bee61a commit 171d3a0

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

CoreHelpers.WindowsAzure.Storage.Table/Extensions/TableClientExtensions.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,34 @@ public static async Task<Response<IReadOnlyList<Response>>> SubmitTransactionWit
4949
// check the exception
5050
if (allowAutoCreate && ex.ErrorCode.Equals("TableNotFound"))
5151
{
52-
// try to create the table
53-
await tc.CreateAsync();
52+
53+
// This is a double check pattern to ensure that two independent processes
54+
// who are trying to create the table in parallel do not end up in an unhandled
55+
// situation.
56+
try
57+
{
58+
// try to create the table
59+
await tc.CreateAsync();
60+
}
61+
catch (TableTransactionFailedException doubleCheckEx)
62+
{
63+
// check if we have an errorCode if not the system throws the exception
64+
// to the caller
65+
if (String.IsNullOrEmpty(doubleCheckEx.ErrorCode))
66+
{
67+
ExceptionDispatchInfo.Capture(ex).Throw();
68+
return null;
69+
}
70+
71+
// Every error except the TableAlreadyExists is thrown to the caller but
72+
// in the case the system is trying to create the table in parallel we
73+
// ignore the error and execute the transaction!
74+
if (!doubleCheckEx.ErrorCode.Equals("TableAlreadyExists"))
75+
{
76+
ExceptionDispatchInfo.Capture(ex).Throw();
77+
return null;
78+
}
79+
}
5480

5581
// retry
5682
return await tc.SubmitTransactionAsync(transactionActions, cancellationToken);

0 commit comments

Comments
 (0)