[runtime] Don't terminate application on unhandled AppDomainUnloadedException
[mono.git] / mono / metadata / object.c
index 2ea88c75f8cfde20ecb362453f027e16dbe1225a..9ace3f66652855063c4af180e32252fb48708dc2 100644 (file)
@@ -4587,7 +4587,14 @@ mono_unhandled_exception_checked (MonoObjectHandle exc, MonoError *error)
        MonoObjectHandle current_appdomain_delegate = MONO_HANDLE_NEW (MonoObject, NULL);
 
        MonoClass *klass = mono_handle_class (exc);
-       if (mono_class_has_parent (klass, mono_defaults.threadabortexception_class))
+       /*
+        * AppDomainUnloadedException don't behave like unhandled exceptions unless thrown from 
+        * a thread started in unmanaged world.
+        * https://msdn.microsoft.com/en-us/library/system.appdomainunloadedexception(v=vs.110).aspx#Anchor_6
+        */
+       if (klass == mono_defaults.threadabortexception_class ||
+                       (klass == mono_class_get_appdomain_unloaded_exception_class () &&
+                       mono_thread_info_current ()->runtime_thread))
                return;
 
        field = mono_class_get_field_from_name (mono_defaults.appdomain_class, "UnhandledException");