-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Calling JS callback asynchronously from C++ thread. #44813
Comments
|
Please create a repro using the Reproducer Template |
@cortinico I have updated the issue with the required ReproducerApp. |
In
|
@huzhanbo1996 Suppose only one thread in C++ is accessing the JS Runtime then how can we stop JavaScript from using Runtime when C++ is using it? |
In
It depends on what you want to achieve. In code you post. I presume you want to dispatch the task in another thread and return the result by callback in JS Thread. You could try things like serial queue or |
hi @AbeereSpark, to ensure safe calls to JS functions from cpp files during asynchronous operations, you must use an jsCallInvoker. Your application crashes because most likely you are trying to access memory that is already allocated for other needs 1st - get jsCallInvoker from bridge context
for android
in cpp
then you will be able to use callInvoker something like this
I haven't checked this code, maybe you need to play around with pointers, but the general idea is correct |
@Evgen74 I am not sure if it's possible to get |
@AbeereSpark You should be able to achieve something like that by generating C++ module which gets access to |
This seems to be more of a question and @coado kindly replied. |
@coado @cipolleschi Tried to call Synchronously but it crashed and when calling from jsInvoker, the compiler says that the copy constructor is disabled. Please guide if i'm handling it incorrectly.
|
have you tried |
I think you should also be able to use the helper class AsyncCallback, that can be moved and does the See here: For function arguments, just use |
@cipolleschi I implemented this approach, and it is working well. I will proceed as you and @hsjoberg suggested. Thanks for the solution @coado.
|
@cipolleschi @hsjoberg, both of your solutions are working well. @hsjoberg, your solution simplifies things by eliminating the need for manual handling. Thanks to both of you for your great input!
|
Closing as the author is unblocked and the current solution is working fine. |
Description
I am encountering an issue where my React Native app crashes when a JavaScript callback is called from C++ code running on a C++ thread asynchronously. However, when I call the same callback on the same thread from where the foo function is called, it works as expected without crashing.
Steps to reproduce
Nil
React Native Version
0.74.1
Affected Platforms
Runtime - Android, Build - Windows
Areas
TurboModule - The New Native Module System, JSI - Javascript Interface, Bridgeless - The New Initialization Flow
Output of
npx react-native info
Stacktrace or Logs
Reproducer
JSI-App
Screenshots and Videos
App.tsx
C++ module
The text was updated successfully, but these errors were encountered: