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 issues with CustomerSession listeners #856

Merged
merged 2 commits into from Apr 9, 2019

Conversation

mshafrir-stripe
Copy link
Collaborator

Motivation

The previous implementation of CustomerSession listeners
had a reference to a single CustomerRetrievalListener and
SourceRetrievalListener. After a listener is called, it
would be nulled out.

This logic was buggy because it resulted in possible race
conditions in which two calls are both in flight, and the
first call that completes nulls out the listener for the
second.

Summary

The solution to this issue is to associate an operationId
(i.e. UUID) with each listener. This avoids the race condition
and ambiguity about which listener is being referenced.

Additionally, the listeners were being held with a strong
reference, which could lead to memory leaks if the listener
held a reference to an Activity.

Testing

Manually verified with samplestore and example apps.

**Motivation**
The previous implementation of `CustomerSession` listeners
had a reference to a single `CustomerRetrievalListener` and
`SourceRetrievalListener`. After a listener is called, it
would be nulled out.

This logic was buggy because it resulted in possible race
conditions in which two calls are both in flight, and the
first call that completes nulls out the listener for the
second.

**Summary**
The solution to this issue is to associate an `operationId`
(i.e. UUID) with each listener. This avoids the race condition
and ambiguity about which listener is being referenced.

Additionally, the listeners were being held with a strong
reference, which could lead to memory leaks if the listener
held a reference to an Activity.

**Testing**
Manually verified with samplestore and example apps.
@mshafrir-stripe mshafrir-stripe changed the base branch from refactor-customer-session to master April 9, 2019 15:47
@@ -327,19 +354,23 @@ long getCustomerCacheTime() {
private void addCustomerSource(
@NonNull final Context context,
@NonNull final CustomerEphemeralKey key,
@Nullable final String operationId,
Copy link

Choose a reason for hiding this comment

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

Makes sense to me to stick this argument on the end to match the API of the other functions

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@@ -81,8 +84,10 @@
@Nullable private Customer mCustomer;
private long mCustomerCacheTime;
@Nullable private Context mContext;
@Nullable private CustomerRetrievalListener mCustomerRetrievalListener;
@Nullable private SourceRetrievalListener mSourceRetrievalListener;
@NonNull private final Map<String, WeakReference<CustomerRetrievalListener>>
Copy link

@aphexcx aphexcx Apr 9, 2019

Choose a reason for hiding this comment

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

Is there really a need for weakreferences here? I'm concerned it would bring back that issue of listeners getting garbage collected, and I don't see these leaking any contexts

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These listeners can have references to Activity contexts (see PaymentMethodsActivity), so they should be WeakReferences to avoid memory leaks. We'll monitor new reports of GC issues.

@mshafrir-stripe mshafrir-stripe merged commit 2c44949 into master Apr 9, 2019
@mshafrir-stripe mshafrir-stripe deleted the customer-listeners branch April 9, 2019 18:55
@jemerick-stripe jemerick-stripe mentioned this pull request Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants