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

[0.74.0] AppState module has an error on android #44251

Open
NiuGuohui opened this issue Apr 25, 2024 · 11 comments
Open

[0.74.0] AppState module has an error on android #44251

NiuGuohui opened this issue Apr 25, 2024 · 11 comments
Labels
API: AppState Needs: Attention Issues where the author has responded to feedback. Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Needs: Triage 🔍 Platform: Android Android applications. Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)

Comments

@NiuGuohui
Copy link

NiuGuohui commented Apr 25, 2024

Description

After I migrated to 0.74, I found that Logcat was reporting an error:
Unhandled SoftException com. Facebook. react. bridge ReactNoCrashSoftException: Tried to access onWindowFocusChange while context is not ready

The AppState.addEventListener fn not triggered

Steps to reproduce

  1. index.js
AppState.addEventListener('change', state => {
  console.log(state);
});
  1. run app (android)
  2. make app to the background

React Native Version

0.74.0

Affected Platforms

Runtime - Android, Build - MacOS

Areas

JSI - Javascript Interface, Bridgeless - The New Initialization Flow

Output of npx react-native info

System:
  OS: macOS 14.3.1
  CPU: (10) arm64 Apple M1 Max
  Memory: 1.39 GB / 64.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.19.0
    path: ~/.nvm/versions/node/v18.19.0/bin/node
  Yarn:
    version: 1.22.22
    path: ~/.nvm/versions/node/v18.19.0/bin/yarn
  npm:
    version: 10.2.3
    path: ~/.nvm/versions/node/v18.19.0/bin/npm
  Watchman:
    version: 2021.11.15.00
    path: /usr/local/bin/watchman
Managers:
  CocoaPods:
    version: 1.10.1
    path: /usr/local/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 21.4
      - iOS 16.0
      - macOS 12.3
      - tvOS 16.0
      - watchOS 9.0
  Android SDK:
    Android NDK: 22.1.7171670
IDEs:
  Android Studio: 2023.2 AI-232.10300.40.2321.11567975
  Xcode:
    version: 14.0.1/14A400
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.1
    path: /usr/bin/javac
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.74.0
    wanted: ^0.74.0
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found


(Because I am using PNPM, it shows none `Not found`)

Stacktrace or Logs

ReactHost{0}.onWindowFocusChange(hasFocus = "false")
Unhandled SoftException
  com.facebook.react.bridge.ReactNoCrashSoftException: Tried to access onWindowFocusChange while context is not ready
  at com.facebook.react.runtime.ReactHostImpl.onWindowFocusChange(ReactHostImpl.java:664)
  at com.facebook.react.ReactDelegate.onWindowFocusChanged(ReactDelegate.java:193)
  at com.facebook.react.ReactActivityDelegate.onWindowFocusChanged(ReactActivityDelegate.java:175)
  at com.facebook.react.ReactActivity.onWindowFocusChanged(ReactActivity.java:128)
  at androidx.appcompat.view.WindowCallbackWrapper.onWindowFocusChanged(WindowCallbackWrapper.java:125)
  at com.android.internal.policy.DecorView.onWindowFocusChanged(DecorView.java:2422)
  at android.view.View.dispatchWindowFocusChanged(View.java:15279)
  at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1566)
  at android.view.ViewRootImpl.handleWindowFocusChanged(ViewRootImpl.java:4981)
  at android.view.ViewRootImpl.access$1400(ViewRootImpl.java:270)
  at android.view.ViewRootImpl$ViewRootHandler.handleMessageImpl(ViewRootImpl.java:7115)
  at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:7009)
  at android.os.Handler.dispatchMessage(Handler.java:117)
  at android.os.Looper.loopOnce(Looper.java:205)
  at android.os.Looper.loop(Looper.java:293)
  at android.app.ActivityThread.loopProcess(ActivityThread.java:9961)
  at android.app.ActivityThread.main(ActivityThread.java:9950)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:586)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1240)

Reproducer

none

Screenshots and Videos

No response

@NiuGuohui NiuGuohui added Needs: Triage 🔍 Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules) labels Apr 25, 2024
@github-actions github-actions bot added API: AppState Platform: Android Android applications. Needs: Author Feedback Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. labels Apr 25, 2024
Copy link

⚠️ Missing Reproducible Example
ℹ️ We could not detect a reproducible example in your issue report. Please provide either:
  • If your bug is UI related: a Snack
  • If your bug is build/update related: use our Reproducer Template. A reproducer needs to be in a GitHub repository under your username.

@cortinico
Copy link
Contributor

@NiuGuohui Just to confirm, you're on New Architecture right?

@NiuGuohui
Copy link
Author

@NiuGuohui Just to confirm, you're on New Architecture right?

I tried again. Now AppState seems to be working properly. But I found that this error still occurs when enable bridgeless, and after disabled bridgeless, it no longer reports an error.

@github-actions github-actions bot added Needs: Attention Issues where the author has responded to feedback. and removed Needs: Author Feedback labels Apr 25, 2024
@alanjhughes
Copy link
Collaborator

@cortinico can confirm that I have run into this. I meant to reach out about it but I wanted to investigate myself first as it doesn't repro reliably and I'm not sure of the cause

@cortinico
Copy link
Contributor

@cortinico can confirm that I have run into this. I meant to reach out about it but I wanted to investigate myself first as it doesn't repro reliably and I'm not sure of the cause

Great thanks for confirming it 👍

@cortinico
Copy link
Contributor

@alanjhughes @NiuGuohui can we isolate what's the problem here?

I've tried to reproduce, and yes, the:

Unhandled SoftException
  com.facebook.react.bridge.ReactNoCrashSoftException: Tried to access onWindowFocusChange while context is not ready

gets fired but that's unrelated. I was able to successfully use the AppState API with the provided reproducer.

repro.mov

@NiuGuohui
Copy link
Author

@alanjhughes @NiuGuohui can we isolate what's the problem here?

I've tried to reproduce, and yes, the:

Unhandled SoftException
  com.facebook.react.bridge.ReactNoCrashSoftException: Tried to access onWindowFocusChange while context is not ready

gets fired but that's unrelated. I was able to successfully use the AppState API with the provided reproducer.

repro.mov

I think AppState works fine now.
But the error still occurred.
I tried to open ReactHostImpl.class (the built-in decompilation tool in Android Studio) and noticed that it seems not quite consistent with the code in the repository. I'm not sure if this decompiled code is reliable for reference.
This is my decompiled ReactHostImpl.class content:

@ThreadConfined("UI")
  public void onWindowFocusChange(boolean hasFocus) {
    String method = "onWindowFocusChange(hasFocus = \"" + hasFocus + "\")";
    this.log(method);
    ReactContext currentContext = this.getCurrentReactContext();
    if (currentContext != null) {
      currentContext.onWindowFocusChange(hasFocus);
    }

    ReactSoftExceptionLogger.logSoftException("ReactHost", new ReactNoCrashSoftException("Tried to access onWindowFocusChange while context is not ready"));
  }

@cortinico
Copy link
Contributor

I tried to open ReactHostImpl.class (the built-in decompilation tool in Android Studio) and noticed that it seems not quite consistent with the code in the repository. I'm not sure if this decompiled code is reliable for reference.

That's because you're looking at the class in main while you should be looking at the same class on the 0.74-stable branch:

https://github.com/facebook/react-native/blob/0.74-stable/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java

@alanjhughes what was the error you were facing?

@alanjhughes
Copy link
Collaborator

@cortinico its this error but it's only happening on app launch and not every time. Kudo reproduced in a RN project without expo. I'll speak to him today and see if he found a reliable way to reproduce.

@NiuGuohui
Copy link
Author

I tried to open ReactHostImpl.class (the built-in decompilation tool in Android Studio) and noticed that it seems not quite consistent with the code in the repository. I'm not sure if this decompiled code is reliable for reference.

That's because you're looking at the class in main while you should be looking at the same class on the 0.74-stable branch:

https://github.com/facebook/react-native/blob/0.74-stable/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java

So, should an else or return statement be added to line 664 in 0.74-stable ReactHostImpl.java

@cortinico
Copy link
Contributor

I tried to open ReactHostImpl.class (the built-in decompilation tool in Android Studio) and noticed that it seems not quite consistent with the code in the repository. I'm not sure if this decompiled code is reliable for reference.

That's because you're looking at the class in main while you should be looking at the same class on the 0.74-stable branch:
0.74-stable/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java

So, should an else or return statement be added to line 664 in 0.74-stable ReactHostImpl.java

We could pick ac3261f on the 0.74 release branch that would solve this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: AppState Needs: Attention Issues where the author has responded to feedback. Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Needs: Triage 🔍 Platform: Android Android applications. Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)
Projects
None yet
Development

No branches or pull requests

3 participants