SQL distributed lock not releasing on dispose

812 Views Asked by At

I am trying out distributed locks from DistributedLock nuget.

Here's code snippet I use:

class Program
{
    private static SqlConnection _sqlConn = new SqlConnection("my connection string");
   
    static void Main(string[] args)
    {
        int pid;
        using (var p = Process.GetCurrentProcess()) { pid = p.Id; }
        TestSqlLocking(pid).Wait();
    }

    private static async Task TestSqlLocking(int processId)
    {
        _sqlConn.Open();
        var t1 = DoWorkAsyncWithSqlLock($"Process {processId} Client 1");
        var t2 = DoWorkAsyncWithSqlLock($"Process {processId} Client 2");
        var t3 = DoWorkAsyncWithSqlLock($"Process {processId} Client 3");
        var t4 = DoWorkAsyncWithSqlLock($"Process {processId} Client 4");
        await Task.WhenAll(t1, t2, t3, t4);
    }

    private static async Task DoWorkAsyncWithSqlLock(string client)
    {
        var sqlDistributedLock = new SqlDistributedLock("myLock", _sqlConn);
        Console.WriteLine($"SQL Before taking the lock for client [{client}]");
        await using (await sqlDistributedLock.AcquireAsync(TimeSpan.FromSeconds(10)))
        {
            Console.WriteLine($"SQL After taking the lock and doing some work for client [{client}]");
            await Task.Delay(1000);
            Console.WriteLine($"SQL After doing some work and releasing lock for client [{client}]");
        }
    }
}

The thing is I always get timeout after first lock acquiring, as if it never gets released. While it is said that lock will be released after disposing. But that is not hapening. Second task is never able to enter the lock.

Output I get is:

SQL Before taking the lock for client [Process 8864 Client 1]
SQL Before taking the lock for client [Process 8864 Client 2]
SQL Before taking the lock for client [Process 8864 Client 3]
SQL Before taking the lock for client [Process 8864 Client 4]
SQL After taking the lock and doing some work for client [Process 8864 Client 1]
SQL After doing some work and releasing lock for client [Process 8864 Client 1]

Then I get timeout.

0

There are 0 best solutions below