microsoft / react-native-windows

A framework for building native Windows apps with React.
https://microsoft.github.io/react-native-windows/
Other
16.15k stars 1.13k forks source link

SEHException when handling promises inside CoreApplication dispatcher in React Native Module #12961

Open anacardix opened 3 months ago

anacardix commented 3 months ago

Problem Description

When handling promises within a React Native Module, specifically using the CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync method to launch an application, a System.Runtime.InteropServices.SEHException labeled "External component has thrown an exception." is thrown. This occurs at the method WriteArgs of JSValueWriter. However, moving the promise resolution outside of the CoreApplication block resolves the issue.

Steps to Reproduce

  1. Implement a React Native Module that handles promises and involves launching an application with Launcher.LaunchFileAsync.
  2. Use the following code snippet where the promise is resolved or rejected inside the CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync block:

    
    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
       CoreDispatcherPriority.Normal, async () =>
       {
           var success = await Launcher.LaunchFileAsync(fileToLaunch);
    
           if (!success)
           {
               promise.Reject(new ReactError { Code = "ERR_LAUNCH_FAILED", Message = "Unable to launch application." });
           }
           else
           {
               promise.Resolve();
           }
       });

Steps To Reproduce

Run the above code

Expected Results

No response

CLI version

12.3.2

Environment

System:
  OS: Windows 10 10.0.19045
  CPU: (4) x64 Intel(R) Xeon(R) Gold 6238 CPU @ 2.10GHz
  Memory: 6.48 GB / 16.00 GB
Binaries:
  Node:
    version: 20.11.1
    path: C:\Program Files\nodejs\node.EXE
  Yarn:
    version: 1.22.21
    path: ~\AppData\Roaming\npm\yarn.CMD
  npm:
    version: 10.4.0
    path: C:\Program Files\nodejs\npm.CMD
  Watchman: Not Found
SDKs:
  Android SDK: Not Found
  Windows SDK:
    AllowDevelopmentWithoutDevLicense: Enabled
    AllowAllTrustedApps: Enabled
    Versions:
      - 10.0.10586.0
      - 10.0.19041.0
      - 10.0.20348.0
      - 10.0.22621.0
IDEs:
  Android Studio: Not Found
  Visual Studio:
    - 16.11.34601.136 (Visual Studio Enterprise 2019)
    - 17.9.34728.123 (Visual Studio Enterprise 2022)
Languages:
  Java: Not Found
  Ruby: Not Found
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.4
    wanted: 0.73.4
  react-native-windows:
    installed: 0.73.5
    wanted: 0.73.5
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: Not found
  newArchEnabled: Not found
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

Target Platform Version

10.0.19041

Target Device(s)

Desktop

Visual Studio Version

Visual Studio 2022

Build Configuration

Debug

Snack, code example, screenshot, or link to a repository

No response

acoates-ms commented 3 months ago

Can you provide a callstack of the exception being thrown in WriteArgs? (Preferably with both Managed+Native frames)

Would surrounding the call to Launcher.LaunchFileAsync in a try/catch. And rejecting the promise in the catch fix the issue? Is it the promise Resolve/Reject thats throwing, or something else in Launcher.LaunchFileAsync?

If the exception is thrown in WriteArgs that generally indicates that you have a function signature mismatch between your JS and your native code.

vmoroz commented 3 months ago

@anacardix, could you create a project that shows the issue? It would be much easier to repro and debug it.

anacardix commented 3 months ago

Can you provide a callstack of the exception being thrown in WriteArgs? (Preferably with both Managed+Native frames)

Would surrounding the call to Launcher.LaunchFileAsync in a try/catch. And rejecting the promise in the catch fix the issue? Is it the promise Resolve/Reject thats throwing, or something else in Launcher.LaunchFileAsync?

If the exception is thrown in WriteArgs that generally indicates that you have a function signature mismatch between your JS and your native code.

Here is the call stack:

Regarding your question about handling exceptions with Launcher.LaunchFileAsync: Wrapping the call to Launcher.LaunchFileAsync in a try/catch block and handling promise rejection in the catch does not resolve the issue, as the exception does not appear to originate from the Launcher.LaunchFileAsync.

chrisglein commented 3 months ago

@anacardix, could you create a project that shows the issue? It would be much easier to repro and debug it.

@anacardix Would it be possible to create a standalone repro of this? As is this is hard for us to evaluate. The overall environment is going to affect what's happening here so we need more context.

anacardix commented 3 months ago

@anacardix, could you create a project that shows the issue? It would be much easier to repro and debug it.

@anacardix Would it be possible to create a standalone repro of this? As is this is hard for us to evaluate. The overall environment is going to affect what's happening here so we need more context.

Sorry for the delay in responding to your request. I had some difficulty initially in reproducing the error in a standalone project. However, I've now managed to replicate the issue and have pushed the relevant code to my repository on GitHub. You can access the repro project at anacardix/promiseExample.