runtime: [API Proposal]: Overriding the default behavior in case of unhandled exceptions and fatal errors.
Re: The previous proposal and a discussion that led to this proposal - (https://github.com/dotnet/runtime/issues/42275)
Background and motivation
The current default behavior in a case of unhandled exception is termination of a process. The current default behavior in a case of a fatal error is print exception to console and invoke Watson/CrashDump.
While satisfactory to the majority of uses, the scheme is not flexible enough for some classes of scenarios.
Scenarios like Designers, REPLs or game scripting that host user provided code are not able to handle unhandled exceptions thrown by the user provided code. Unhandled exceptions on finalizer thread, threadpool threads or user created threads will take down the whole process. This is not desirable experience for these types of scenarios.
In addition, there are customers that have existing infrastructure for postmortem analysis of failures and inclusion of .NET components requires interfacing with or overriding the way the fatal errors are handled.
API Proposal
API for process-wide handling of unhandled exception
namespace System.Runtime.ExceptionServices
{
public delegate bool UnhandledExceptionHandler(System.Exception exception);
public static class ExceptionHandling
{
/// <summary>
/// Sets a handler for unhandled exceptions.
/// </summary>
/// <exception cref="ArgumentNullException">If handler is null</exception>
/// <exception cref="InvalidOperationException">If a handler is already set</exception>
public static void SetUnhandledExceptionHandler(UnhandledExceptionHandler handler);
}
}
The semantics of unhandled exception handler follows the model of imaginary handler like the following inserted in places where the exception will not lead to process termination regardless of what handler() returns.
try { UserCode(); } catch (Exception ex) when handler(ex){};
In particular:
- only exceptions that can be caught and ignored will cause the handler to be invoked. (i.e. stack overflow will not)
- an unhandled exception thrown in a handler will not invoke the handler, but will be treated as returning
false. - when an exception is handled via a handler in a user-started thread, the thread will still exit (but not escalate to process termination)
- when an exception is handled in a task-like scenario on an infrastructure thread (thread pool, finalizer queue…), the execution of tasks will continue. (Whether the infrastructure thread continues or restarted is unspecified, but the process should be able to proceed)
- a reverse pinvoke will not install the try/catch like above.
main()will not install the try/catch like above
API Proposal for custom handling of fatal errors
Managed API to set up the handler.
namespace System.Runtime.ExceptionServices
{
public static class ExceptionHandling
{
/// <summary>
/// .NET runtime is going to call `fatalErrorHandler` set by this method before its own
/// fatal error handling (creating .NET runtime-specific crash dump, etc.).
/// </summary>
/// <exception cref="ArgumentNullException">If fatalErrorHandler is null</exception>
/// <exception cref="InvalidOperationException">If a handler is already set</exception>
public static void SetFatalErrorHandler(delegate* unmanaged<int, void*, int> fatalErrorHandler);
}
}
The shape of the FatalErrorHandler, if implemented in c++ (the default calling convention for the given platform is used)
// expected signature of the handler
FatalErrorHandlerResult FatalErrorHandler(int32_t hresult, struct FatalErrorInfo* data);
With FatalErrorHandlerResult and FatalErrorInfo defined in “FatalErrorHandling.h” under src/native/public:
enum FatalErrorHandlerResult : int32_t
{
RunDefaultHandler = 0,
SkipDefaultHandler = 1,
};
#if defined(_MSC_VER) && defined(_M_IX86)
#define DOTNET_CALLCONV __stdcall
#else
#define DOTNET_CALLCONV
#endif
struct FatalErrorInfo
{
size_t size; // size of the FatalErrorInfo instance
void* address; // code location correlated with the failure (i.e. location where FailFast was called)
// exception/signal information, if available
void* info; // Cast to PEXCEPTION_RECORD on Windows or siginfo_t* on non-Windows.
void* context; // Cast to PCONTEXT on Windows or ucontext_t* on non-Windows.
// An entry point for logging additional information about the crash.
// As runtime finds information suitable for logging, it will invoke pfnLogAction and pass the information in logString.
// The callback may be called multiple times.
// Combined, the logString will contain the same parts as in the console output of the default crash handler.
// The errorLog string will have UTF-8 encoding.
void (DOTNET_CALLCONV *pfnGetFatalErrorLog)(
FatalErrorInfo* errorData,
void (DOTNET_CALLCONV *pfnLogAction)(char8_t* logString, void *userContext),
void* userContext);
// More information can be exposed for querying in the future by adding
// entry points with similar pattern as in pfnGetFatalErrorLog
};
API Usage
Setting up a handler for unhandled exceptions:
using System.Runtime.ExceptionServices;
ExceptionHandling.SetUnhandledExceptionHandler(
(ex) =>
{
if (DesignMode)
{
DisplayException(ex);
// the exception is now "handled"
return true;
}
return false;
}
);
Setting up a handler for fatal errors:
Setting up the handler for the process (C# code in the actual app):
internal unsafe class Program
{
[DllImport("myCustomCrashHandler.dll")]
public static extern delegate* unmanaged<int, void*, int> GetFatalErrorHandler();
static void Main(string[] args)
{
ExceptionHandling.SetFatalErrorHandler(GetFatalErrorHandler());
RunMyProgram();
}
}
The handler. (c++ in myCustomCrashHandler.dll)
#include "FatalErrorHandling.h"
static FatalErrorHandlerResult FatalErrorHandler(int32_t hresult, struct FatalErrorInfo* data)
{
// this is a special handler that analyzes OOM crashes
if (hresult != COR_E_OUTOFMEMORY)
{
return FatalErrorHandlerResult::RunDefaultHandler;
}
DoSomeCustomProcessingOfOOM(data);
// retain the additional error data
data->pfnGetFatalErrorLog(data, &LogErrorMessage, NULL);
// no need for a huge crash dump after an OOM.
return FatalErrorHandlerResult::SkipDefaultHandler;
}
static void LogErrorMessage(char8_t* logString, void *userContext)
{
AppendToBlob(logString);
}
extern "C" DLL_EXPORT void* GetFatalErrorHandler()
{
return &FatalErrorHandler;
}
Alternative Designs
Unmanaged hosting API that enables this behavior. (CoreCLR has undocumented and poorly tested configuration option for this today. #39587. This option is going to be replaced by this API.)
Extending AppDomain.CurrentDomain.UnhandledException API and make IsTerminating property writeable to allow “handling”.
Upon scanning the existing use of this API it was found that IsTerminating is often used as a fact - whether an exception is terminal or not. Changing the behavior to mean “configurable” will be a breaking change to those uses.
Risks
This APIs can be abused to ignore unhandled exceptions or fatal errors in scenarios where it is not warranted.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 26
- Comments: 55 (47 by maintainers)
I hope this will still make it into .NET 10! It would make life much easier when dealing with native libraries and crash handling.
Yes, it is the intention to implement these APIs on all platforms. (On some platforms, the fatal error handler may have limitations imposed by the platform.)
@killaragorn Which one of the two proposed APIs are you interested in?
ExceptionHandling.SetUnhandledExceptionHandlerAPI is implemented and it will be included in .NET 10 (November 2025).ExceptionHandling.SetFatalErrorHandleris work-in-progress and it is likely going to be included in .NET 11 (November 2026).It assumes that the handler wants the fixed information for all types of crashes. It is not necessarily the case.
I am coming back to the callback idea: By default, the crash handler should only get information that does not require any extra memory allocations or substantial effort to compute. Everything else should be provided via callbacks. I think it is the most future proof and flexible design.
If conversion to UTF-8 is going to be needed in majority of scenarios (especially on Unix), I think we should do UTF-8 rather than expect that the end user will convert.
I’ll update the proposal to have
char8_t.It can be ~100 - ~1000 lines depending on how optimized implementation you want. The problem is that there is no broadly available standardized method to do this conversion on non-Windows platforms, so everybody using this API on non-Windows would have to copy the conversion routine from somewhere.
As a (usability) test for this API, you can write a sample handler that produces .json that captures the failure details (similar to .json that is produced by native AOT crash handler today). You will hit the UTF16 conversion problem pretty much immediately.
Calling managed implementation is not an option for the crash handlers.
Same here.
I am ready to have a broader conversation. @jkotas?
I’ve updated the signatures to add the
#ifdef/__stdcall thing and made the context the last.There is nothing else that the thread dealing with the unhandled exception can do until the unhandled exception was dealt with, so there is no advantage in complicating this API with async. If you handler needs to call async methods, it should use Task.Wait.
I am not worried about performance. I am worried about providing the right information to the crash handler, without introducing unnecessary failure points between the failure point and the call of the crash handler.
On one end, you can have crash handlers that just want to capture crashdump for offline processing and do nothing else. Computing the textual stacktrace is source of unnecessary failure points for those.
On the other end, you can have crash handlers that want to capture many details like stacktrace with file and line numbers if possible.
The design should accommodate the whole spectrum. (We do not need to implement everything in the first round, but we should have an idea for how we would go about covering the whole spectrum.)
If a string comes from native assert (like from GC), running managed code might not be an option. I assumed the conversion can be done completely in native code.
I wonder if having the messages etc. as 16 bit strings is the right way. Our coreclr_xxx hosting APIs use 8 bit (UTF-8) strings so that users on Unix don’t have to convert them using some external library. 16 bit strings are very unusual on Unix and on Windows, it is trivial to do the conversion if needed thanks to the Windows APIs.
Defines
int32_t, but the C# function pointer inSetFatalErrorHandler()defineduint. I assume both should be signed.