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

Could not find root view for a given ancestor with tag #1675

Closed
federicot opened this issue Oct 7, 2021 · 51 comments · Fixed by #1936
Closed

Could not find root view for a given ancestor with tag #1675

federicot opened this issue Oct 7, 2021 · 51 comments · Fixed by #1936
Labels
Platform: Android This issue is specific to Android

Comments

@federicot
Copy link

Description

We are seeing the following crash on Android:

com.facebook.react.bridge.JSApplicationIllegalArgumentException: Could find root view for a given ancestor with tag 3199
    at com.swmansion.gesturehandler.react.RNGestureHandlerModule.tryInitializeHandlerForReactRootView(RNGestureHandlerModule.java:18)
    at com.swmansion.gesturehandler.react.RNGestureHandlerModule.attachGestureHandler(RNGestureHandlerModule.java:1)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:18)
    at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:2)
    at com.facebook.react.bridge.queue.NativeRunnable.run(NativeRunnable.java)
    at android.os.Handler.handleCallback(Handler.java:869)
    at android.os.Handler.dispatchMessage(Handler.java:101)
    at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:1)
    at android.os.Looper.loop(Looper.java:206)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:8)
    at java.lang.Thread.run(Thread.java:764)

Screenshots

Screenshot 2021-10-04 at 17 09 33

Steps To Reproduce

I was not able to reproduce it consistently, but it seems to happen after the app goes to foreground.

Package versions

  • React: 17.0.1
  • React Native: >= 0.64.2
  • React Native Gesture Handler: 1.10.3
  • React Navigation: 5.14.4
@alexfov
Copy link

alexfov commented Oct 13, 2021

I have the same problem in production.
"react-native": "0.65.1",
"react-native-gesture-handler": "1.9.0",
"@react-navigation/native": "5.8.10",

@farafonoff
Copy link

facing same issue
"react-native": "0.65.1",
"react-native-gesture-handler": "1.9.0"

@sapramit
Copy link

sapramit commented Oct 26, 2021

Any update on this one. occuring for me as well. Every time I try restarting the app programatically.

"react-native": "0.65.1",
"react-native-gesture-handler": "1.9.0" // Any version actually

@jcdhlzq
Copy link

jcdhlzq commented Oct 29, 2021

we are also suffering from this problem reported by users online. EXPECTING a solution!

@rdeift
Copy link

rdeift commented Nov 2, 2021

+1

1 similar comment
@ThienNgn
Copy link

ThienNgn commented Nov 4, 2021

+1

@systemride
Copy link

We are also seeing this with:

"react-native": "0.66.1",
"react-native-gesture-handler": "1.8.0",

@aleksandr-senichev
Copy link

+1

1 similar comment
@leofolive
Copy link

+1

@stephenkopylov
Copy link

+1

com.facebook.react.bridge.JSApplicationIllegalArgumentException: Could find root view for a given ancestor with tag 6343
        at com.swmansion.gesturehandler.react.RNGestureHandlerModule.tryInitializeHandlerForReactRootView(RNGestureHandlerModule.java:18)
        at com.swmansion.gesturehandler.react.RNGestureHandlerModule.attachGestureHandler(RNGestureHandlerModule.java:1)
        at java.lang.reflect.Method.invoke(Method.java:-2)
        at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:18)
        at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:13)
        at com.facebook.react.bridge.queue.NativeRunnable.run(NativeRunnable.java:-2)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:1)
        at android.os.Looper.loop(Looper.java:214)
        at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:8)
        at java.lang.Thread.run(Thread.java:919)

@wemow
Copy link

wemow commented Nov 23, 2021

Same issue here too
"react-native": "0.66.1",
"react-native-gesture-handler": "^1.10.3",

@anatooly
Copy link

Same issue here too
"react-native": "0.66.1",
"react-native-gesture-handler": "^1.10.3",

@systemride
Copy link

systemride commented Nov 25, 2021

This may not really be a fix, but we found a workaround for this that is working well for us. We are using:

"react-native": "0.66.1",
"react-native-gesture-handler": "1.8.0",
  1. Remove the Override in MainActivity.java:
-  @Override
-  protected ReactActivityDelegate createReactActivityDelegate() {
-    return new ReactActivityDelegate(this, getMainComponentName()) {
-      @Override
-      protected ReactRootView createRootView() {
-       return new RNGestureHandlerEnabledRootView(MainActivity.this);
-      }
-    };
-  }
  1. Add the GestureHandlerRootView to your app.
+ import { GestureHandlerRootView } from "react-native-gesture-handler";

...

+ <GestureHandlerRootView style={{ flex: 1 }}>
+   <App/>
+ </GestureHandlerRootView>
  1. This step might not be necessary and probably not recommended by the library authors, but we also forked our own version and made the following changes to RNGestureHandlerModule.java:
- throw new JSApplicationIllegalArgumentException("Could find root view for a given ancestor with tag "
              + ancestorViewTag);

... and just return instead of throwing. Honestly we're not exactly sure of the ramifications of this, but the app seems to behave normally in this scenario.

+ return;

@federicot
Copy link
Author

@systemride Thanks for sharing. Are you also using codepush?

@wemow
Copy link

wemow commented Nov 26, 2021

This may not really be a fix, but we found a workaround for this that is working well for us. We are using:

"react-native": "0.66.1",
"react-native-gesture-handler": "1.8.0",
  1. Remove the Override in MainActivity.java:
-  @Override
-  protected ReactActivityDelegate createReactActivityDelegate() {
-    return new ReactActivityDelegate(this, getMainComponentName()) {
-      @Override
-      protected ReactRootView createRootView() {
-       return new RNGestureHandlerEnabledRootView(MainActivity.this);
-      }
-    };
-  }
  1. Add the GestureHandlerRootView to your app.
+ import { GestureHandlerRootView } from "react-native-gesture-handler";

...

+ <GestureHandlerRootView>
+   <App/>
+ </GestureHandlerRootView>
  1. This step might not be necessary and probably not recommended by the library authors, but we also forked our own version and made the following changes to RNGestureHandlerModule.java:
- throw new JSApplicationIllegalArgumentException("Could find root view for a given ancestor with tag "
              + ancestorViewTag);

... and just return instead of throwing. Honestly we're not exactly sure of the ramifications of this, but the app seems to behave normally in this scenario.

+ return;

Well, unfortunately, your trick won't do for me, my app goes completely blank

@systemride
Copy link

Well, unfortunately, your trick won't do for me, my app goes completely blank

I updated my comment, you might also try adding flex styles to the component. Eg:

<GestureHandlerRootView style={{ flex: 1 }}>

@systemride
Copy link

@systemride Thanks for sharing. Are you also using codepush?

No, not using codepush.

@wemow
Copy link

wemow commented Nov 26, 2021

Damn, I did not even thought about that, I ended up using patch-package to patch the module and return instead of throw, I don't have any overrides on my MainActivity.java, it's working fine, I may try your way tho thanks!

@nchase
Copy link

nchase commented Dec 13, 2021

@federicot You asked if another user was using Codepush.

Are you aware of a relationship between Codepush and this crash?

My team is seeing an increase in crashes with this stacktrace, and it's not clear to us why. Unlike @systemride , we are using Codepush, and the crashes seem to have increased after a Codepush.

@jbaudanza
Copy link

@federicot You asked if another user was using Codepush.

Are you aware of a relationship between Codepush and this crash?

My team is seeing an increase in crashes with this stacktrace, and it's not clear to us why. Unlike @systemride , we are using Codepush, and the crashes seem to have increased after a Codepush.

Our team is also using Codepush. Crashes with this stack trace is pretty common after a codepush restart on Android. We have our app wrapped in GestureHandlerRootView, but maybe something about the codepush restart causes this to become unmounted? I'm planning on looking more into it this week.

@federicot
Copy link
Author

@federicot You asked if another user was using Codepush.

Are you aware of a relationship between Codepush and this crash?

My team is seeing an increase in crashes with this stacktrace, and it's not clear to us why. Unlike @systemride , we are using Codepush, and the crashes seem to have increased after a Codepush.

