Bug 5804 - Thread.ResetAbort can prevent appdomain unloading
Summary: Thread.ResetAbort can prevent appdomain unloading
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: 5.4 (2017-06)
Hardware: PC Mac OS
: Normal normal
Target Milestone: Future Cycle (TBD)
Assignee: Vlad Brezae
URL:
Depends on:
Blocks:
 
Reported: 2012-06-21 12:05 UTC by Alan McGovern
Modified: 2017-10-19 19:33 UTC (History)
6 users (show)

Tags: bugpool-archive
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:
RESOLVED FIXED

Description Alan McGovern 2012-06-21 12:05:32 UTC
If an appdomain is being unloaded and a thread is running code similar to this, the process will spin using 100% of the cpu and never exit:

while (true) {
    try {
        lock (obj)
            Monitor.Wait (obj);
    } catch (ThreadAbortException) {
        Thread.ResetAbort ();
    }
}

This can be triggered by running any of the 5 tests in the Sharpen test suite in monodevelop. In this scenario, the unit test runner is set to use multiple appdomains when running the test harness.

1) Check out monodevelop at revision 3febc7e542
2) ./configure --enable-tests && make
3) cd main/tests
4) mono ../external/mdtestharness/nunit-console-x86.exe -domain=multiple ../external/ngit/bin/Sharpen.Test.dll
Comment 1 Rodrigo Kumpera 2017-07-12 23:45:17 UTC
Here's a full test case:

using System;
using System.Threading;
using System.Threading.Tasks;

public class Driver : MarshalByRefObject
{
	static object init_obj = new object ();

	static void BadThread () {
		var obj = new object ();
		while (true) {
		    try {
				lock (init_obj) {
					Monitor.Pulse (init_obj);
				}

		        lock (obj)
		            Monitor.Wait (obj);
		    } catch (ThreadAbortException) {
		        Thread.ResetAbort ();
				Console.WriteLine (".");
		    }
		}
	}

	public void Run () {
		Console.WriteLine ("in {0}", AppDomain.CurrentDomain.FriendlyName);
		var t = new Thread (BadThread);
		t.Start ();
		lock (init_obj) {
			Monitor.Wait (init_obj);
			Console.WriteLine ("thread inited, get now go away");
		}
	}

	static void Main () {
		AppDomain domain = AppDomain.CreateDomain ("domain");
		Driver d = (Driver)domain.CreateInstanceAndUnwrap (typeof (Driver).Assembly.FullName, typeof (Driver).FullName);
		d.Run ();
		AppDomain.Unload (domain);
		Console.WriteLine ("done");

	}
}
Comment 2 Ludovic Henry 2017-09-18 22:07:18 UTC
https://github.com/mono/mono/pull/5594