Bug 5804 - Thread.ResetAbort can prevent appdomain unloading
Summary: Thread.ResetAbort can prevent appdomain unloading
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
Depends on:
Reported: 2012-06-21 12:05 UTC by Alan McGovern
Modified: 2017-10-19 19:33 UTC (History)
6 users (show)

See Also:
Tags: bugpool-archive
Is this bug a regression?: ---
Last known good build:


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

Notice (2018-05-21): bugzilla.xamarin.com will be switching to read-only mode on Thursday, 2018-05-25 22:00 UTC.

Please join us on Visual Studio Developer Community and GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs and copy them to the new locations as needed for follow-up. The See Also field on each Bugzilla bug will be updated with a link to its new location when applicable.

After Bugzilla is read-only, if you have new information to add for a bug that does not yet have a matching issue on Developer Community or GitHub, you can create a follow-up issue in the new location. Copy and paste the title and description from this bug, and then add your new details. You can get a pre-formatted version of the title and description here:

In special cases you might also want the comments:

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.

Note You need to log in before you can comment on or make changes to this bug.