Skip to content

Conversation

janvorli
Copy link
Member

@janvorli janvorli commented Aug 6, 2025

There are several issues with processing exceptions that are not handled by managed code on Windows:

  • Sometimes the AppDomain.UnhandledException event is sent multiple times
  • Exception flowing to foreign native code is reported as unhandled even when it is actually caught by the native code
  • In rare cases, the unhandled exception stack trace is doubled

This change fixes them by not reporting the unhandled exception in the SfiNext on Windows. It just raises the underlying SEH exception there with the thread marked so that the personality routines for the managed frames won't run the managed exception handling code. If the exception is truly unhandled, the InternalUnhandledExceptionFilter_Worker will be called by the unhandled exception filter installed for the process and report the exception as unhandled.
If that exception ends up being caught by a foreign native code, then nothing will be reported.

Close #115215

There are several issues with processing exceptions that are not handled
by managed code on Windows:
* Sometimes the AppDomain.UnhandledException event is sent multiple
  times
* Exception flowing to foreign native code is reported as unhandled even
  when it is actually caught by the native code
* In rare cases, the unhandled exception stack trace is doubled

This change fixes them by not reporting the unhandled exception in the
SfiNext on Windows. It just raises the underlying SEH exception there
with the thread marked so that the personality routines for the managed
frames won't run the managed exception handling code. If the exception
is truly unhandled, the `InternalUnhandledExceptionFilter_Worker` will be
called by the unhandled exception filter installed for the process and
report the exception as unhandled.
If that exception ends up being caught by a foreign native code, then
nothing will be reported.

Close dotnet#115215
@janvorli janvorli added this to the 10.0.0 milestone Aug 6, 2025
@janvorli janvorli requested a review from jkotas August 6, 2025 15:31
@janvorli janvorli self-assigned this Aug 6, 2025
@Copilot Copilot AI review requested due to automatic review settings August 6, 2025 15:31
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes several issues with unhandled exception reporting on Windows by changing how exceptions are processed when they propagate to native code. The main goal is to prevent duplicate unhandled exception events and incorrect reporting of exceptions that are actually caught by foreign native code.

Key changes:

  • Renamed thread state flag from TSNC_UnhandledException2ndPass to TSNC_SkipManagedPersonalityRoutine to better reflect its purpose
  • Modified exception handling logic to use RaiseException instead of immediately reporting unhandled exceptions in SfiNext on Windows
  • Streamlined the flow so truly unhandled exceptions are reported by the process-level unhandled exception filter

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/coreclr/vm/threads.h Renames thread state flag to better reflect its purpose of skipping managed personality routines
src/coreclr/vm/exceptionhandling.cpp Updates exception handling logic to defer unhandled exception reporting and use RaiseException on Windows

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thanks

@janvorli
Copy link
Member Author

janvorli commented Aug 7, 2025

The unhandled exceptions test is failing, debug build was passing for me locally, I am investigating what's wrong.

@janvorli
Copy link
Member Author

janvorli commented Aug 8, 2025

@jkotas I have fixed the ordering of the UnhandledException event and the calls to finally that was causing the test failure in my last commit.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thank you!

@jkotas jkotas merged commit 54e7818 into dotnet:main Aug 8, 2025
92 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unhandled exception message is printed multiple times to console
2 participants