Skip to content

Commit eff45f6

Browse files
authored
Merge pull request #228 from MartinDembergerR9/master
Added case for rare race conditions on azure lease
2 parents a695066 + 4460986 commit eff45f6

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/DistributedLock.Azure/AzureBlobLeaseDistributedLock.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ static string ConvertToValidName(string name)
105105
);
106106

107107
private async ValueTask<AzureBlobLeaseDistributedLockHandle?> TryAcquireAsync(
108-
BlobLeaseClientWrapper leaseClient,
108+
BlobLeaseClientWrapper leaseClient,
109109
CancellationToken cancellationToken,
110110
bool isRetryAfterCreate)
111111
{
112-
try { await leaseClient.AcquireAsync(this._options.duration, cancellationToken).ConfigureAwait(false); }
112+
try { await leaseClient.AcquireAsync(this._options.duration, cancellationToken).ConfigureAwait(false); }
113113
catch (RequestFailedException acquireException)
114114
{
115115
if (acquireException.ErrorCode == AzureErrors.LeaseAlreadyPresent) { return null; }
@@ -135,6 +135,11 @@ static string ConvertToValidName(string name)
135135
{
136136
// if the retry fails and we created, attempt deletion to clean things up
137137
try { await this._blobClient.DeleteIfExistsAsync().ConfigureAwait(false); }
138+
catch (RequestFailedException deletionException) when (deletionException.ErrorCode == AzureErrors.LeaseIdMissing)
139+
{
140+
// handle the race condition where we try to delete and someone else acquired it
141+
// only the original Exception from TryAcquireAsync should be thrown
142+
}
138143
catch (Exception deletionException)
139144
{
140145
throw new AggregateException(retryException, deletionException);
@@ -160,7 +165,7 @@ internal sealed class InternalHandle : IDistributedSynchronizationHandle, LeaseM
160165
private readonly bool _ownsBlob;
161166
private readonly AzureBlobLeaseDistributedLock _lock;
162167
private readonly LeaseMonitor _leaseMonitor;
163-
168+
164169
public InternalHandle(BlobLeaseClientWrapper leaseClient, bool ownsBlob, AzureBlobLeaseDistributedLock @lock)
165170
{
166171
this._leaseClient = leaseClient;
@@ -193,7 +198,7 @@ public async ValueTask DisposeAsync()
193198
{
194199
await this._lock._blobClient.DeleteIfExistsAsync(leaseId: this._leaseClient.LeaseId).ConfigureAwait(false);
195200
}
196-
else
201+
else
197202
{
198203
await this._leaseClient.ReleaseAsync().ConfigureAwait(false);
199204
}

0 commit comments

Comments
 (0)