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

[Android] Inverted FlatList + Text Input causing ANR on android 13

Open hannojg opened this issue 2 years ago β€’ 29 comments

Description

When having a FlatList and a TextInput in one view and the FlatList has the inverted={true} prop set and the android device is on API 33 (Android 13) the app will freeze/die in an ANR.

https://user-images.githubusercontent.com/16821682/201913904-fd6f071d-2712-4cb5-b7b2-f6c1c7a4ee02.mov

Version

0.70.5

Output of npx react-native info

info Fetching system and libraries information...
System:
    OS: macOS 12.6
    CPU: (8) arm64 Apple M1 Pro
    Memory: 96.63 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 8.11.0 - ~/.nvm/versions/node/v16.15.1/bin/npm
    Watchman: Not Found
  Managers:
    CocoaPods: Not Found
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 16.0, macOS 12.3, tvOS 16.0, watchOS 9.0
    Android SDK:
      API Levels: 28, 29, 30, 31, 32, 33
      Build Tools: 29.0.2, 30.0.2, 30.0.3, 31.0.0, 32.0.0, 32.1.0, 33.0.0
      System Images: android-32 | Google APIs ARM 64 v8a, android-32 | Google APIs Intel x86 Atom_64, android-32 | Google Play ARM 64 v8a, android-33 | Google APIs ARM 64 v8a, android-33 | Google Play ARM 64 v8a
      Android NDK: 21.4.7075529
  IDEs:
    Android Studio: Chipmunk 2021.2.1 Patch 1 Chipmunk 2021.2.1 Patch 1
    Xcode: 14.0.1/14A400 - /usr/bin/xcodebuild
  Languages:
    Java: 17.0.5 - /Users/hannogodecke/.jenv/shims/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 18.1.0 => 18.1.0 
    react-native: 0.70.5 => 0.70.5 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps to reproduce

  • Clone this repository
  • Run yarn
  • Run yarn start
  • Run yarn android, make sure the app launches on a device with android API 33 (Android 13)
  • Open the app
  • Open the developer tools and enable the performance monitor
  • Focus the text input
  • Run the following in your terminal:
adb shell input text "Lorem\ ipsum\ dolor\ sit\ amet,\ consetetur\ sadipscing\ elitr,\ sed\ diam\ nonumy\ eirmod\ tempor\ invidunt\ ut\ labore\ et\ dolore\ magna\ aliquyam\ erat,\ sed\ diam\ voluptua.\ At\ vero\ eos\ et\ accusam\ et\ justo\ duo\ dolores\ et\ ea\ rebum.\ Stet\ clita\ kasd\ gubergren,\ no\ sea\ takimata\ sanctus\ est\ Lorem\ ipsum\ dolor\ sit\ amet.\ Lorem\ ipsum\ dolor\ sit\ amet,\ consetetur\ sadipscing\ elitr,\ sed\ diam\ nonumy\ eirmod\ tempor\ invidunt\ ut\ labore\ et\ dolore\ magna\ aliquyam\ erat,\ sed\ diam\ voluptua.\ At\ vero\ eos\ et\ accusam\ et\ justo\ duo\ dolores\ et\ ea\ rebum.\ Stet\ clita\ kasd\ gubergren,\ no\ sea\ takimata\ sanctus\ est\ Lorem\ ipsum\ dolor\ sit\ amet."
  • Run it repeatedly until the app freezes (for me it takes ~ 3 repetitions until the ANR happens. On a real Pixel 4 of a colleague the app almost freezes immediately).

Snack, code example, screenshot, or link to a repository

https://github.com/hannojg/RN-AndroidAPI33-TextInput-ANR

hannojg avatar Nov 15 '22 12:11 hannojg

https://github.com/facebook/react-native/issues/35155 The similar problem seems to occur even with only 'TextInput'

rudenick0309 avatar Nov 15 '22 23:11 rudenick0309

Interestingly the TextInput works for me on the device but stops working once it's next to an inverted FlatList. So I am not entirely sure if it's related, but I will test the reproduction of the issue you mentioned as well!

hannojg avatar Nov 16 '22 11:11 hannojg

Observations I made:

  • I just put a plain android EditText view instead of RN's TextInput and the issue still happens (so I assume it isn't the TextInput implementation)
  • I tried it with VirtualizedList and the following styles (to re-create the inverted effect), but the ANR still happens:
style={{
    transform: [{scaleY: -1}],
}}
contentContainerStyle={{
    transform: [{scaleY: -1}],
}}
  • I tried it with ScrollView (used by VirtualizedList) using the styles above, and the issue keeps happening as well
  • If I don't do the inverted styling (its the same way VirtualizedList does it) the ANR doesn't happen

Still looking for the place that's causing the issue πŸ”

hannojg avatar Nov 16 '22 11:11 hannojg

@hannojg we had the same problem with our app. We are using the inverted FlatList in our chat screen. One time I wasn't typing, I was just receiving messages and the app froze. So I'm not 100% sure the TextInput is needed to repro. The workaround we implemented was to invert the list with styles instead of the prop. You can check the solution in this Github issue.

Ah! And also this issue was happening only for Pixel devices with Android 13. Since the first family of devices to get Android 13 was the Pixel, maybe that's why we only saw this issue in Pixel devices.

JuanAlejandro avatar Nov 16 '22 13:11 JuanAlejandro

Wow thx for sharing this information, will give it a try!

What I found is, that you don't even need a list. You just need two views that have scaleY: 1 set:

     <View
        style={{
          flex: 1,
          backgroundColor: 'red',
          transform: [{scaleY: -1}],
        }}>
        <View
          style={{
            transform: [{scaleY: -1}],
          }}>
           {/* Some more children inside here that got inverted */}

However, I still need to input text constantly into a TextInput to cause the ANR πŸ˜…

hannojg avatar Nov 16 '22 14:11 hannojg

Okay, the issue doesn't happen anymore if I also add scaleX: -1 πŸ‘€ :

     <View
        style={{
          flex: 1,
          backgroundColor: 'red',
          transform: [{scaleX: -1}, {scaleY: -1}],
        }}>
        <View
          style={{
            transform: [{scaleX: -1}, {scaleY: -1}],
          }}>

Going to dig into the native code to see where the issue happens, maybe also a bug with yoga?

hannojg avatar Nov 16 '22 18:11 hannojg

@hannojg @JuanAlejandro sorry for telling this, this problem was occured at real device of galaxy22 with google play system version - 22.07.01, but, after updating google play system version to '22.10.01'. the problem was disappeared. ( i could not test my local, that device is our customer. ) so, i am looking for other galaxy22 for verifying. after results are out, i will write down.

rudenick0309 avatar Nov 16 '22 23:11 rudenick0309

I just found the offending line that's causing the issue. If I remove the view.setRotationX line the issue stops happening. At this point I am not sure whether this is a react native or an android bug. Will dig deeper:

https://github.com/facebook/react-native/blob/e504141583fc2b695fb42a1037edf92c89ae552a/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java#L441-L483

hannojg avatar Nov 17 '22 14:11 hannojg

So at this point, I was able to reproduce the issue with just a native android project (no react native involved). It's a bug in the android framework.

I tried to create an issue ticket on the AOSP, however, after creation, I get weird permission errors and the bug ticket just disappears.

Sorry for the ping, but maybe @cortinico you can help with opening a bug ticket for the android team (I recall you were an android engineer? πŸ˜… )?

The bug report for the native android looks like this:

Setting scaleY: -1 and rotationX: 180 for two nested views causes an ANR on android API 33

On Android 13 we are noticing an ANR, when we have two views that have each:

scaleY: -1

rotationX: 180

set.

In the video, you can see a green view, the parent of a blue view (and the blue view has a lot of children (list)). Both views have the transformation properties set (creating an "inverted" effect).

We then have an EditText rendered underneath these views. When entering text the ANR happens. This is much easier to reproduce on a real device. We are reproducing with a Pixel 4 Android API 33 emulator. Here you need to enter text rapidly with the help of add shell input text to cause the ANR.

In the bottom right you can see a small FPS counter (using Choreographer), and you can see that the app starts to freeze at some point.

The full reproduction: https://github.com/hannojg/AndroidScaleYReproductionANR-

I also attached an adb bugreport (it's from an emulator, so no sensitive data)

The important code part to reproduce:

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val parentView = RelativeLayout(this)
        parentView.layoutParams = ViewGroup.LayoutParams(1000, 2000)
        parentView.setBackgroundColor(0xFF00FF00.toInt())
        parentView.scaleY = -1f
        parentView.rotationX = 180f

        // add a child view to the parentView which we invert as well:
        val childView = LinearLayout(this)
        childView.orientation = LinearLayout.VERTICAL
        childView.layoutParams = ViewGroup.LayoutParams(800, 1800)
        childView.setBackgroundColor(0xFF0000FF.toInt())
        childView.scaleY = -1f
        childView.rotationX = 180f

        parentView.addView(childView)

        // add 20 text views to the child view
        for (i in 0..19) {
            val textView = TextView(this)
            val layoutPrams = ViewGroup.MarginLayoutParams(200, 100)
            layoutPrams.topMargin = 10
            textView.layoutParams = layoutPrams
            textView.setBackgroundColor(0xFFD3D3D3.toInt())
            textView.gravity = Gravity.CENTER
            textView.text = "Text $i"

            childView.addView(textView)
        }

        binding.rootLayout.addView(parentView)
    }
}

Thanks in advance for checking!

https://user-images.githubusercontent.com/16821682/203284168-c5c6ad81-338f-4132-8f9f-c220d1d448f9.mov

hannojg avatar Nov 22 '22 09:11 hannojg

Sorry for the ping, but maybe @cortinico you can help with opening a bug ticket for the android team (I recall you were an android engineer? πŸ˜… )?

You can do so by yourself here: https://issuetracker.google.com/issues

cortinico avatar Nov 23 '22 19:11 cortinico

Closign also as it's a Android Bug apparently. Feel free to reopen if they send you here

cortinico avatar Nov 23 '22 19:11 cortinico

@hannojg Hi, have you opened a bug ticket for the android team? Same issue here.

GuoXiaoyang avatar Nov 24 '22 07:11 GuoXiaoyang

Yeah that's the point, opening a bug ticket at https://issuetracker.google.com/issues doesn't work for me and results in permission errors. Does anyone of you have created an issue there and can help with that?

hannojg avatar Nov 24 '22 08:11 hannojg

Yeah that's the point, opening a bug ticket at https://issuetracker.google.com/issues doesn't work for me and results in permission errors. Does anyone of you have created an issue there and can help with that?

I will have a try. And I noticed that you have forked the React Native repo and added a verticalScrollbarPosition prop to FlatList to fix this issue as a workaround. But it's a little difficult for us to use a forked RN version😭. I will try to find another solution if possible. Do you have any suggestions?

GuoXiaoyang avatar Nov 24 '22 08:11 GuoXiaoyang

Yeah please try, you can copy paste the bug report I pasted in one of the above comments. I always get this weird error after creation:

Screenshot 2022-11-24 at 10 30 11

Yeah, I was thinking about adding verticalScrollbarPosition prop to react native itself. Unfortunately, I haven't found any other workaround yet

hannojg avatar Nov 24 '22 09:11 hannojg

@hannojg I believe I succeeded to add it with the correct info and at the right place (the issue tracker is a jungle πŸ˜…) https://issuetracker.google.com/issues/261572772

EmilScherdin avatar Dec 06 '22 19:12 EmilScherdin

@EmilScherdin unfortunately not, I can't open that issue either:

Screenshot 2022-12-09 at 10 50 06

hannojg avatar Dec 09 '22 09:12 hannojg

The issue is closed. Then what is the workaround or any update in react native? Can someone help me please

MohammadAzimi avatar Dec 18 '22 16:12 MohammadAzimi

It shouldn't be closed for sure; seems like it is still an ongoing issue based on multiple threads on the very same topic.

An issue that seems quite huge to be honest and should be part of priorities; but should it be on react-native or Android?

pierroo avatar Dec 19 '22 10:12 pierroo

Can we get a minimal repo that shows the issue? Then we can fix it.

billnbell avatar Jan 26 '23 02:01 billnbell

Re-opening as this seems to affect a significant number of users, although this seems to be an Android-specific issue (and not an RN one).

@hannojg I believe I succeeded to add it with the correct info and at the right place (the issue tracker is a jungle πŸ˜…) issuetracker.google.com/issues/261572772

@EmilScherdin can you (or someone else) open another issue on Google's issue tracker to report this?

cortinico avatar Jan 27 '23 19:01 cortinico

@cortinico Not sure if they move the issues internally or not. I can see it directly after reporting, but then later I also get the access denied. At least I reported it again: https://issuetracker.google.com/issues/266987950

EmilScherdin avatar Jan 28 '23 17:01 EmilScherdin

At least I reported it again: issuetracker.google.com/issues/266987950

I also can't open this sadly

cortinico avatar Jan 30 '23 12:01 cortinico

The issue with text input might be due to a bug with Samsung's Grammarly integration. Samsung recently did an update that made most of our ANRs go away, as long users had our update code that stopped using inverted lists and used the scaleY trick instead.

phatmann avatar Jan 31 '23 20:01 phatmann

@phatmann these are two separate issues:

hannojg avatar Feb 01 '23 08:02 hannojg

@hannojg They are indeed separate issues, yet most of the bug reports I have seen are around the combination of these two issues. Apparently they exacerbate each other. However, in our experience users on Android 13 will experience ANRs unless both issues are resolved. We found that fixing the inverted issue fixed ANRs for some but not all users; we did not follow up to see if these successful users had disabled Grammarly keyboard integration or used Gboard, but I suspect that is the case. We did have reports that users with the Samsung update (that fixes their keyboard issues) but without our inverted update still had ANRs. TL;DR: both issues have to be fixed.

phatmann avatar Feb 01 '23 12:02 phatmann

We've tried to use the workaround using the scaleY=-1 transform, but running into an issue with refreshControl getting triggered at the top instead of the bottom. Has anyone figured out a workaround that works with refreshControl as well?

t0nyh0 avatar Feb 01 '23 22:02 t0nyh0

@t0nyh0 check this: https://github.com/facebook/react-native/issues/30034#issuecomment-1063980066

efstathiosntonas avatar Feb 01 '23 22:02 efstathiosntonas

The android bug tracker seems to be working again: https://issuetracker.google.com/issues/287304310

Meanwhile I am preparing a workaround fix for within react native.

hannojg avatar Jun 15 '23 12:06 hannojg

@hannojg is this issue already fixed in react-native v0.72.4? https://github.com/facebook/react-native/releases/tag/v0.72.4

stelmakhivan avatar Oct 03 '23 08:10 stelmakhivan