@nchase We are seeing a slight correlation when we do codepush releases. There's also this related issue #1724

@stephenkopylov
Copy link

We use codepush too

@aleksandr-senichev
Copy link

aleksandr-senichev commented Dec 14, 2021

As a temporal solution, we made a fork of stack navigator and replaced the GestureHandlerRootView wrapper with View for the Android app, because it doesn't use any gesture feature in our case. It works stable now.

@jayshah-swiggy
Copy link

Codepush user, also seeing similar crash

@aamagda
Copy link

aamagda commented Dec 17, 2021

We have the same issue after the codepush releases

@aamagda
Copy link

aamagda commented Dec 20, 2021

There is interesting analysis by @netshade
#715 (comment)

@kmagiera Hi, could you explain please the reason for checking rootViewTag < 1 and not for rootViewTag == View.NO_ID inside tryInitializeHandlerForReactRootView(ancestorViewTag: Int) method?

Do you have a plan to fix this issue or maybe any workarounds? What do you think about this advice #1675 (comment)

@jbaudanza
Copy link

I was able to resolve this issue in my project. The problem is that Codepush.restartApp doesn't actually restart the app. Instead, it uses the ReactNativeInstanceManager class to reload the bundle:

https://github.com/facebook/react-native/blob/4fb49cd555942caba1515b6a03f8ccea931dfc90/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java#L435

This means that if any RN-GestureHandler components begin initializing before a Codepush.restartApp, there is a very good chance that when they finish., the RootNode doesn't exist anymore. This is what causes the crash.

To solve this, you have to make sure there are no instances of RN-GestureHandler in the process of loading before a Codepush.restartApp operation. This includes things like react-navigation, which will mount gesture handlers behind the scenes.

In our case, when our app loads we begin a codepush sync. While the codepush is in any of these states, we render a simple View with no gesture handlers or react-navigation are mounted: CHECKING_FOR_UPDATE, DOWNLOADING_PACKAGE, UPDATE_INSTALLED, INSTALLING_UPDATE.

Only after the codepush is completely finished, it's safe to mount any react-navigation components.

@scesbron
Copy link

I have the same problem with code push in production. What I don't understand is that it is not systematic and in staging we are not able to reproduce it.

@jbaudanza
Copy link

I have the same problem with code push in production. What I don't understand is that it is not systematic and in staging we are not able to reproduce it.

It's not systematic because it's a race condition between 'attachGestureHandler' and codepush/ReactNativeInstanceHandler. If the gesture handler wins, then there is no crash. If you try it on an older Android phone, you should be able to reproduce it. I have a Samsung galaxy J3 and can reproduce it fairly often.

@nchase
Copy link

nchase commented Dec 21, 2021

Does anyone have steps that we can follow to consistently reproduce this bug? (ideally in an emulator)

@basurahan
Copy link

+1

@j-piasecki j-piasecki added the Platform: Android This issue is specific to Android label Jan 5, 2022
@tijs
Copy link

tijs commented Jan 12, 2022

We have a lot of these crashes but we do not use codepush so while it seems to be triggered by a codepush restart it can also happen in apps that do not use codepush at all.

@brduck
Copy link

brduck commented Jan 20, 2022

We're facing the same issue with OTA updates by expo-updates. It's happening since we upgraded react native to 0.66

@kperdomo1
Copy link

we found a way to test this out. In our case, our codePush installMode uses the ON_NEXT_RESUME strategy, so after downloading the codePush update it is installed when user comes back to the foreground.

  1. Launch an android emulator with low resources (in our case, 343 MB of RAM, 48PB of VM heap, 2 multi-core CPU)
  2. Find a way to trigger attaching a new gesture handler. In our case, it was enough to navigate to other screen when the app state changes from background to active.
  3. Release an update to CodePush so you can download it in your app.
  4. Launch your app and wait for the CodePush update to be downloaded
  5. Set app to the background
  6. Come back to the foreground
  7. At this point the race condition pointed out by @jbaudanza should happen and the app crashes.

Hope this helps

@cglacet
Copy link

cglacet commented Feb 14, 2022

The error doesn't say "Could not find" but "Could find".

@nchase
Copy link

nchase commented Feb 22, 2022

@systemride have you discovered any issues related to your workaround?

@nchase
Copy link

nchase commented Feb 22, 2022

This next one is more of a question for the maintainers: why is it desirable to throw an error in this case?

@systemride
Copy link

@systemride have you discovered any issues related to your workaround?

So far we have not discovered any other issues to our workaround but it's still something we want to revisit.

Also maybe worth noting... that while we don't use codepush, we do use react-native-restart which might do something similar to codepush.

@leofolive
Copy link

+1

@johnsoncwb
Copy link

same problem here, any solution?

@leofolive
Copy link

leofolive commented Mar 4, 2022

We're facing the same issue with OTA updates by expo-updates. It's happening since we upgraded react native to 0.66
@j-piasecki @kmagiera

@nchase
Copy link

nchase commented Mar 4, 2022

@kmagiera maybe this is a question that you can answer, since it seems that you may have written this code originally: why does this library throw an error in this case?

@deboralbarros
Copy link

We have the same issue here, any updates?

@leofolive
Copy link

We're facing the same issue with OTA updates by expo-updates. It's happening since we upgraded react native to 0.66
@j-piasecki @kmagiera

@leofolive
Copy link

We're facing the same issue with OTA updates by expo-updates. It's happening since we upgraded react native to 0.66
@j-piasecki @kmagiera

@itenl
Copy link

itenl commented Mar 21, 2022

0.66.2 + react-native-gesture-handler + CodePush same issues

j-piasecki added a commit that referenced this issue Mar 30, 2022
`tryInitializeHandlerForReactRootView` was causing crashes, especially with combination with CodePush. `RNGestureHandlerEnabledRootView` was deprecated in version 2.0.0 in favor of `GestureHandlerRootView` component and this PR removes implementation of `RNGestureHandlerEnabledRootView` and code responsible for interactions with it.
Adds a new page in docs explaining how to migrate to `GestureHandlerRootView`.

Potentially fixes #1675.
@nchase
Copy link

nchase commented Apr 6, 2022

Thank you, @j-piasecki ❤️

Has anyone had time to try #1936 as a patch?

@abdymm
Copy link

abdymm commented Aug 23, 2022

hi @LFMAKER @brduck are you guys find a way to solve this on expo?

@xwartz
Copy link

xwartz commented May 25, 2023

are there any solutions?

@sebas21
Copy link

sebas21 commented Aug 16, 2023

any solution??? RN 0.69

@VashaMasha
Copy link

This may not really be a fix, but we found a workaround for this that is working well for us. We are using:

"react-native": "0.66.1",
"react-native-gesture-handler": "1.8.0",
  1. Remove the Override in MainActivity.java:
-  @Override
-  protected ReactActivityDelegate createReactActivityDelegate() {
-    return new ReactActivityDelegate(this, getMainComponentName()) {
-      @Override
-      protected ReactRootView createRootView() {
-       return new RNGestureHandlerEnabledRootView(MainActivity.this);
-      }
-    };
-  }
  1. Add the GestureHandlerRootView to your app.
+ import { GestureHandlerRootView } from "react-native-gesture-handler";

...

+ <GestureHandlerRootView style={{ flex: 1 }}>
+   <App/>
+ </GestureHandlerRootView>
  1. This step might not be necessary and probably not recommended by the library authors, but we also forked our own version and made the following changes to RNGestureHandlerModule.java:
- throw new JSApplicationIllegalArgumentException("Could find root view for a given ancestor with tag "
              + ancestorViewTag);

... and just return instead of throwing. Honestly we're not exactly sure of the ramifications of this, but the app seems to behave normally in this scenario.

+ return;

Thank you!
Worked for me

"react-native": "0.67.2",
"react-native-gesture-handler": "2.2.0",

Version numbers stayed the same and I don't have the code from step 3. So I only implemented steps 1 and 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: Android This issue is specific to Android
Projects
None yet
Development

Successfully merging a pull request may close this issue.