Bug 13884 - Deadlock and Performance issue ReaderWriterLockSlim
Summary: Deadlock and Performance issue ReaderWriterLockSlim
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Core ()
Version: master
Hardware: PC All
: --- critical
Target Milestone: Untriaged
Assignee: Jérémie Laval
Depends on:
Reported: 2013-08-09 08:41 UTC by Mario Jungwirth
Modified: 2016-02-16 23:44 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Simple Test-Application (2.70 KB, application/x-zip-compressed)
2013-08-09 08:41 UTC, Mario Jungwirth

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Mario Jungwirth 2013-08-09 08:41:29 UTC
Created attachment 4598 [details]
Simple Test-Application

   If you start my Application (simple counter++ application with 10 threads and readerwriterlockslim)
   the Application hangs immediately (on my pc intel i5 <10 Sec).

   Tested on Linux Ubuntu with Mono 3.2.1, Windows Mono 3.0.10, Windows MS.Net
   On MS.Net all OK.
   On Any Mono/OS the Application hangs immediately.
   Performance (all the same hardware):
   On Windows with MS.Net the counter count every sec. <> 1000000.
   On Windows with Mono (3.0.10) the counter count every seconds < 1000
   On Linux with Mono (trunk 3.2.1) the counter count every seconds <> 100000

   I don't understand the diff.
Comment 1 Mario Jungwirth 2013-08-09 15:22:40 UTC
The same on ReaderWriteLock

I Test it with Monitor.Enter/Exit. No problems.
Comment 2 Mario Jungwirth 2013-08-10 04:48:03 UTC
I have started my application with 5 threads. After 3 Sec the Application hangs. 
I Checked the ReaderWriterLockSlim Properties and see: 

WaitingReaderCount = 41 (only 5 threads no recursion!)
WaitingWriteCount = -2 (negativ!?!)

All Threads are Waiting to Read. (ManualResetEventSlim)
Comment 3 Marek Safar 2013-08-19 04:21:10 UTC
Yeah, the code is very racy. 

Probably the place where is all goes wrong is ExitWriteLock because it does not remove RwWait flag when it should. When I tried to fix the issue another race shows up where we end up in ExitWriteLock without RwWrite flag.

WaitingWriteCount and WaitingReaderCount counters are racy too, so you cannot rely on them.

Jeremie, can you look into it
Comment 4 Miguel de Icaza [MSFT] 2016-02-16 23:44:31 UTC
We have replaced enough code in Mono with Reference Source code that at some point this got fixed.