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 issue with PaymentLauncher configuration change #4776

Merged

Conversation

jameswoo-stripe
Copy link
Contributor

@jameswoo-stripe jameswoo-stripe commented Mar 25, 2022

Summary

References #4739

Fixes an issue where configuration change of PaymentLauncherConfirmationActivity would cause a crash. This was because PaymentLauncherConfirmationActivity was launching a coroutine in the onCreate and would get called whenever there is a configuration change (e.g. screen rotation) and would emit a JobCancellationException. This exception was trying to be serialized as PaymentResult.Failed, but the exception contains a Job which cannot be serialized, causing a crash.

To fix this issue, we make sure that PaymentLauncherViewModel starts the coroutine so that it can survive configuration changes. However, additional changes needed to be made. The activity would also lose the ability to listen to the ActivityResultCallback after configuration change. This was fixed by adding a register method on the ViewModel.

Motivation

This issue will break all landscape mode PaymentLauncher confirmations.

Testing

  • Added tests
  • Modified tests
  • Manually verified

Screenshots

Before

crash.mov

After

landscapepaymentlauncher.mov

Changelog

@github-actions
Copy link
Contributor

Diffuse output:

@jameswoo-stripe jameswoo-stripe force-pushed the jameswoo/payment-launcher-configuration-change branch from e1538a7 to a02c12f Compare March 25, 2022 23:54
Copy link
Contributor

@skyler-stripe skyler-stripe left a comment

Choose a reason for hiding this comment

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

LGTM. Before approving though, did you check to see if this works with dont keep activity on? Just want to make sure the payment doesn't try to do through twice and that it restores and redirects back correctly after being killed.

Assigning Chen explicitly so he can take a look too.

/**
* Set when activity calls [register]. Used to redirect back to calling activity.
*/
private var authActivityStarterHost: AuthActivityStarterHost? = null
Copy link
Collaborator

@carlosmuvi-stripe carlosmuvi-stripe Mar 28, 2022

Choose a reason for hiding this comment

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

This keeps a reference to the activity internally. A viewModel cannot hold a reference to an Activity / any other view.

The Activity / Fragment gets destroyed and recreated on every configuration change, and a new instance gets the same instance of the ViewModel. Keeping a reference to the activity would prevent it from getting GCed here.

image

Copy link
Collaborator

Choose a reason for hiding this comment

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

Update on this - Looks like we were already leaking by keeping an activity reference on the viewModel, so this seems out of the scope of this bugfix. Unblocking.

@carlosmuvi-stripe carlosmuvi-stripe dismissed their stale review March 28, 2022 18:18

Blocked originally due to an existing memory leak.

skyler-stripe
skyler-stripe previously approved these changes Mar 29, 2022
@jameswoo-stripe jameswoo-stripe force-pushed the jameswoo/payment-launcher-configuration-change branch from a02c12f to ab1b8d4 Compare March 29, 2022 19:11
@jameswoo-stripe jameswoo-stripe requested review from skyler-stripe and carlosmuvi-stripe and removed request for carlosmuvi-stripe March 29, 2022 19:12
@jameswoo-stripe
Copy link
Contributor Author

LGTM. Before approving though, did you check to see if this works with dont keep activity on? Just want to make sure the payment doesn't try to do through twice and that it restores and redirects back correctly after being killed.

Assigning Chen explicitly so he can take a look too.

Checked DKA, works as expected. Thanks!

Copy link
Collaborator

@carlosmuvi-stripe carlosmuvi-stripe left a comment

Choose a reason for hiding this comment

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

Unlocking this bugfix, but proposing a follow-up to avoid potential OOMs due to screen rotations.

Comment on lines 139 to 125
authActivityStarterHost?.let {
authenticatorRegistry.getAuthenticator(intent).authenticate(
it,
intent,
apiRequestOptionsProvider.get()
)
}
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

looks like we're keeping a reference to authActivityStarterHost just to be able to run this logic right?

We're unlocking rotations with this PR, meaning every time the user rotates we're stacking an activity in memory that cannot be GCed, eventually running into an OOM / crash.

An important follow-up of this would be:

  • Move authActivityStarterHost to activity
  • Move this logic to the activity Communicate from viewModel to activity via a SharedFlow or LiveData<Event>

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 to this. I'm not too thrilled about holding on to this AuthActivityStarterHost in the view model. I think flow is the way to go. We've been using those over LiveData.

skyler-stripe
skyler-stripe previously approved these changes Mar 29, 2022
@jameswoo-stripe jameswoo-stripe force-pushed the jameswoo/payment-launcher-configuration-change branch 2 times, most recently from aae1069 to 1c0432a Compare March 30, 2022 16:13
@jameswoo-stripe jameswoo-stripe force-pushed the jameswoo/payment-launcher-configuration-change branch from 1c0432a to 9c940ea Compare March 30, 2022 16:49
Copy link
Collaborator

@carlosmuvi-stripe carlosmuvi-stripe left a comment

Choose a reason for hiding this comment

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

LGTM, not holding an activity reference 👏

@jameswoo-stripe jameswoo-stripe merged commit 52ed246 into master Mar 30, 2022
@jameswoo-stripe jameswoo-stripe deleted the jameswoo/payment-launcher-configuration-change branch March 30, 2022 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants