Skip to content

Commit 2bbfcee

Browse files
Revert unhandled exception logic to prevent multiple calls. (#120103)
This reverts the behavior to .NET Framework - there is now nothing to prevent the handlers from being called multiple times. Co-authored-by: Aaron R Robinson <[email protected]>
1 parent d51f619 commit 2bbfcee

File tree

2 files changed

+10
-40
lines changed
  • src
    • libraries/System.Private.CoreLib/src/System
    • tests/baseservices/exceptions/RaiseAppDomainUnhandledExceptionEvent

2 files changed

+10
-40
lines changed

src/libraries/System.Private.CoreLib/src/System/AppContext.cs

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,50 +82,28 @@ public static void SetData(string name, object? data)
8282
internal static event EventHandler<FirstChanceExceptionEventArgs>? FirstChanceException;
8383
#pragma warning restore CS0067
8484

85-
private static ulong s_crashingThreadId;
86-
8785
#if NATIVEAOT
8886
[System.Runtime.RuntimeExport("OnUnhandledException")]
8987
#endif
9088
internal static void OnUnhandledException(object e)
9189
{
92-
ulong currentThreadId = Thread.CurrentOSThreadId;
93-
ulong previousCrashingThreadId = Interlocked.CompareExchange(ref s_crashingThreadId, currentThreadId, 0);
94-
if (previousCrashingThreadId == 0)
95-
{
9690
#if NATIVEAOT
97-
RuntimeExceptionHelpers.SerializeCrashInfo(System.Runtime.RhFailFastReason.UnhandledException, (e as Exception)?.Message, e as Exception);
91+
RuntimeExceptionHelpers.SerializeCrashInfo(System.Runtime.RhFailFastReason.UnhandledException, (e as Exception)?.Message, e as Exception);
9892
#endif
99-
if (UnhandledException is UnhandledExceptionEventHandler handlers)
93+
if (UnhandledException is UnhandledExceptionEventHandler handlers)
94+
{
95+
UnhandledExceptionEventArgs args = new(e, isTerminating: true);
96+
foreach (UnhandledExceptionEventHandler handler in Delegate.EnumerateInvocationList(handlers))
10097
{
101-
UnhandledExceptionEventArgs args = new(e, isTerminating: true);
102-
foreach (UnhandledExceptionEventHandler handler in Delegate.EnumerateInvocationList(handlers))
98+
try
99+
{
100+
handler(/* AppDomain */ null!, args);
101+
}
102+
catch
103103
{
104-
try
105-
{
106-
handler(/* AppDomain */ null!, args);
107-
}
108-
catch
109-
{
110-
}
111104
}
112105
}
113106
}
114-
else
115-
{
116-
if (s_crashingThreadId == previousCrashingThreadId)
117-
{
118-
Environment.FailFast("OnUnhandledException called recursively");
119-
}
120-
121-
// If we are already in the process of handling an unhandled
122-
// exception, we do not want to raise the event again. We wait
123-
// here while the other thread raises the unhandled exception.
124-
// Waiting is important because it is possible upon returning, this thread
125-
// could call some rude abort method that would terminate the process
126-
// before the other thread finishes raising the unhandled exception.
127-
Thread.Sleep(-1);
128-
}
129107
}
130108

131109
internal static void OnProcessExit()

src/tests/baseservices/exceptions/RaiseAppDomainUnhandledExceptionEvent/RaiseEvent.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
using System;
4-
using System.Runtime.CompilerServices;
54
using System.Runtime.ExceptionServices;
65
using TestLibrary;
76
using Xunit;
@@ -20,14 +19,7 @@ public HandlerRegistration(UnhandledExceptionEventHandler handler)
2019
public void Dispose()
2120
{
2221
AppDomain.CurrentDomain.UnhandledException -= _handler;
23-
24-
// See usage of s_crashingThreadId in the ExceptionHandling class.
25-
// This is to ensure that the static field is reset after the test.
26-
GetCrashingThreadId(null) = 0;
2722
}
28-
29-
[UnsafeAccessor(UnsafeAccessorKind.StaticField, Name = "s_crashingThreadId")]
30-
private static extern ref ulong GetCrashingThreadId([UnsafeAccessorType("System.AppContext")]object? obj);
3123
}
3224

3325
[ThreadStatic]

0 commit comments

Comments
 (0)