CLR hosting exception handling in a non-CLR-created thread
Asked Answered
G

1

18

The issue:

An unhandled exception in a thread entering the CLR from unmanaged code does not trigger the "normal" unhandled exception CLR processing.

In the code below calling CSSimpleObject.GetstringLength() from C++ with

  • "1" throws an exception in the calling thread (non-CLR-created thread),
  • "2" throws an exception in a new Thread() (CLR-created thread).

In case "1"

  • CurrentDomain_UnhandledException() is never called.
  • The Application Domain and the process will stay loaded and running, you will only get a FAILED.

In case "2" (expected behavior)

  • CurrentDomain_UnhandledException() is called.
  • The proces gets killed.

The Question:

What has to be done to get the "normal" behavior?

Sample code:

The code below is based on the Visual Studio 2010 "CppHostCLR" code sample from the "all interop and fusion samples".

RuntimeHost (C++):

PCWSTR pszStaticMethodName = L"GetStringLength";
PCWSTR pszStringArg = L"1";
//PCWSTR pszStringArg = L"2";

hr = pClrRuntimeHost->ExecuteInDefaultAppDomain(pszAssemblyPath,
    pszClassName, pszStaticMethodName, pszStringArg, &dwLengthRet);
if (FAILED(hr))
{
    wprintf(L"Failed to call GetStringLength w/hr 0x%08lx\n", hr);
    goto Cleanup;
}

Managed Code (C#):

public class CSSimpleObject
{
    public CSSimpleObject()
    {
    }
    //------8<----------
    public static int GetStringLength(string str)
    {
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

        switch (str)
        {
            case "1":
                throw new Exception("exception in non-CLR-created thread");
            case "2":
                Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
                thread.Start();
                break;
        }
        return str.Length;

    }
    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Console.WriteLine("CurrentDomain_UnhandledException:" + e.ToString());
        Console.ReadKey();
    }
    public static void WorkThreadFunction()
    {
        throw new Exception(""exception in CLR-created thread"");
    }

Research so far:

MSDN initially implies that unhandled exceptions in a non-CLR-created thread should behave more or less "naturally" - see "Exceptions in Managed Threads"

The common language runtime allows most unhandled exceptions in threads to proceed naturally. In most cases this means that the unhandled exception causes the application to terminate."

"Most" meaning that in CLR-created threads internal, thread abort and Application Domain unloaded exceptions are handled differently. In non-CLR threads

"they proceed normally, resulting in termination of the application."

Further research led me to "Unhandled Exception Processing In The CLR" where I found out the following:

"if the exception was not handled ... in the managed method, the exception would exit the CLR but continue to propagate up the stack as a native SEH exception (managed exceptions are represented as native SEH exceptions) ... The OS unhandled exception filter (UEF) mechanism may not always result in triggering the CLR's unhandled exception processing. Under normal circumstances, this will work as expected and the CLR's unhandled exception processing will be triggered. However, in certain instances this may not happen."

What is wrong with the code above or how can it be changed so that the CLR's unhandled exception processing is triggered?

Update (2011-05-31):

I just found an old bug report, "UnhandledExceptionEventHandler is not called when managed code is called from unmanaged and throws an exception - http://tinyurl.com/44j6gvu", where Microsoft confirms this is a "buggy" behavior:

Thank you for taking the time to report this problem. The behavior is indeed a bug caused by the CLR execution engine and the CRT competing for the UnhandledExceptionFilter. The architecture of the CLR has been revised in the 4.0 version supporting this scenario.

Update (2011-06-06):

Why is it important to get this right?

  • if you are creating a hosting-environment your developers expect a consistent behavior in the exception handling
  • unless there is a way to trigger the "normal CLR exception handling" in a native thread it means you always have to transfer execution to a managed thread (enqueueing in a thread pool for instance)
  • there is still that tiny bit of code transfering excecution from native to managed thread ... that has to catch all exceptions and somehow handle that situation differently

Note: changing the CLR behavior through SetActionOnFailure() makes matters worse, in the sence, that it ends up masking the original exception (i.e. instead of an out of memory you endup seeing threadAborts - without a clue where the original error cam from)!

Gurnard answered 25/5, 2011 at 12:43 Comment(1)
wondering if you were able to find a solution to the problem #2 above, i have run into a similar situationDemeanor
A
2

The update implies that you perhaps have a solution here. However, its solution won't work in all cases, so here is some more info.

If you prefer CLR Unhandled Exception behaviour, then make that the outer-most program and call native code only to perform specific functions. That will ensure that the CLR gets control of the unhandled exception filter.

If you want to retain your current structure and the C++ code you have is small, you could stop using the CRT at all (which will deny you a bunch of useful stuff including static constructors and C++ exception handling). That will ensure that the CLR gets the Unhandled Exception Filter.

And, of course, you could simply call SetUnhandledExceptionFilter youself and get the behaviour you want.

However, I think the best recommendation in this situation is to put an actual function with a catch block on the call stack of any thread where you want to do something when an exception happens and not rely on the UEF mechanism -- because in the context of a component system, it is always fragile as multiple users compete for it.

Martyn

Arsenious answered 4/6, 2011 at 18:21 Comment(4)
Sorry the update was misleading - I just added because of the confirmation of microsoft, that the behavior is a bug. And if you see the original question, what i'm looking for is how to change the code, so that the normal "clr" termination process (with the call to unhandled exception) is triggered. In "Unhandled Exception Processing In The CLR" the writer explains this "wrong" behavior is related to the UEF mechanism and the way the filters are set. So the question is how do i have to set them?Gurnard
We actually have a workaround: 1) try / catch all in the method call by native thread 2) enqueue as action in a workerthreadpool. Only a minimum of code could go wrong and not trigger "normal clr" termination process. The rest of the code is treated "normaly" by the clr".Gurnard
I like your item one. Just don't __try/__catch all in /EHa C++ code.Arsenious
The catch all is in the managed code called by the native thread. In c++ there isn't even a try/catch that is done by the com plumbing where you check failure/success per HRESULT code. The catchall in managed is necessary because there is no other way to get hold of the exception ... unless someone can tell me how :).Gurnard

© 2022 - 2024 — McMap. All rights reserved.