-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Unhandled exception handler. #109806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unhandled exception handler. #109806
Changes from 9 commits
5c15489
2cbefea
38b3e7a
c66a034
d80cd9c
92bc4dc
8bd32f2
e9aeb09
eff45d1
5b3f13c
4496367
c7833c5
4fd352e
96ef6e2
d1ade60
fa16227
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Threading; | ||
|
|
||
| namespace System.Runtime.ExceptionServices | ||
| { | ||
| public delegate bool UnhandledExceptionHandler(System.Exception exception); | ||
|
|
||
| public static class ExceptionHandling | ||
| { | ||
| internal static UnhandledExceptionHandler? s_handler; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Threading; | ||
|
|
||
| namespace System.Runtime.ExceptionServices | ||
| { | ||
| public delegate bool UnhandledExceptionHandler(System.Exception exception); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @VSadov the approved API shape used
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@VSadov do you have plans to address this? We cannot check in new unapproved public APIs.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened #110254.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes. It was an oversight. |
||
|
|
||
| public static class ExceptionHandling | ||
| { | ||
| internal static UnhandledExceptionHandler? s_handler; | ||
|
|
||
| /// <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) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(handler); | ||
|
|
||
| if (Interlocked.CompareExchange(ref s_handler, handler, null) != null) | ||
| { | ||
| throw new InvalidOperationException(SR.InvalidOperation_CannotRegisterSecondHandler); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| project (ForeignThreadRevPInvokeUnhandledNative) | ||
|
jkotas marked this conversation as resolved.
|
||
|
|
||
| include_directories(${INC_PLATFORM_DIR}) | ||
|
|
||
| if(CLR_CMAKE_HOST_OSX) | ||
| # Enable non-POSIX pthreads APIs, which by default are not included in the pthreads header | ||
| add_definitions(-D_DARWIN_C_SOURCE) | ||
| endif(CLR_CMAKE_HOST_OSX) | ||
|
|
||
| set(SOURCES ForeignThreadRevPInvokeUnhandledNative.cpp) | ||
|
|
||
| if(NOT CLR_CMAKE_HOST_WIN32) | ||
| add_compile_options(-pthread) | ||
| endif() | ||
|
|
||
| # add the executable | ||
| add_library (ForeignThreadRevPInvokeUnhandledNative SHARED ${SOURCES}) | ||
|
|
||
| # add the install targets | ||
| install (TARGETS ForeignThreadRevPInvokeUnhandledNative DESTINATION bin) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Runtime.InteropServices; | ||
| using Xunit; | ||
|
|
||
| public delegate void MyCallback(); | ||
|
|
||
| public class PInvokeRevPInvokeUnhandled | ||
| { | ||
| [DllImport("ForeignThreadRevPInvokeUnhandled")] | ||
| public static extern void InvokeCallback(MyCallback callback); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cannot see this being used in this test
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just copied the file between two tests. One uses |
||
|
|
||
| [DllImport("ForeignThreadRevPInvokeUnhandled")] | ||
| public static extern void InvokeCallbackOnNewThread(MyCallback callback); | ||
|
|
||
| [ThreadStatic] | ||
| private static Exception lastEx; | ||
| private static bool expectUnhandledException = false; | ||
|
|
||
| private static bool Handler(Exception ex) | ||
| { | ||
| lastEx = ex; | ||
| return true; | ||
| } | ||
|
|
||
| private static void SetHandler() | ||
| { | ||
| System.Runtime.ExceptionServices.ExceptionHandling.SetUnhandledExceptionHandler(Handler); | ||
| } | ||
|
|
||
| // test-wide setup | ||
| static PInvokeRevPInvokeUnhandled() | ||
| { | ||
| AppDomain.CurrentDomain.UnhandledException += (_, _) => | ||
| { | ||
| if (expectUnhandledException && | ||
| lastEx == null) | ||
| { | ||
| Environment.Exit(100); | ||
| } | ||
| }; | ||
|
|
||
| SetHandler(); | ||
| } | ||
|
|
||
| public static void RunTest() | ||
| { | ||
| // sanity check, the handler should be working in a separate thread | ||
| Thread th = new Thread(() => | ||
| { | ||
| try | ||
| { | ||
| lastEx = null; | ||
| throw new Exception("here is an unhandled exception1"); | ||
| Assert.Fail(); | ||
| } | ||
| finally | ||
| { | ||
| Assert.Equal("here is an unhandled exception1", lastEx.Message); | ||
| } | ||
| }); | ||
|
|
||
| th.Start(); | ||
| th.Join(); | ||
|
|
||
| expectUnhandledException = true; | ||
| InvokeCallbackOnNewThread(() => { | ||
| try | ||
| { | ||
| lastEx = null; | ||
| throw new Exception("here is an unhandled exception2"); | ||
| Assert.Fail(); | ||
| } | ||
| finally | ||
| { | ||
| Assert.Null(lastEx); | ||
| } | ||
| }); | ||
|
|
||
| Assert.Fail(); | ||
| } | ||
|
|
||
| public static int Main() | ||
| { | ||
| RunTest(); | ||
|
|
||
| // should not reach here | ||
| return 42; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <!-- Needed for CMakeProjectReference --> | ||
| <RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
| <!-- Test requires EH-clean Main --> | ||
| <ReferenceXUnitWrapperGenerator>false</ReferenceXUnitWrapperGenerator> | ||
| <CLRTestPriority>0</CLRTestPriority> | ||
| <MonoAotIncompatible>true</MonoAotIncompatible> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="ForeignThreadRevPInvokeUnhandled.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <CMakeProjectReference Include="CMakeLists.txt" /> | ||
| </ItemGroup> | ||
| </Project> |
Uh oh!
There was an error while loading. Please reload this page.