Bug 46994 - Monitor.Exit throws a SynchronizationLockException after Monitor.Wait is called on another thread
Summary: Monitor.Exit throws a SynchronizationLockException after Monitor.Wait is call...
Status: VERIFIED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: master
Hardware: All All
: Normal normal
Target Milestone: ---
Assignee: Vlad Brezae
URL:
Depends on:
Blocks:
 
Reported: 2016-11-15 15:25 UTC by Josh Peterson
Modified: 2016-12-26 14:06 UTC (History)
6 users (show)

Tags:
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:
Status:
VERIFIED FIXED

Description Josh Peterson 2016-11-15 15:25:57 UTC
This code demonstrates the bug:

using System;
using System.Threading;

class Program
{
	public static void CallMonitorWaitAndCatchException(object dummy)
	{
		try
		{
			Monitor.Wait(dummy);
		}
		catch (SynchronizationLockException)
		{
			Console.WriteLine("A SynchronizationLockException was thrown, as expected.");
			return;
		}

		Console.WriteLine("A SynchronizationLockException was not thrown, which is not expected.");
	}

	public static void Main()
	{
		var dummy = new object();
		Monitor.Enter(dummy);
		var thread = new Thread(()=> CallMonitorWaitAndCatchException(dummy));
		thread.Start();
		thread.Join();
		Console.WriteLine("About to call Monitor.Exit - on Mono this will throw a SynchronizationLockException, on .NET it won't.");
		Monitor.Exit(dummy);
	}
}

The call to Monitor.Exit throws an exception, which is not expected, I think. It should unlock the object without throwing an exception.

Expected output:
A SynchronizationLockException was thrown, as expected.
About to call Monitor.Exit - on Mono this will throw a SynchronizationLockException, on .NET it won't.

Actual output:
A SynchronizationLockException was thrown, as expected.
About to call Monitor.Exit - on Mono this will throw a SynchronizationLockException, on .NET it won't.

Unhandled Exception:
System.Threading.SynchronizationLockException: Object synchronization method was called from an unsynchronized block of code.
  at Program.Main () [0x00049] in <b4cfe210357e4550a41e9918fe27f89f>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.Threading.SynchronizationLockException: Object synchronization method was called from an unsynchronized block of code.
  at Program.Main () [0x00049] in <b4cfe210357e4550a41e9918fe27f89f>:0
Comment 1 Josh Peterson 2016-11-15 15:38:40 UTC
I've just noticed that I see the same behavior if Monitor.Exit(dummy) is called on the thread instead of Monitor.Wait(dummy).
Comment 2 Vlad Brezae 2016-11-15 16:12:27 UTC
https://github.com/mono/mono/pull/3968 should address the issue