-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Reader] Fix IllegalStateException in ReaderPostListFragment #20507
[Reader] Fix IllegalStateException in ReaderPostListFragment #20507
Conversation
📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
|
📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, @daniloercoli . LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm marking it a "change requested" because I don't think this addresses the root cause and will also not fix the crash, see my comment in the code.
@@ -745,6 +745,9 @@ public void onPause() { | |||
@Override | |||
public void onResume() { | |||
super.onResume(); | |||
if (!isAdded() || getView() == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this will help though, it looks like the line that's crashing is line 2322 here, according to the stack trace of the crash:
java.lang.IllegalStateException: Fragment ReaderPostListFragment{b2c45df} (12cf4b19-e069-4888-ab5a-911d5ed99a51) not attached to an activity.
at androidx.fragment.app.Fragment.requireActivity(Fragment.java:1000)
at org.wordpress.android.ui.reader.ReaderPostListFragment$11.run(ReaderPostListFragment.java:2322)
If you look a couple of lines above the crashing line you will see that a similar check is already being made and even then the crash happened to a few users. I guess that, since the code is being run asynchronously, it might have been delayed enough that when line 2322 is reached the Fragment is no longer attached.
If that's indeed the case, we probably shouldn't be using requireActivity
there, but instead just get the activity and only run the code if it is not null, since being null should mean that Fragment is no longer attached so that update is no longer needed anyway. Wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your suggestions 🙇 . I've made the adjustments as recommended, although it resulted in about 4 isAdded checks and 2 getActivity() != null conditions within a few lines of code. 😵💫
Side note: I believe the bottleneck isn't the shouldAutoUpdateTag
function itself, but rather the delay in starting the thread, likely due to many other threads running concurrently to update the Reader in the background.
Regardless, we should have significantly reduced the likelihood of the crash occurring now.
…urrentTag Thread starts.
@@ -2320,10 +2321,15 @@ private void updateCurrentTagIfTime() { | |||
new Thread() { | |||
@Override | |||
public void run() { | |||
// Check the fragment is attached to the activity when this Thread starts. | |||
FragmentActivity activity = getActivity(); | |||
if (activity == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 I think doing the check here still has a great chance of not solving the crash, since the function that probably takes the most time to run is shouldAutoUpdateTag
. This means that even with this check here, when the code reaches line 2330 or 2332 the fragment might already be dettached anyway, which will likely yield other issues.
Because of that, I think it would be better if the check is done in each of the if clause's blocks (before line 2330 and line 2332) below.
I know the code will look a bit repetitive, but since we're working with Java here, there's not much way around it. 😞
… the code that actually use the activity
Quality Gate passedIssues Measures |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! Thanks! 🙇🏼
Fixes #20228
I was unable to reproduce the problem, and the occurrences of it (JETPACK-ANDROID-JMJ) are very low. Ensuring that the Fragment is still attached to the Activity in onResume should fix the problem.
To Test:
PR Submission Checklist:
RELEASE-NOTES.txt
if necessary.Testing Checklist (strike-out the not-applying and unnecessary ones):