From 0d5cdf3d728698e93ffb1b7e331169bad3aa2fed Mon Sep 17 00:00:00 2001 From: Sam Cook Date: Mon, 4 Apr 2022 17:59:56 +0100 Subject: [PATCH] Fix for locks failing to extend in certain circumstances #97 --- RedLockNet.SERedis/RedLock.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/RedLockNet.SERedis/RedLock.cs b/RedLockNet.SERedis/RedLock.cs index ab3aa1c..dbc5609 100644 --- a/RedLockNet.SERedis/RedLock.cs +++ b/RedLockNet.SERedis/RedLock.cs @@ -366,9 +366,9 @@ private void ExtendLockLifetime() catch (OperationCanceledException) { // unlock has been called, don't extend + logger.LogDebug($"Lock renewal cancelled: {Resource} ({LockId})"); } } - private long GetRemainingValidityTicks(Stopwatch sw) { @@ -414,8 +414,6 @@ private RedLockInstanceSummary Extend() private void Unlock() { - unlockCancellationTokenSource.Cancel(); - // ReSharper disable once MethodSupportsCancellation extendUnlockSemaphore.Wait(); try @@ -430,8 +428,6 @@ private void Unlock() private async Task UnlockAsync() { - unlockCancellationTokenSource.Cancel(); - // ReSharper disable once MethodSupportsCancellation await extendUnlockSemaphore.WaitAsync().ConfigureAwait(false); try @@ -629,6 +625,7 @@ protected virtual void Dispose(bool disposing) StopKeepAliveTimer(); } + unlockCancellationTokenSource.Cancel(); Unlock(); Status = RedLockStatus.Unlocked; @@ -656,6 +653,7 @@ protected virtual async ValueTask DisposeAsync(bool disposing) StopKeepAliveTimer(); } + unlockCancellationTokenSource.Cancel(); await UnlockAsync().ConfigureAwait(false); Status = RedLockStatus.Unlocked;