-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
fix: Headless tasks in bridgeless mode (fixes #44255) #45100
Conversation
3bb9d4a
to
ac870aa
Compare
@robik are you blocked on something? (asking as this is marked as draft) |
@cortinico hey, I left a comment on the ticket about this :) |
packages/react-native/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java
Show resolved
Hide resolved
@arushikesarwani94 allrighty then! PR published :) |
@arushikesarwani94 can you import and merge this one? |
@arushikesarwani94 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
protected ReactContext getReactContext() { | ||
if (DefaultNewArchitectureEntryPoint.getBridgelessEnabled()) { | ||
ReactHost reactHost = getReactHost(); | ||
Assertions.assertNotNull(reactHost, "React host is null in newArchitecture"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assertions.assertNotNull(reactHost, "React host is null in newArchitecture"); | |
Assertions.assertNotNull(reactHost, "getReactHost() is null in New Architecture"); |
final ReactInstanceManager reactInstanceManager = | ||
getReactNativeHost().getReactInstanceManager(); | ||
return reactInstanceManager.getCurrentReactContext(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put this in the else branch pls
@arushikesarwani94 merged this pull request in 9a1ae97. |
This pull request was successfully merged by @robik in 9a1ae97. When will my fix make it into a release? | How to file a pick request? |
On Android/newArch we're having issues with background tasks not working correctly. Background tasks are used in `expo-background-fetch`, `expo-location` and `expo-notifications` and are all driven by the module `expo-task-manager` and its core [TaskService](https://github.com/expo/expo/blob/031a3559dd669ed3edf1800a23eb1d86c52adb39/packages/expo-task-manager/android/src/main/java/expo/modules/taskManager/TaskService.java) class on Android. The `TaskService` class might be started from a background trigger like a push notification being received by the device. In some cases the app is not running, it might be stopped by the user, closed by the system or just not being started after a device reboot. In such a case the service will load the JS bundle in headless mode so that we can run code to make network/api calls, update storage etc. from within the background. This is handled by the [RNHeadlessAppLoader](https://github.com/expo/expo/blob/37ec6ba3c191ae10b315789397b9428da2d48d03/packages/expo-modules-core/android/src/main/java/expo/modules/adapters/react/apploader/RNHeadlessAppLoader.kt) class. On Android/newArch we're seeing some exceptions when trying to run JS code in headless mode: ``` Error: Exception in HostFunction: Could not enqueue microtask because they are disabled in this runtime, js engine: hermes ``` (We might also see other exceptions, but this is something I was reliably seeing). This seems to be caused by how we initialise our RN contexts when running headless. # How I found some similar issues in the RN repo where they updated the [HeadlessJSTaskService](https://github.com/robik/react-native/blob/ac870aa5e6a65f4253c2942aa1d8eaab408d1c90/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java): [#45100](facebook/react-native#45100) This PR applies some of the same patterns for setting up the headless JS system in Expo. # Test plan Run background tests on Android using push-notifications, location and background fetch on both new and old arch.
…re (#32146) # Why On Android/newArch we're having issues with background tasks not working correctly. Background tasks are used in `expo-background-fetch`, `expo-location` and `expo-notifications` and are all driven by the module `expo-task-manager` and its core [TaskService](https://github.com/expo/expo/blob/031a3559dd669ed3edf1800a23eb1d86c52adb39/packages/expo-task-manager/android/src/main/java/expo/modules/taskManager/TaskService.java) class on Android. The `TaskService` class might be started from a background trigger like a push notification being received by the device. In some cases the app is not running, it might be stopped by the user, closed by the system or just not being started after a device reboot. In such a case the service will load the JS bundle in headless mode so that we can run code to make network/api calls, update storage etc. from within the background. This is handled by the [RNHeadlessAppLoader](https://github.com/expo/expo/blob/37ec6ba3c191ae10b315789397b9428da2d48d03/packages/expo-modules-core/android/src/main/java/expo/modules/adapters/react/apploader/RNHeadlessAppLoader.kt) class. On Android/newArch we're seeing some exceptions when trying to run JS code in headless mode: ``` Error: Exception in HostFunction: Could not enqueue microtask because they are disabled in this runtime, js engine: hermes ``` (We might also see other exceptions, but this is something I was reliably seeing). This seems to be caused by how we initialise our RN contexts when running headless. # How I found some similar issues in the RN repo where they updated the [HeadlessJSTaskService](https://github.com/robik/react-native/blob/ac870aa5e6a65f4253c2942aa1d8eaab408d1c90/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java): [#45100](facebook/react-native#45100) This PR applies some of the same patterns for setting up the headless JS system in Expo's `RNHeadlessAppLoader`. # Test plan Run background tests on Android using push-notifications, location and background fetch on both new and old arch. --------- Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com> Co-authored-by: Kudo Chien <kudo@expo.dev>
Summary:
HeadlessTaskService was to create a Bridge instance in bridgeless mode.
Fixes #44255
Changelog:
[ANDROID] [FIXED] - Fixed Headless JS tasks in New Architecture
Test Plan: