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

Closed
NiuGuohui opened this issue Apr 25, 2024 · 21 comments
Closed

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

NiuGuohui opened this issue Apr 25, 2024 · 21 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

@bennysalewski
Copy link

It seems this issue is still present in 0.74.1 (tried also 0.74.1-rc.0)

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:1810)
at android.view.View.dispatchWindowFocusChanged(View.java:16171)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1510)
at android.view.ViewRootImpl.dispatchFocusEvent(ViewRootImpl.java:4283)
at android.view.ViewRootImpl.handleWindowFocusChanged(ViewRootImpl.java:4187)
at android.view.ViewRootImpl.-$$Nest$mhandleWindowFocusChanged(Unknown Source:0)
at android.view.ViewRootImpl$ViewRootHandler.handleMessageImpl(ViewRootImpl.java:6315)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:6223)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loopOnce(Looper.java:232)
at android.os.Looper.loop(Looper.java:317)
at android.app.ActivityThread.main(ActivityThread.java:8501)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)

@duguyihou
Copy link

I see the same error in 0.74.1 as well.

  Unable to match the desired swap behavior.
  Unable to match the desired swap behavior.
  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:674)
	at com.facebook.react.ReactDelegate.onWindowFocusChanged(ReactDelegate.java:194)
	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:1788)
	at android.view.View.dispatchWindowFocusChanged(View.java:15933)
	at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1509)
	at android.view.ViewRootImpl.dispatchFocusEvent(ViewRootImpl.java:4059)
	at android.view.ViewRootImpl.handleWindowFocusChanged(ViewRootImpl.java:3963)
	at android.view.ViewRootImpl.-$$Nest$mhandleWindowFocusChanged(Unknown Source:0)
	at android.view.ViewRootImpl$ViewRootHandler.handleMessageImpl(ViewRootImpl.java:5954)
	at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:5862)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loopOnce(Looper.java:205)
	at android.os.Looper.loop(Looper.java:294)
	at android.app.ActivityThread.main(ActivityThread.java:8177)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
  [0518/133752.908692:ERROR:variations_seed_loader.cc(37)] Seed missing signature.

@C2418M31
Copy link

I have the same issue but this is from RN as AAR and uploaded to the Android native

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:674) at com.facebook.react.ReactDelegate.onWindowFocusChanged(ReactDelegate.java:194) 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:2320) at android.view.View.dispatchWindowFocusChanged(View.java:16490) at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1517) at android.view.ViewRootImpl.dispatchFocusEvent(ViewRootImpl.java:5003) at android.view.ViewRootImpl.handleWindowFocusChanged(ViewRootImpl.java:4887) at android.view.ViewRootImpl.-$$Nest$mhandleWindowFocusChanged(Unknown Source:0) at android.view.ViewRootImpl$ViewRootHandler.handleMessageImpl(ViewRootImpl.java:7150) at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:7051) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:230) at android.os.Looper.loop(Looper.java:319) at android.app.ActivityThread.main(ActivityThread.java:8893) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)

@NiuGuohui
Copy link
Author

ac3261f
I think it will be merge in the future.

@raphaeladams
Copy link

@cortinico I'm using "react-native": "0.74.2" and still seeing this error. Are there any other dependencies that could cause this?

E  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:675)
	at com.facebook.react.ReactDelegate.onWindowFocusChanged(ReactDelegate.java:194)
	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:2767)
	at android.view.View.dispatchWindowFocusChanged(View.java:15880)
	at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1518)
	at android.view.ViewRootImpl.handleWindowFocusChanged(ViewRootImpl.java:4713)
	at android.view.ViewRootImpl.-$$Nest$mhandleWindowFocusChanged(Unknown Source:0)
	at android.view.ViewRootImpl$ViewRootHandler.handleMessageImpl(ViewRootImpl.java:6866)
	at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:6775)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loopOnce(Looper.java:226)
	at android.os.Looper.loop(Looper.java:313)
	at android.app.ActivityThread.main(ActivityThread.java:8810)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

@cortinico
Copy link
Contributor

and still seeing this error

Do you mean you see the app crashing or is it appearing in the log?

@raphaeladams
Copy link

and still seeing this error

Do you mean you see the app crashing or is it appearing in the log?

@cortinico the error appears in the log, and the app just shows a blank screen and fails to render its loading screen, but does not crash.

Let me know if it's expected that this error will still appear in logs without crashing. That could indicate some other issue in my code that isn't related to this.

@cortinico
Copy link
Contributor

Let me know if it's expected that this error will still appear in logs without crashing. That could indicate some other issue in my code that isn't related to this.

It's expected that it appears in the logs, as we're raising a soft exception. It shouldn't cause your app to crash though

@NetPumi2
Copy link

i still have it on RN 0.76.1

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

8 participants