Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Threading;
Expand Down Expand Up @@ -318,7 +319,16 @@ private static unsafe uint RunFinalizers()
void* fptr = GetNextFinalizeableObject(ObjectHandleOnStack.Create(ref target));
if (fptr == null)
break;
((delegate*<object, void>)fptr)(target!);

try
{
((delegate*<object, void>)fptr)(target!);
}
catch (Exception ex) when (ExceptionHandling.s_handler?.Invoke(ex) == true)
{
// the handler returned "true" means the exception is now "handled" and we should continue.
}

currentThread.ResetFinalizerThread();
count++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;

//
Expand Down Expand Up @@ -63,10 +64,17 @@ private static unsafe uint DrainQueue()

finalizerCount++;

// Call the finalizer on the current target object. If the finalizer throws we'll fail
// fast via normal Redhawk exception semantics (since we don't attempt to catch
// anything).
((delegate*<object, void>)target.GetMethodTable()->FinalizerCode)(target);
try
{
// Call the finalizer on the current target object.
((delegate*<object, void>)target.GetMethodTable()->FinalizerCode)(target);
}
// We do not use "?." operator here like in other places.
// It would cause "Predefined type 'System.Nullable`1' is not defined" errors.
catch (Exception ex) when (ExceptionHandling.s_handler != null && ExceptionHandling.s_handler(ex))
{
// the handler returned "true" means the exception is now "handled" and we should continue.
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Globalization;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

Expand Down Expand Up @@ -458,6 +459,10 @@ private static void StartThread(IntPtr parameter)

startHelper.Run();
}
catch (Exception ex) when (ExceptionHandling.s_handler?.Invoke(ex) == true)
{
// the handler returned "true" means the exception is now "handled" and we should gracefully exit.
}
finally
{
thread.SetThreadStateBit(ThreadState.Stopped);
Expand Down
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
Expand Up @@ -220,6 +220,7 @@
<Compile Include="System\Runtime\CompilerServices\ClassConstructorRunner.cs" />
<Compile Include="System\Runtime\CompilerServices\InlineArrayAttribute.cs" />
<Compile Include="System\Runtime\CompilerServices\StaticClassConstructionContext.cs" />
<Compile Include="System\Runtime\ExceptionServices\ExceptionHandling.cs" />
<Compile Include="System\Runtime\InteropServices\InAttribute.cs" />
<Compile Include="System\Diagnostics\DebuggerStepThroughAttribute.cs" />
<Compile Include="System\Diagnostics\StackTraceHiddenAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,9 @@
<data name="InvalidOperation_CannotRegisterSecondResolver" xml:space="preserve">
<value>A resolver is already set for the assembly.</value>
</data>
<data name="InvalidOperation_CannotRegisterSecondHandler" xml:space="preserve">
<value>A handler for unhandled exceptions is already set.</value>
</data>
<data name="InvalidOperation_CannotRestoreUnsuppressedFlow" xml:space="preserve">
<value>Cannot restore context flow when it is not suppressed.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\ConstrainedExecution\PrePrepareMethodAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\ConstrainedExecution\ReliabilityContractAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\ExceptionServices\ExceptionDispatchInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\ExceptionServices\ExceptionHandling.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\ExceptionServices\FirstChanceExceptionEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\ExceptionServices\HandleProcessCorruptedStateExceptionsAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\GCFrameRegistration.cs" Condition="'$(FeatureMono)' != 'true'" />
Expand Down
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);
Copy link
Contributor

@teo-tsirpanis teo-tsirpanis Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VSadov the approved API shape used Func<Exception, bool> instead of a custom delegate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VSadov the approved API shape used Func<Exception, bool> instead of a custom delegate.

@VSadov do you have plans to address this? We cannot check in new unapproved public APIs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #110254.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you have plans to address this? We cannot check in new unapproved public APIs.

Yes. It was an oversight.
The approved shape was the same as in proposal except for this part. I missed that there was a diff from the proposal.


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
Expand Up @@ -8,6 +8,7 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Runtime.ExceptionServices;
using System.Runtime.Versioning;
using System.Security.Principal;

Expand Down Expand Up @@ -69,18 +70,25 @@ private void RunWorker()
AutoreleasePool.CreateAutoreleasePool();
#endif

if (start is ThreadStart threadStart)
try
{
threadStart();
}
else
{
ParameterizedThreadStart parameterizedThreadStart = (ParameterizedThreadStart)start;
if (start is ThreadStart threadStart)
{
threadStart();
}
else
{
ParameterizedThreadStart parameterizedThreadStart = (ParameterizedThreadStart)start;

object? startArg = _startArg;
_startArg = null;
object? startArg = _startArg;
_startArg = null;

parameterizedThreadStart(startArg);
parameterizedThreadStart(startArg);
}
}
catch (Exception ex) when (ExceptionHandling.s_handler?.Invoke(ex) == true)
{
// the handler returned "true" means the exception is now "handled" and we should gracefully exit.
}

#if FEATURE_OBJCMARSHAL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Threading.Tasks;
Expand Down Expand Up @@ -1187,12 +1188,21 @@ private static void DispatchWorkItem(object workItem, Thread currentThread)
{
if (workItem is Task task)
{
// Task workitems catch their exceptions for later observation
// We do not need to pass unhandled ones to ExceptionHandling.s_handler
task.ExecuteFromThreadPool(currentThread);
}
else
{
Debug.Assert(workItem is IThreadPoolWorkItem);
Unsafe.As<IThreadPoolWorkItem>(workItem).Execute();
try
{
Unsafe.As<IThreadPoolWorkItem>(workItem).Execute();
}
catch (Exception ex) when (ExceptionHandling.s_handler?.Invoke(ex) == true)
{
// the handler returned "true" means the exception is now "handled" and we should continue.
}
}
}
}
Expand Down
Loading