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 (show other bugs)
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)

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


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.

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