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
Status: CONFIRMED
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: Normal normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-02-06 10:33 UTC by Jonathan Pryor
Modified: 2017-09-25 17:46 UTC (History)
5 users (show)

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


Attachments

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 5.4.0.135 (2017-06/6425f06)
Comment 3 Ludovic Henry 2017-09-20 13:58:40 UTC
https://github.com/mono/mono/pull/5606
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...

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