-
Notifications
You must be signed in to change notification settings - Fork 24.6k
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
Async JS "stacking up" when MainActivity is in the background #50327
Comments
Warning Could not parse version: We could not find or parse the version number of React Native in your issue report. Please use the template, and report your version including major, minor, and patch numbers - e.g. 0.76.2. |
Warning Could not parse version: We could not find or parse the version number of React Native in your issue report. Please use the template, and report your version including major, minor, and patch numbers - e.g. 0.76.2. |
Hi @descorp thanks for the issue. I highly doubt that the problem is related to the It is more likely that we changed something in the execution model and we "pause" the JS thread if we see that the activity is not active. I need to ask around as I don't have context about it. Perhaps @cortinico or @rubennorte knows more about it. Does it happens on Android only or also on iOS? |
Only on Android. Before you ask, rewriting the native library too use Fragments instead of Activity is not a feasible option 😅
I have noticed similar behaviour on Android when app is dismisses (which make sense). Could be that at some point App's lifecycle was changed to MainActivity lifecycle f? |
It could be. I don't have a lot of details on the Android side, unfortunately. @cortinico can you have a look at this? |
Thanks @cipolleschi
What grinds my gears is the fact, that disabling New Arch does not resolve it for me locally, and yet not stoping some library consumers with latest SDK to use it..
I understand the intent. |
I don't have an answer right now, unfortunately. I think we have to discuss this more thoroughly internally, to understand what will be the right execution model for this use case, and that's not a decision a can make myself. I'll keep this issue updated as soon as I learn more about this. |
Thanks!
Just to emphasise, sync JS code works perfectly on background (you can see that on my reproducer repo). Async code is the problem. I am not a JS core expert, but my assumption is that something is off with the way how |
i remember seeing something similar when there was an effort to make headlessJsTaskService to be newarch compatible. this PR ended up solving it. i dunno how practical it is to mess with the new timer manager. perhaps adding a persisting headlessJsTaskService will ensure the timer not getting paused I've noticed setState also "stacks up" when MainAcitivty is backgrounded with new arch. I have no real solution other than pausing it all together. |
Thanks for the tip @lovegaoshi As far as I understand, problem is the Again, what grinds my gears is the fact, that setting |
rather than a limitation, I see all of this working as intended - jstimer's onHostPause is functioning as expected and paused all async execution I'm presuming, unless there are active headlessJsTasks. sync js code executes just fine. I dont see this is a new vs old arch thing either. perhaps you can find something by diffing your working test app with a RN template at the time, it shouldnt work to begin with unfortunately i dont have any experience on this without involving headlessJsTaskService. the other pointer I have is this seemingly setState "stacking" issue only with the new arch, but since u mentioned neither arch work, i doubt this is a real lead. |
Okay, I've investigated further and believe I've identified the underlying causes: Following behaviour reproduced on blank new 0.78.2 app:
This observation aligns with why the majority of users haven't experienced issues (likely using older RN versions or configurations) and why switching to the "old architecture" resolves the problem for those who do. @cipolleschi does this behavior align with the React Native community's vision for background task execution, or should either of these points be considered for future improvement or clarification? |
I really have to defer this to @cortinico as he has more experience on Android than me. |
Description
Hi everyone,
I'm encountering a really strange issue and hoping someone here might have some insight.
Here's the problem: In any new React Native app generated using @react-native-community/cli, when a secondary Activity is presented (and the main Activity is in the background), asynchronous JavaScript code seems to stop functioning correctly. It appears to "stack up" and only executes after the secondary Activity is dismissed.
For context, I'm developing a React Native package that has worked without this issue for the past two years. My local test app also works fine, and many users are using it successfully on modern React Native versions (I know of at least one on 0.76.7). This issue seems specific to freshly created apps.
I've created a minimal repo to reproduce the problem: https://github.com/descorp/RNAlertTest.
Interestingly, one user reported that turning off the "new Architecture" resolved this "thread freezing" problem for them, but I haven't been able to reproduce this fix locally.
My suspicion is that something has changed in the project generation process with
@react-native-community/cli
compared to the oldernpx react-native init
, but I'm not sure what it could be.Has anyone else experienced anything similar or have any ideas on what might be causing this? Any help would be greatly appreciated!
Steps to reproduce
yarn && yarn android
Abstract:
await sleep(100)
orfetch('www.google.com')
)React Native Version
0.78.2
Affected Platforms
Runtime - Android
Output of
npx @react-native-community/cli info
Stacktrace or Logs
Reproducer
https://github.com/descorp/RNAlertTest
Screenshots and Videos
The text was updated successfully, but these errors were encountered: