madelson / DistributedLock

A .NET library for distributed synchronization
MIT License
1.86k stars 192 forks source link

Timeouts seems not work in DistributedLock.Redis #148

Closed ccccccmd closed 1 year ago

ccccccmd commented 1 year ago

there is no -1 return after 5 seconds but after almost 30 seconds.

  await using var handle =await _lockProvider.TryAcquireLockAsync("MyLockName", TimeSpan.FromSeconds(5));
  if (handle != null)
  {    
      await Task.Delay(TimeSpan.FromSeconds(500));
      return 1;
  }
  else
  {
      return -1;
  }
madelson commented 1 year ago

@ccccccmd thanks for filing. I'm not able to reproduce this behavior; when I run this code snippet it times out after 5s as expected.

Can you please provide a minimal repro that reproduces the behavior?

ccccccmd commented 1 year ago

@ccccccmd thanks for filing. I'm not able to reproduce this behavior; when I run this code snippet it times out after 5s as expected.

Can you please provide a minimal repro that reproduces the behavior?

Thanks for quick reply.

It happened during the second visit to the API.

https://github.com/ccccccmd/WebApplication1

madelson commented 1 year ago

Hi @ccccccmd thanks for putting this together. I downloaded your app and ran your repo. The only change I made was changing the redis connection string to "localhost:6379" to hit my local redis (I'm running 4.0.9 64-bit in WSL).

I first opened one tab, which spun for a long time (made sense because of the 500s wait).

Then I opened another tab and it spun for a bit before printing: image

Refreshing produced the same thing. Oddly, though, while the printed time always shows ~5000ms, the wall-clock time for the request is substantially longer; ~20s.

I tried setting a breakpoint at the start of the MapGet block; and indeed there is a 10-15s delay before each request even enters that block... weird.

Then I ripped out the DistributedLock code and replaced with a simple SemaphoreSlim: same behavior.

Then I tried running the app not in debug mode (CTRL+F5) and the problem goes away.

I've reported this to ASP.NET Core here: https://github.com/dotnet/aspnetcore/issues/44940.

Can you try my repro steps and see if you get the same behavior I do?

ccccccmd commented 1 year ago

Many thanks madelson!

I get the same behavior as you describe in release mode.

It's weird ☺.

madelson commented 1 year ago

Appreciate the confirmation @ccccccmd . I'm closing this issue for now since it doesn't appear to be specific to this library. It will be interesting to see what MSFT says.