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

[Text] Set LayoutParams in ReactTextView to fix crash #7011

Closed
wants to merge 1 commit into from

Conversation

ide
Copy link
Contributor

@ide ide commented Apr 16, 2016

ReactTextView occasionally crashes when setText is called. Doing some cursory research, it looks like this could be a bug in Android. We also suspect it might be related to removeClippedSubviews though.

The LayoutParams don't actually matter because RN controls layout, but on occasion (haven't narrowed down what this is...) mLayout is non-null and triggers relayout during setText, which fails because the LayoutParams are null. @jesseruder came up with this fix and it appears to be working well.

Stack trace: http://pastebin.com/P8VbxvPz

Test Plan: Run an Android app that was sporadically crashing with the stack trace linked above. With this patch the crashes appear to have disappeared and the app is performing fine.

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @dmmiller, @mkonicek and @axelander to be potential reviewers.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Apr 16, 2016
@skevy
Copy link
Contributor

skevy commented Apr 16, 2016

cc @kmagiera

@dmmiller
Copy link

@ide Will you look if this is related to #6805 and the subsequent fix (expo@1a99efb) I put in? I think what happens is that when removeClippedSubviews is true we do some operations in a slightly different order such that we can set text before the view is actually attached to a window. We thought there might be other issues that manifested like this but weren't sure. Can you see if fixing in the same way or looking at that has any leads?


// Android's TextView crashes when it tries to relayout if LayoutParams are
// null; explicitly set the LayoutParams to prevent this crash. See:
// https://github.com/facebook/react-native/pulls/7011
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like there is a typo in the link (should be /pull/7011 perhaps)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah thanks

@ide
Copy link
Contributor Author

ide commented Apr 19, 2016

@dmmiller sure -- are you referring to the technique of overriding onAttachedToWindow and calling some code that puts TextView in a state where it doesn't crash anymore?

// null; explicitly set the LayoutParams to prevent this crash. See:
// https://github.com/facebook/react-native/pulls/7011
setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd feel much safer if we were using LayoutParams(0, 0) instead of WRAP_CONTENT

Also layout param object can be created once as a static variable and then reused instead of allocating new object every time we create new component

@facebook-github-bot
Copy link
Contributor

@ide updated the pull request.

@dmmiller
Copy link

@ide yes. I was referring to the diff where we do stuff in onAttachedToWindow.

@ide ide force-pushed the layoutparams-fix branch 2 times, most recently from 6cdceab to 154dd3e Compare May 10, 2016 06:15
@ghost
Copy link

ghost commented May 10, 2016

@ide updated the pull request.

@ide
Copy link
Contributor Author

ide commented May 10, 2016

Trying a new approach by setting the layout params from onAttachedToWindow only when they are missing. Will test this in prod a bit...

@ghost
Copy link

ghost commented May 10, 2016

@ide updated the pull request.

@ide
Copy link
Contributor Author

ide commented May 10, 2016

The crash still happens when using onAttachedToWindow as the point to call the code. The TextView seems not to be attached at the point setText is called -- moving the check to setText() is the most straightforward here.

@ghost
Copy link

ghost commented May 10, 2016

@ide updated the pull request.

@ghost
Copy link

ghost commented May 13, 2016

@dmmiller would you mind taking a look at this pull request? It's been a while since the last commit was reviewed.

@fov42550564
Copy link

+1

@fov42550564
Copy link

why not merge to master, newest version is crash, too

@ide
Copy link
Contributor Author

ide commented May 31, 2016

If it's not the right fix or simple to maintain we don't want to merge it. Instead you should fork RN and cherry-pick the fixes you need.

@dmmiller
Copy link

dmmiller commented Jun 6, 2016

@facebook-github-bot shipit

ReactTextView occasionally crashes when `setText` is called. Doing some cursory research, it looks like this could be a bug in Android. We also suspect it might be related to removeClippedSubviews though.

The LayoutParams don't actually matter because RN controls layout, but on occasion (haven't narrowed down what this is...) `mLayout` is non-null and triggers relayout during `setText`, which fails because the LayoutParams are null.

Stack trace: http://pastebin.com/P8VbxvPz

Test Plan: Run an Android app that was sporadically crashing with the stack trace linked above. With this patch the crashes appear to have disappeared and the app is performing fine.
@ide
Copy link
Contributor Author

ide commented Jul 1, 2016

@facebook-github-bot shipit

@ghost ghost removed the GH Review: review-needed label Jul 1, 2016
@ghost ghost added GH Review: accepted Import Started This pull request has been imported. This does not imply the PR has been approved. labels Jul 1, 2016
@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to Phabricator to review.

@ghost ghost closed this in a8011bb Jul 1, 2016
@ide ide deleted the layoutparams-fix branch August 8, 2016 21:23
samerce pushed a commit to iodine/react-native that referenced this pull request Aug 23, 2016
Summary:
ReactTextView occasionally crashes when `setText` is called. Doing some cursory research, it looks like this could be a bug in Android. We also suspect it might be related to removeClippedSubviews though.

The LayoutParams don't actually matter because RN controls layout, but on occasion (haven't narrowed down what this is...) `mLayout` is non-null and triggers relayout during `setText`, which fails because the LayoutParams are null. jesseruder came up with this fix and it appears to be working well.

Stack trace: http://pastebin.com/P8VbxvPz
Closes facebook#7011

Differential Revision: D3508097

fbshipit-source-id: 12b4aa11e42112c8ba19a1af325e3ee9a232d08f
mpretty-cyro pushed a commit to HomePass/react-native that referenced this pull request Aug 25, 2016
Summary:
ReactTextView occasionally crashes when `setText` is called. Doing some cursory research, it looks like this could be a bug in Android. We also suspect it might be related to removeClippedSubviews though.

The LayoutParams don't actually matter because RN controls layout, but on occasion (haven't narrowed down what this is...) `mLayout` is non-null and triggers relayout during `setText`, which fails because the LayoutParams are null. jesseruder came up with this fix and it appears to be working well.

Stack trace: http://pastebin.com/P8VbxvPz
Closes facebook#7011

Differential Revision: D3508097

fbshipit-source-id: 12b4aa11e42112c8ba19a1af325e3ee9a232d08f
facebook-github-bot pushed a commit that referenced this pull request Feb 24, 2021
Summary:
## Issue:
Sometimes ReactTextView are vertically displayed as one column with bridgeless.

## Root cause:
After debugging, I found out this is caused by a workaround in 2016 to fix a crash caused by mLayout occasionally being non-null and triggers relayout during setText.

#7011

## Fix
Revert previous hack, if the crash happens again I'll try to fix it.

Changelog:
[Android][Fixed] -  Fix text in ReactTextView sometimes being vertically displayed

Reviewed By: mdvacca

Differential Revision: D26581756

fbshipit-source-id: a373d84dc1ab3d787bda7ec82f2d0865a354cf60
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Import Started This pull request has been imported. This does not imply the PR has been approved.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants