Skip to content
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

Do_Abi_* event/callback handlers should wrap invocations with RoReportUnhandledError #1259

Closed
Scottj1s opened this issue Oct 11, 2022 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@Scottj1s
Copy link
Member

Describe the bug
The legacy C# WinRT projection support for UWP apps wrapped all synchronous calls back into C# code with a try/catch, calling RoOriginateLanguageException and then RoReportUnhandledError on any unhandled exceptions, ensuring they were then routed to the App.UnhandledException handler. This behavior is missing from C#/WinRT projections, causing regressions in Microsoft.Windows.SDK.NET.dll (e.g., Window.Activated) and Microsoft.WinUI.dll (most everything else). Thus, fixes might be required across several repos.

To Reproduce
For an override callback:

  1. Clone WinUI3_UnhandledExceptionTestAppd
  2. Build, set a BP in OnLaunched and in the App.UnhandledException handler, and debug
  3. When the OnLaunched BP hits, set next statement to throw the exception

For an event:

  1. Clone WinUI3_UnhandledExceptionTestAppd
  2. Build, set a BP in the MainWindow's Activated handler and in the App.UnhandledException handler, and debug
  3. When the Activated handler BP hits, set next statement to throw the exception

Expected behavior
In either case above, callback or event, the App.UnhandledException handler should be invoked as it is in a UWP app.

Workarounds
There are a couple workarounds:

  1. User callbacks can be marked 'async' (whether or not they contain any awaits). This causes the CLR to wrap invocations in a try/catch, propagating any caught exceptions through the DispatcherQueueSynchronizationContext.
    this.Activated += **async** (s, e) => { ... };
  2. User callback code can itself be wrapped in a try/catch to propagate errors:
this.Activated += (s, e) => {
  try
  {
    // ...
  }
  catch (System.Exception e)
  {
    ExceptionHelpers.ReportUnhandledError(e);
  }
}

@Scottj1s
Copy link
Member Author

Scottj1s commented Nov 1, 2022

This issue is better addressed lower in the stack, in Microsoft.UI.Xaml.dll. Fixing under original issue: microsoft/microsoft-ui-xaml#5221

@Scottj1s Scottj1s closed this as completed Nov 1, 2022
@gabbybilka
Copy link
Member

🎉Microsoft.WindowsAppSDK.1.2.221209.1 has been released which fixes this issue.

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants