react-native-windows
react-native-windows copied to clipboard
SEHException when handling promises inside CoreApplication dispatcher in React Native Module
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
-
Implement a React Native Module that handles promises and involves launching an application with
Launcher.LaunchFileAsync. -
Use the following code snippet where the promise is resolved or rejected inside the
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsyncblock: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
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.
@anacardix, could you create a project that shows the issue? It would be much easier to repro and debug it.
Can you provide a callstack of the exception being thrown in
WriteArgs? (Preferably with both Managed+Native frames)Would surrounding the call to
Launcher.LaunchFileAsyncin a try/catch. And rejecting the promise in the catch fix the issue? Is it the promise Resolve/Reject thats throwing, or something else inLauncher.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:
- SIRConnector.exe!ReactNativeCitrixLauncher.CitrixLauncher.DownloadAndLaunch.AnonymousMethod__0() at Line 69 (C#)
- Microsoft.ReactNative.Managed.dll!Microsoft.ReactNative.Managed.ReactPromise<Microsoft.ReactNative.Managed.JSValue>.Resolve(Microsoft.ReactNative.Managed.JSValue value) at Line 58 (C#)
- Microsoft.ReactNative.Managed.dll!Microsoft.ReactNative.Managed.JSValueWriter.WriteArgs<Microsoft.ReactNative.Managed.JSValue>(Microsoft.ReactNative.IJSValueWriter writer, Microsoft.ReactNative.Managed.JSValue arg1) at Line 423 (C#)
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.
@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, 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.
@vmoroz Can you take a look at the example repo they provided?
Also, IIRC it's possible that they need to better capture the promise in the C++ code - I think the samples repo shows that you need to capture the promise variable by copying it locally before passing it into a windows async lambda.