We are using the AsyncReaderWriterLock to protect cached resources in an MVC4/Framework 4.5 application.
Each request takes a Read Lock, reads what it needs from the cache and then on Dispose (when the request completes as Ninject is maintaining the scope InRequestScope) the lock is released. When the cache needs to be updated, a request is made and the handler
tries to take a Write Lock using the following pattern:
IDisposable t = _lock.WriterLock(CancellationTokenHelpers.Timeout(timeout).Token);
Trace.WriteLine("Enter Writelock Success");
Trace.WriteLine("Enter Writelock Failed");
What is supposed to happen is that the writer request will block until it is able to take the lock, but all other requests will finish (and release their locks) and new read requests will be queued.
However, when the application is under load and there are a number of open read locks, what actually happens is that immediately after calling _lock.WriterLock() a significant number of requests simply hang - they do not complete and their locks are never released.
Once the _lock.WriterLock() is cancelled (by the timeout expiring) the other requests immediately complete and release their read locks. Of course, it is too late by this time and queuing requests waiting for Read Locks are also timing out.
Do you have any idea what might be causing this deadlock? I have stepped through the code and it is the line task.GetAwaiter().GetResult() in TaskExtensions.WaitAndUnwrapException which is hanging.