Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

robik
Copy link
Contributor

@robik robik commented Jun 21, 2024

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:

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 21, 2024
@robik robik force-pushed the robik/fix-headless-js-new-arch branch from 3bb9d4a to ac870aa Compare June 21, 2024 13:21
@robik robik changed the title fix: Headless tasks in bridgeless mode (fix #44255) fix: Headless tasks in bridgeless mode (fixes #44255) Jun 21, 2024
@cortinico
Copy link
Contributor

cortinico commented Jul 4, 2024

@robik are you blocked on something? (asking as this is marked as draft)

@robik
Copy link
Contributor Author

robik commented Jul 4, 2024

@cortinico hey, I left a comment on the ticket about this :)

@arushikesarwani94
Copy link
Contributor

These changes look good with regards to backwards compatibility, @robik if you can publish these changes and then #44255 library will be able to test it with the latest nightly

@robik robik marked this pull request as ready for review July 8, 2024 09:31
@robik
Copy link
Contributor Author

robik commented Jul 8, 2024

@arushikesarwani94 allrighty then! PR published :)

@cortinico
Copy link
Contributor

@arushikesarwani94 can you import and merge this one?

@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jul 8, 2024
@facebook-github-bot
Copy link
Contributor

@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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Assertions.assertNotNull(reactHost, "React host is null in newArchitecture");
Assertions.assertNotNull(reactHost, "getReactHost() is null in New Architecture");

Comment on lines +182 to +184
final ReactInstanceManager reactInstanceManager =
getReactNativeHost().getReactInstanceManager();
return reactInstanceManager.getCurrentReactContext();
Copy link
Contributor

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

@facebook-github-bot
Copy link
Contributor

@arushikesarwani94 merged this pull request in 9a1ae97.

Copy link

github-actions bot commented Jul 9, 2024

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?

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Jul 9, 2024
chrfalch added a commit to expo/expo that referenced this pull request Oct 18, 2024
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.
chrfalch added a commit to expo/expo that referenced this pull request Oct 23, 2024
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Emiting Events from HeadlessTask on Android in New Arch + Bridgeless + interop layer
4 participants