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

[Reader] Fix IllegalStateException in ReaderPostListFragment #20507

Conversation

daniloercoli
Copy link
Contributor

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:

  • Smoke test the Reader.

PR Submission Checklist:

  • I have completed the Regression Notes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

Testing Checklist (strike-out the not-applying and unnecessary ones):

  • WordPress.com sites and self-hosted Jetpack sites.
  • Portrait and landscape orientations.
  • Light and dark modes.
  • Fonts: Larger, smaller and bold text.
  • High contrast.
  • Talkback.
  • Languages with large words or with letters/accents not frequently used in English.
  • Right-to-left languages. (Even if translation isn’t complete, formatting should still respect the right-to-left layout)
  • Large and small screen sizes. (Tablet and smaller phones)
  • Multi-tasking: Split screen and Pop-up view. (Android 10 or higher)

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Mar 19, 2024

WordPress📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
App NameWordPress WordPress
FlavorJalapeno
Build TypeDebug
Versionpr20507-88e9cab
Commit88e9cab
Direct Downloadwordpress-prototype-build-pr20507-88e9cab.apk
Note: Google Login is not supported on these builds.

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Mar 19, 2024

Jetpack📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
App NameJetpack Jetpack
FlavorJalapeno
Build TypeDebug
Versionpr20507-88e9cab
Commit88e9cab
Direct Downloadjetpack-prototype-build-pr20507-88e9cab.apk
Note: Google Login is not supported on these builds.

Copy link
Contributor

@RenanLukas RenanLukas left a 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 :shipit:

Copy link
Contributor

@thomashorta thomashorta left a 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) {
Copy link
Contributor

@thomashorta thomashorta Mar 20, 2024

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?

Copy link
Contributor Author

@daniloercoli daniloercoli Mar 21, 2024

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.

@@ -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) {
Copy link
Contributor

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

Copy link

sonarcloud bot commented Mar 21, 2024

Quality Gate Passed Quality Gate passed

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link
Contributor

@thomashorta thomashorta left a 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! 🙇🏼

@daniloercoli daniloercoli merged commit c04dc18 into trunk Mar 22, 2024
20 checks passed
@daniloercoli daniloercoli deleted the issue/20228-Illegal-state-Exception---ReaderPostListFragment branch March 22, 2024 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants