Bug 5650 - Unable to start thread during AppDomain.ProcessExit event
Summary: Unable to start thread during AppDomain.ProcessExit event
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-06-14 10:27 UTC by Jeroen Frijters
Modified: 2014-12-01 04:21 UTC (History)
5 users (show)

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:

Description Jeroen Frijters 2012-06-14 10:27:05 UTC
The following program hangs on Mono (and runs as expected on .NET):

using System;
using System.Threading;

class Test
  static void Main()
    AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
    Console.WriteLine("should exit!");

  static void Dummy() {

  static void OnProcessExit(object sender, EventArgs e) {
    Console.WriteLine("starting shutdown hook");
    Thread t = new Thread(Dummy);

While it's arguably not a great idea to start a new thread during exit, this is the mechanism that Java uses for application cleanup handlers and I've implemented it as-is in IKVM and it works on .NET. If there is a good reason not to support this, I'd be happy to add a workaround to IKVM, but maybe it is easy to fix in the Mono runtime.
Comment 1 Zoltan Varga 2012-06-14 17:41:04 UTC
This is probably a mono bug, we run the ProcessExit handler during shutdown, when thread creation is disabled. Its not very easy to fix.
Comment 2 Jeroen Frijters 2012-06-15 02:40:24 UTC
As I said, I'm fine with it not being fixed, but I would like to suggest two changes:
- Thread.Start() should throw an exception under these conditions
- Thread.Join() should return immediately
Comment 3 Rodrigo Kumpera 2012-07-19 13:10:28 UTC
I guess we need to give ProcessExit the same treatment we did with the appdomain unload event and make it run as an independent step of the shutdown process.

But, yeah, it's a non-trivial change to an already convoluted part of the runtime, shutdown.
Comment 4 Zoltan Varga 2014-12-01 04:21:06 UTC
Fixed a long time ago.