react-native-macos/ReactAndroid
Marces Engel dac56ce077 fix: Android headless JS timeout (#33044)
Summary:
Fixes https://github.com/facebook/react-native/issues/33043 and thereby https://github.com/invertase/react-native-firebase/issues/3955.

The issue arised because when there currently is no available React context, `HeadlessJsTaskService` will create a new one in background and start the task using `onReactContextInitialized` of `ReactInstanceManager.addReactInstanceEventListener`.
7ef14af81f/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java (L94-L113)
The `TimingModule` however is initialized asynchronously, meaning the headless JS is started before its initialization. That's an issue because the `TimingModule` is only run when there is JS code executing (meaning if the application is running or there is a headless task running) - this is checked by registering a `HeadlessJsTaskEventListener` on the `HeadlessJsTaskContext` in `TimingModule.initialize()`.
7ef14af81f/ReactAndroid/src/main/java/com/facebook/react/modules/core/TimingModule.java (L69-L75)
However this event listener is never invoked because the task was started before `TimingModule.initialize()` is called -> `TimingModule.onHeadlessJsTaskStart(...)` is not called and the timer never resumes.

In order to fix this we can just invoke `HeadlessJsTaskEventListener.onHeadlessJsTaskStart(...)` for all currently running tasks when a new listener is added to `HeadlessJsTaskContext`. This call then needs to be `synchronized` as otherwise there's a race condition with `HeadlessJsTaskContext.finishTask(...)` where `onHeadlessJsTaskFinish(...)` could be called before `onHeadlessJsTaskStart(...)`. See the diff for the exact changes.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Fix] - Fixed `TimingModule` related functions for headless JS tasks, eg. `setTimeout`

Pull Request resolved: https://github.com/facebook/react-native/pull/33044

Test Plan: I did a local build with the changes and tested the provided example code from https://github.com/facebook/react-native/issues/33043 there.

Reviewed By: sshic

Differential Revision: D34006573

Pulled By: dmitryrykun

fbshipit-source-id: d6a821bbd6476ba278c1d8895edb4a0ba16d889e
2022-04-11 06:58:37 -07:00
..
hermes-engine Improve support for Android users on M1 machine 2022-04-07 10:55:32 -07:00
libs Upgrade Android support library to version 28 in RN 2019-01-22 10:44:53 -08:00
src fix: Android headless JS timeout (#33044) 2022-04-11 06:58:37 -07:00
.npmignore Do not bundle ReactAndroid/.cxx inside the npm package 2022-03-24 05:02:28 -07:00
Android-prebuilt.mk Bump boost for Android to 1.76 to align with iOS + fix (#33565) 2022-04-07 04:50:30 -07:00
DevExperience.md Fix Dead links to documents in the comments (#32619) 2021-11-22 03:31:10 -08:00
README.md docs: Fix dead links in README for rn-tester (#31901) 2021-07-27 11:01:47 -07:00
build.gradle Do not specify a Kotlin version in the RN rootProject (#33589) 2022-04-08 03:44:56 -07:00
gradle.properties Bump boost for Android to 1.76 to align with iOS + fix (#33565) 2022-04-07 04:50:30 -07:00
proguard-rules.pro Cleanup OSS proguard rules and add @DoNotStripAny 2022-02-02 12:54:34 -08:00

README.md

Building React Native for Android

See the docs on the wiki.

Running tests

When you submit a pull request CircleCI will automatically run all tests. To run tests locally, see Testing.