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

"Unable to auto-initialize connection" message on Android (and some suggestions) #2265

Closed
cwhenderson20 opened this issue Mar 1, 2023 · 8 comments
Labels
🤖 android Related to android 🐛 bug Something isn't working

Comments

@cwhenderson20
Copy link

Description

First, thank you all of the work you do on this module! It's immensely helpful! Unfortunately, I am seeing thousands of promise rejections from Android devices with the message Unable to auto-initialize connection. I noticed that others have reported this (#2183, #1958), but there's not enough information in there to help me figure out what's going on. For what it's worth, I believe this is mostly just a warning message. I haven't seen this interfering with purchasing rates on Android.

For context, I am using the withIAPContext HOC at the top level of my app, and then a hook that attaches an onPurchaseUpdated listener in order to act on changes to the latest purchase.

Simplified, that hook looks like this:

import { purchaseUpdatedListener } from 'react-native-iap'

function usePurchaseUpdatedListener() {
  const { connected } = useIAP();

  useEffect(() => {
    if (!connected) {
      return;
    }

    async function onPurchaseUpdated(purchase: Purchase) {
      // do some async things like fetching data here...
    }

    const purchaseUpdatedSubscription = purchaseUpdatedListener(onPurchaseUpdated);

    return () => {
      purchaseUpdatedSubscription.remove();
    }
  }, [connected])
}

And here's the stacktrace I see in Bugsnag:

Error Unable to auto-initialize connection 
    node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:105:50 promiseMethodWrapper
    node_modules/react-native-iap/src/eventEmitter.ts:62:4 purchaseUpdatedListener
    src/data/billing/hooks.ts:218:6 anonymous
    ...

image

So I looked into it a bit, and purchaseUpdatedListener comes from eventEmitter.ts. The relevant lines are:

if (isAndroid) {
  getAndroidModule().startListening();
}

…which then calls startListening from RNIapModule.kt. That calls sendUnconsumedPurchases, which calls ensureConnection, which is where this Unable to auto-initialize connection message originates. Ultimately, I don't understand how all of the native code works well enough to make a diagnosis to the root cause, but one thing that I find somewhat strange is that the getAndroidModule.startListening() method supposedly returns a Promise, but that I, on the consumer side, have no way to catch errors that may be thrown from that Promise. I suspect this is why the error goes uncaught.

Maybe the code should be modified to either handle the potential rejection internally (if it really is innocuous):

if (isAndroid) {
  getAndroidModule().startListening().catch(e => console.warn(e));
}

You could also bubble it up to the user, but given the fact that the purchaseUpdatedListener function already returns an EventEmitter subscription, maybe an errorCallback as a second parameter that would be called in the case of an error would work:

export const purchaseUpdatedListener = (
  listener: (event: Purchase) => void,
  errorCallback?: (error: unknown) => void,
) => {
  //... 
  if (isAndroid) {
    getAndroidModule().startListening().catch(error => {
      if (errorCallback) {
        errorCallback(error);
      } else {
        throw error;
      }
    });
  }

  return emitterSubscription;
};

// ...

const purchaseUpdateSubscription =
      purchaseUpdatedListener(onPurchaseUpdated , (error) => console.warn(error));

Though this certainly has its drawbacks as well (mixing three separate async paradigms [events, promises, callback]).

Again, thank you for this module, it's been instrumental in offering subscriptions to users. I'm interested to hear your thoughts on the above.

Environment:

  • react-native-iap: 12.02
  • react-native: 0.70.6
  • Platform: Android
@andresesfm andresesfm added 🤖 android Related to android ⌛️ legacy Getting old and removed ⌛️ legacy Getting old labels Mar 1, 2023
@andresesfm
Copy link
Collaborator

It makes sense to have the option to catch the error or ignore it. This is a bug

@andresesfm andresesfm added the 🐛 bug Something isn't working label Mar 1, 2023
@cwhenderson20
Copy link
Author

Thanks for the reply! I'm most curious why it's happening in the first place, given that I seem to be following all of the guidelines around connection status, but having the option to silently ignore it would be nice.

@andresesfm
Copy link
Collaborator

How about something like

export const purchaseUpdatedListener = (
  listener: (event: Purchase) => void,
  handleAndroidConnectionFailure: (reason: any) => any = () => {},
) => {
  //...
  if (isAndroid) {
    getAndroidModule().startListening().catch(handleAndroidConnectionFailure);
  }
//...
};

In that way, it has a default value so it's not a breaking change

@cwhenderson20
Copy link
Author

cwhenderson20 commented Mar 1, 2023

Sure, that works. Though I think that re-throwing the error from inside the Promise technically retains the previous behavior, which is that the error will go uncaught rather than swallowed.

@andresesfm
Copy link
Collaborator

Upon further research, I went with: #2268.

@andresesfm
Copy link
Collaborator

Released as 12.8.0 Thank you @cwhenderson20. Please let us know if any other issues

@ramakula
Copy link

This issue is back again on version 12.13.0. We had to rollback because if impacted our android subscriptions, we had 100s instances of this error: we ended up rolling back to 12.10.8, this is the version .

There is no specific device or android version combination.

Unable to auto-initialize connection:

Screenshot 2024-04-26 at 10 31 34 Screenshot 2024-04-26 at 10 36 36 Screenshot 2024-04-26 at 10 36 22

@moritzkarliczek
Copy link

Same for us. We also experience this issue again with version 12.13.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖 android Related to android 🐛 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants