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

Fix crash when Exit event is fired multiple times #13281

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

JorgeMucientes
Copy link
Contributor

@JorgeMucientes JorgeMucientes commented Jan 10, 2025

Exit event could potentially be fired more than once, which will make the app crash because it will attempt to navigate back more than once, leading to "IllegalStateException FragmentManager is already executing transactions"

Closes: #13226
Closes: #13289

Description

Tentative fix for crash:

IllegalStateException: FragmentManager is already executing transactions
    at com.woocommerce.android.ui.products.variations.VariationDetailFragment.setupObservers$lambda$24(VariationDetailFragment.kt:320)
    is Exit -> requireActivity().onBackPressedDispatcher.onBackPressed()
    at com.woocommerce.android.ui.products.variations.VariationDetailFragment.$r8$lambda$6j_9a98nQXRa_5YR2Y_XIxTyzTw(VariationDetailFragment.kt:0)
    at com.woocommerce.android.ui.products.variations.VariationDetailFragment$$ExternalSyntheticLambda14.invoke(VariationDetailFragment:0)
    at com.woocommerce.android.ui.products.variations.VariationDetailFragment$sam$androidx_lifecycle_Observer$0.onChanged(VariationDetailFragment.kt:0)
    at com.woocommerce.android.viewmodel.MultiLiveEvent.observe$lambda$0(MultiLiveEvent.kt:43)
    observer.onChanged(t)

And for the crash:

java.lang.IllegalStateException: FragmentManager is already executing transactions
    at androidx.fragment.app.FragmentManager.ensureExecReady(FragmentManager.java:1937)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1996)
    at androidx.fragment.app.FragmentManager.handleOnBackPressed(FragmentManager.java:862)
    at androidx.fragment.app.FragmentManager$1.handleOnBackPressed(FragmentManager.java:570)
    at androidx.activity.OnBackPressedDispatcher.onBackPressed(OnBackPressedDispatcher.kt:276)

Which has a similar stack trace.

I spent some time trying to replicate the crash but I was unable to. My hypothesis is that the Exit event is triggered simultaneously from VariationDetailViewModel when the user clicks the back button and when loadVariation function fails to load the variation and automatically runs: triggerEvent(Event.Exit). I tried using APIFaker to make different api calls fail when opening variation detail but wasn't able to reproduce the race condition where 2 Exit events are fired at once.

The proposed solution is to basically make sure we unsubscribe from LiveData as soon as the first Exit event is fired, so that any subsequent call to triggerEvent(Event.Exit) will be ignored.

Testing information

Open a product with variations and check that everything works as expected:

  • Add new variation works as expected
  • Opening variation detail and navigating back and forth works as expected
  • Deleting variation works as expected
  • Tap on the three dots menu and update several variations in bulk. Check everything works as expected

The tests that have been performed

The above

  • I have considered if this change warrants release notes and have added them to RELEASE-NOTES.txt if necessary. Use the "[Internal]" label for non-user-facing changes.

Reviewer (or Author, in the case of optional code reviews):

Please make sure these conditions are met before approving the PR, or request changes if the PR needs improvement:

  • The PR is small and has a clear, single focus, or a valid explanation is provided in the description. If needed, please request to split it into smaller PRs.
  • Ensure Adequate Unit Test Coverage: The changes are reasonably covered by unit tests or an explanation is provided in the PR description.
  • Manual Testing: The author listed all the tests they ran, including smoke tests when needed (e.g., for refactorings). The reviewer confirmed that the PR works as expected on big (tablet) and small (phone) in case of UI changes, and no regressions are added.

Exit event could potentially be fired more than once which will make the app crash because it will attempt to navigate back more than once leading to "IllegalStateException FragmentManager is already executing transactions"
@JorgeMucientes JorgeMucientes added type: crash The worst kind of bug. feature: product details Related to adding or editing products, includes product settings. labels Jan 10, 2025
@JorgeMucientes JorgeMucientes modified the milestones: 21.4, 21.5 Jan 10, 2025
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Jan 10, 2025

📲 You can test the changes from this Pull Request in WooCommerce-Wear Android by scanning the QR code below to install the corresponding build.
App Name WooCommerce-Wear Android
Platform⌚️ Wear OS
FlavorJalapeno
Build TypeDebug
Commit792072c
Direct Downloadwoocommerce-wear-prototype-build-pr13281-792072c.apk

@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Jan 10, 2025

📲 You can test the changes from this Pull Request in WooCommerce Android by scanning the QR code below to install the corresponding build.

App Name WooCommerce Android
Platform📱 Mobile
FlavorJalapeno
Build TypeDebug
Commit792072c
Direct Downloadwoocommerce-prototype-build-pr13281-792072c.apk

@codecov-commenter
Copy link

codecov-commenter commented Jan 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 40.79%. Comparing base (9218fa9) to head (792072c).
Report is 30 commits behind head on trunk.

Additional details and impacted files
@@             Coverage Diff              @@
##              trunk   #13281      +/-   ##
============================================
- Coverage     40.79%   40.79%   -0.01%     
  Complexity     6411     6411              
============================================
  Files          1353     1353              
  Lines         77679    77679              
  Branches      10692    10692              
============================================
- Hits          31690    31689       -1     
- Misses        43182    43183       +1     
  Partials       2807     2807              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: product details Related to adding or editing products, includes product settings. type: crash The worst kind of bug.
Projects
None yet
3 participants