Bug 10087 - Process exit can be blocked if the AppDomain.ProcessExit event hangs
Summary: Process exit can be blocked if the AppDomain.ProcessExit event hangs
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: unspecified
Hardware: PC Mac OS
: Normal normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
Depends on:
Reported: 2013-02-06 10:33 UTC by Jonathan Pryor
Modified: 2018-05-26 00:34 UTC (History)
7 users (show)

Tags: bugpool
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 for Bug 10087 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Jonathan Pryor 2013-02-06 10:33:40 UTC
If the AppDomain.ProcessExit event hangs, the process doesn't exit.

  using System;
  using System.Linq;
  using System.Threading;

  class Test {
    public static void Main ()
      AppDomain.CurrentDomain.ProcessExit += (s, e) => {
        while (true) {
          Thread.Sleep (1);
      Environment.Exit (0);

Expected behavior: The process exits after a timeout. This is what happens with .NET, in which the process exits after ~3s.

Actual behavior: The process doesn't exit until forcibly quit (e.g. Ctrl+C).
Comment 1 Rodrigo Kumpera 2017-07-14 23:51:49 UTC
I can repro this.
Comment 2 Ludovic Henry 2017-09-07 13:32:13 UTC
I can reproduce with Mono (2017-06/6425f06)
Comment 3 Ludovic Henry 2017-09-20 13:58:40 UTC
Comment 4 Katelyn Gadd 2017-09-25 17:17:14 UTC
I closed https://github.com/mono/mono/pull/5606 because the approach I went with there was not a feasible solution.

I initially tried using the finalizer thread to run ProcessExit callbacks. While this works for simple scenarios (and gets us an automatic timeout mechanism), it will not behave correctly when multiple appdomains are involved. The finalizer thread's timeout is also much longer than the timeout we want, and there could be interactions with finalizers that aren't desirable.

The correct solution appears to be to spin up 1 or more threads directly and then use them to invoke each ProcessExit callback from within the appropriate domain. A single thread is a reasonable starting point, but it's possible the events need to be run from multiple threads, depending on how much existing applications depend on this event. It's also worth doing a test or two on the MS CLR to determine whether the timeout for ProcessExit is per-domain or is the total amount of time spent by all the events.

When spinning up thread(s) to run the events, it's going to be necessary to make sure everything is retained by the GC - with the finalizer thread approach I ran into trouble with the GC collecting statics out from under me, so it seems like a risk that the same may happen here. I'm still not sure why that happened, since it seems like static variables should still be rooted until later in shutdown...
Comment 5 Rodrigo Kumpera 2017-11-15 20:48:20 UTC
Comment 6 Ludovic Henry 2018-03-09 20:26:39 UTC
It was reverted with https://github.com/mono/mono/pull/7218