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

Avoid null pointer exception when creating new stores #10534

Merged

Conversation

JorgeMucientes
Copy link
Contributor

@JorgeMucientes JorgeMucientes commented Jan 11, 2024

Fixes crash: #10525

Description

Avoid null pointer exceptions during store creation when the Android system process death happens for the app.

Crash in action:

crash.mp4

Testing instructions

  1. Trigger store creation flow from site picker.
  2. Click on "Try for free"
  3. Navigate to country picker step for example and send the app to the background
  4. Run this from terminal adb shell am kill com.woocommerce.android.dev while the app is in background
  5. Wait for about 5 seconds
  6. Open the app and continue the store creation flow to the last step where the progress bar is shown. The app should not crash anymore after theme picker step (like in the screen recording above).

Fixed

fixCrash.mp4
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Jan 11, 2024

📲 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
FlavorJalapeno
Build TypeDebug
Commita388427
Direct Downloadwoocommerce-prototype-build-pr10534-a388427.apk

@JorgeMucientes JorgeMucientes added the type: crash The worst kind of bug. label Jan 11, 2024
@JorgeMucientes JorgeMucientes marked this pull request as ready for review January 11, 2024 11:18
@dangermattic
Copy link
Collaborator

1 Warning
⚠️ PR is not assigned to a milestone.

Generated by 🚫 Danger

@JorgeMucientes JorgeMucientes added this to the 16.9 milestone Jan 11, 2024
Comment on lines -105 to +109
AnalyticsTracker.KEY_URL to storeData.domain!!,
AnalyticsTracker.KEY_URL to storeData.domain,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The domain value is only for tracking purposes. So in the case of process death, it will be null and the !! will be causing the app to crash. Removed it. For the very few corner cases where storeData is null, we'll be tracking a null value, but is not a problem.

@@ -83,6 +83,10 @@ class StoreInstallationViewModel @Inject constructor(
init {
if (!savedStateHandle.contains(STORE_DATA_KEY)) {
storeData = newStore.data
if (storeData.siteId == null) {
storeData = storeData.copy(siteId = appPrefsWrapper.createdStoreSiteId)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In case the values from memory were whipped due to process death, we retrieve the required siteId for the new site from sharedPreferences.

@malinajirka malinajirka self-requested a review January 11, 2024 11:30
@malinajirka malinajirka self-assigned this Jan 11, 2024
Copy link
Contributor

@malinajirka malinajirka 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 working on this @JorgeMucientes!

I've tested the app before the changes and after the changes and I can confirm it fixes the crash. However, it seems the solution ignores all the previous user choices. I tried selecting an industry and I killed the app. It seemed the store data were completely empty and my selection wasn't being taken into account. Wdyt?

@JorgeMucientes
Copy link
Contributor Author

Hey @malinajirka. Thanks for the review!
Good point! Yes, that is true, in the unlikely case of app process death during store creation (which lasts around 1min so chances are really low) the user selected choices will be lost. Currently, we only save siteId and selectedTheme into disk because they are the only options that have an impact on the new store and thus are essential. The rest of the data is kept in memory because is "a nice to have" since its is merely used for tracking purposes.

However, I agree that we should probably save everything into DB and ensure all the data survives process death. But that would require several changes and I think is out of scope for this crash fix, so I've created a new issue to tackle that as a different task. Wdyt?

Copy link
Contributor

@malinajirka malinajirka left a comment

Choose a reason for hiding this comment

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

because they are the only options that have an impact on the new store and thus are essential. The rest of the data is kept in memory because is "a nice to have" since its is merely used for tracking purposes.

Ohh, interesting - I thought the choices are taken into consideration for customized content of the newly created site => that's how it works (or used to work at least) in the WordPress/Jetpack apps.

If that's truly the case and it doesn't have any impact on the UX, I agree this solution is good enough for this edge case. If it'd be affecting the final output, I'd personally prefer exiting the site creation flow and forcing users into starting over. Storing everything into the DB is definitely the best possible solution, but the required effort might not be worth it.

I'm approving the PR, feel free to merge it. Thanks!

@JorgeMucientes
Copy link
Contributor Author

Just a couple clarifications @malinajirka

Ohh, interesting - I thought the choices are taken into consideration for customized content of the newly created site => that's how it works (or used to work at least) in the WordPress/Jetpack apps.

In the future afaik is meant to have an impact. For now, they are just options we sent to Tracks and to Woo backend for a better understanding of Woo Express customers.

I'd personally prefer exiting the site creation flow and forcing users into starting over.

Unfortunately, that is not an option or would be very complicated as it would require deleting the new site before the user start customising it. The moment the user clicks on "Try for free" in store creation first step, a new site is created immediately. So even if you abandon the process right away, the site is already created and will be present in the site picker the next time they open the app or the web.

@JorgeMucientes JorgeMucientes merged commit 7532707 into trunk Jan 11, 2024
14 checks passed
@JorgeMucientes JorgeMucientes deleted the fix/10525-nullpointer-exception-on-store-creation branch January 11, 2024 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: crash The worst kind of bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants