Bug 6635 - ReaderWriterLockSlim.IsWriteLockHeld is unstable just after got lock
Summary: ReaderWriterLockSlim.IsWriteLockHeld is unstable just after got lock
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Core ()
Version: 2.10.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Jérémie Laval
Depends on:
Reported: 2012-08-22 00:09 UTC by tome
Modified: 2012-09-04 13:57 UTC (History)
2 users (show)

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

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 tome 2012-08-22 00:09:44 UTC
It seems that ReaderWriterLockSlim.IsWriteLockHeld property is unstable just after got lock situation.

Please check following test code.
There are some tasks which just tries to get write-lock within race condition.
And each of them does check whether got writer-lock or not (with IsWriteLockHeld) after TryEnterWriteLock () succeeded.
It must be always true but missed sometimes. 
Increasing the number of tasks (10 currently) tend to raise frequency of this issue.
Finally, end of this code show you the total failed count.

public void WriteLockWithSomeTasksTest ()
	ReaderWriterLockSlim rwlockSlim = new ReaderWriterLockSlim (LockRecursionPolicy.NoRecursion);

	List<Task<int>> list = new List<Task<int>> ();
	for (int i = 0; i < 10; ++ i) {
		var task = new Task<int> (() => {
			int nTimeouts = 0;
			int nFailed = 0;

			for (int n = 0; n < 100; ++ n) {
				try {
					if (rwlockSlim.TryEnterWriteLock (100))	{
						Assert.True (rwlockSlim.IsWriteLockHeld);
						Thread.Sleep (10);
						rwlockSlim.ExitWriteLock ();
					else {
						//Assert.False (rwlockSlim.IsWriteLockHeld);
						++ nTimeouts;
				catch (LockRecursionException) {
					++ nFailed;
				catch (AssertionException) {
					++ nFailed;
				finally {
					if (rwlockSlim.IsWriteLockHeld)
						rwlockSlim.ExitWriteLock ();
			return nFailed;
		list.Add (task);

	Task<int>[] tasks = list.ToArray ();
	foreach (var t in tasks) { t.Start (); }

	var sw = Stopwatch.StartNew ();
	Task.WaitAll (tasks);
	sw.Stop ();

	int total = tasks.Aggregate (0, (sum, t) => sum + t.Result);
	Console.WriteLine ("Elapsed = {0:F2}s Total failed={1}", sw.Elapsed.TotalSeconds, total);

	Assert.True (0 == total);
Comment 1 Jérémie Laval 2012-09-04 13:57:22 UTC

Fixed in:
  - master: e223f978bebbc0c10293dc17ff30dff26cda4358
  - mono-2-10: f8f1d6079055158fd306c3bd8447ade379b069fa

Thanks for the report!