Why does a StackOverflowException in an AppDomain child domain terminate the parent AppDomain?

I got the impression that AppDomains are isolated from each other. It seems that with a StackOverException, this is not the case.

To demonstrate this problem, I created a simple console application whose sole purpose is to create a new AppDomain into which I load a very simple assembly and call one of its methods. This method throws a StackOverflowException. This causes my console application to interrupt unceremoniously.

I need the "child" AppDomain to crash and burn such an exception, but leave the console application running in the "parent" AppDomain unscathed.

Is it possible?

UPDATE: here is some kind of code. None of the exception handlers hit.

class Program { static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; // create app domain var domain = AppDomain.CreateDomain("MyDomain"); // create a component var component = (MyComponent)domain.CreateInstanceAndUnwrap( "AppDomainMonitor.Component", typeof(MyComponent).FullName); // create a thread from a method on this component var thread = new Thread(component.CauseStackOverflow); // start the thread thread.Start(); Console.ReadKey(); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { // never hit } } public class MyComponent : MarshalByRefObject { public void CauseStackOverflow() { try { Infinite(); } catch (Exception ex) { // never hit } } void Infinite() { Infinite(); } } 
+8
stack-overflow appdomain
source share
2 answers

Only managed memory is isolated between AppDomains. If a thread throws an exception in any AppDomain application, this will cause the entire application to crash.

I think the best solution is to ensure that all exceptions are handled properly for each thread (or thread pool work items).

However, there is a hack that consists of applying this configuration in the App.Config file:

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <runtime> <legacyUnhandledExceptionPolicy enabled="1"/> </runtime> </configuration> 

Unhandled exceptions will crash the process with .Net 2.0, but you can revert to the old policy using this trick.

I would use this with caution, as it is often best to let processes crash rather than fail. You can add traces to AppDomain.UnhandledException to receive notifications when an unhandled exception occurs and processes them accordingly.

EDIT

You are right about the StackOveflowException, since .Net 2.0 this exception cannot be handled by user code. (See the "Notes" section of this page in msdn ).

There is a way to override this by creating a custom CLR host, but this seems like a crazy thing. I think you have to live with it, or you can create a child process instead of AppDomain if you really need such fault tolerance.

+7
source share

I assume that the problem is that you are calling a method that throws an exception from your main AppDomain, that is, you are calling some method basically AppDomain, this method calls the problem method from a child AppDomain. The exception raises in the child AppDomain, but it propagates up the call stack to your calling AppDomain (the main AppDomain). Try calling this method completely from the child app of AppDomain. For example, create a thread in the AppDomain child domain so that this thread invokes the problem method.

+1
source share

All Articles