-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrapping reentrant call in a loop causes deadlock #18
Comments
Additional information: Same is observed running with .net Framework 4.8 and .net Core 6.0 |
This is an interesting bug report, thank you for filing it. Did you run into this in the real world and then reproduce it w/ a modification to the example code? I imagine .NET is starting new background threads to service the requests because too many tasks are being spun up, but this will need a lot of digging (and might not be solvable). |
Yes it was first discovered when looking into a bug in our application |
I have looked into this issue.
It indeed deadlocks in 100% of cases:
The code locks the mutex in one thread and unlocks it in another. This behavior explains why SemaphoreSlim doesn't support reentrancy. It's also the reason C# doesn't allow you to use the async keyword inside the lock statement, as shown below:
There is a very nice discussion on StackOverflow that provides a lot of details why such deadlocks happen. What's more concerning is that even this basic code is expected to fail:
Conclusion
|
I'm not relying on the task being dispatched on the same thread to enable reentrance, though. I'm using the extremely poorly documented The biggest issue is that AsyncLock/AsyncLock/AsyncLock.cs Lines 26 to 32 in 0649d71
|
Sorry, my bad. |
@alekw911 how did you fix this for your app? |
Hello @mitja-p, What is your use case? Why do you need an async reentrant lock? |
Hello! What about this async reentrant lock implementation? .NET Fiddle link |
Ok, so you are asking about the usage of ReentrantAsyncLock.
|
Oh, by the way, I noticed you are a creator of NuExt.System |
Oh cool a different implementation, I had ended up using: https://github.com/microsoft/vs-threading/blob/main/src/Microsoft.VisualStudio.Threading/ReentrantSemaphore.cs |
I have taken the sample from the "AsyncLock usage example" section of the documentation and added a for loop around the inner reentrant call and observed a deadlock occur
This is the sample code I used
The text was updated successfully, but these errors were encountered: