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

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