react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[0.74.0] AppState module has an error on android

Open NiuGuohui opened this issue 10 months ago • 11 comments

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 avatar Apr 25 '24 09:04 NiuGuohui

:warning: Missing Reproducible Example
:information_source: 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.

github-actions[bot] avatar Apr 25 '24 09:04 github-actions[bot]

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

cortinico avatar Apr 25 '24 09:04 cortinico

@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.

NiuGuohui avatar Apr 25 '24 10:04 NiuGuohui

@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

alanjhughes avatar Apr 25 '24 11:04 alanjhughes

@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 avatar Apr 25 '24 12:04 cortinico

@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.

https://github.com/facebook/react-native/assets/3001957/5d544f60-81d1-44f1-bee2-04fb6f86351f

cortinico avatar Apr 25 '24 16:04 cortinico

@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"));
  }

NiuGuohui avatar Apr 26 '24 01:04 NiuGuohui

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?

cortinico avatar Apr 26 '24 05:04 cortinico

@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.

alanjhughes avatar Apr 26 '24 05:04 alanjhughes

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

NiuGuohui avatar Apr 26 '24 09:04 NiuGuohui

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 https://github.com/facebook/react-native/commit/ac3261ff608768ff43736b413c5a5ad67668af61 on the 0.74 release branch that would solve this

cortinico avatar Apr 26 '24 16:04 cortinico

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)

bennysalewski avatar May 17 '24 17:05 bennysalewski

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.

duguyihou avatar May 18 '24 03:05 duguyihou

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)

C2418M31 avatar May 18 '24 17:05 C2418M31

https://github.com/facebook/react-native/commit/ac3261ff608768ff43736b413c5a5ad67668af61 I think it will be merge in the future.

NiuGuohui avatar May 20 '24 01:05 NiuGuohui

This will be picked in 0.74.2 here:

  • https://github.com/reactwg/react-native-releases/issues/291

cortinico avatar May 20 '24 17:05 cortinico

@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)

raphaeladams avatar Jul 04 '24 21:07 raphaeladams

and still seeing this error

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

cortinico avatar Jul 05 '24 07:07 cortinico

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.

raphaeladams avatar Jul 05 '24 12:07 raphaeladams

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

cortinico avatar Jul 05 '24 14:07 cortinico