Xamarin.Auth: Browser doesn't close due to WebAuthenticatorNativeBrowserActivity has leaked ServiceConnection

Xamarin.Auth Issue

Version

  • nuget version = 1.6.0.1
  • Xamarin.Forms = 2.5.0.280555
  • Xamarin.Android.Support.CustomTabs = 27.0.2

Steps to reproduce

  1. Create a Xamarin Forms .netstandard project
  2. Included the interceptor activity in my android project:
protected override void OnCreate(Bundle savedInstanceState)
{
       base.OnCreate(savedInstanceState);

       var uri = new Uri(Intent.Data.ToString());

       AuthenticateState.oAuth2.OnPageLoading(uri);
       Finish();
       return;
}
  1. Initialized the OAuth2Authenticator with isUsingNativeUI = true

Platform:

  • [] .NET version = netstandard 2.0
  • [] Android SDK= 8.1

Expected behaviour

  1. After successfully login, the login browser should close
  2. With Xamarin.Auth 1.5.0.3, it works perfectly fine - the browser closed after successfully login - but not in 1.6.0.1
  3. The authenticator Completed event is raised successfully with IsAuthenticated = true
  4. No error is trapped in the authenticator 'Error` event

Actual behaviour

The login browser doesn’t close with the following debug message:

03-24 11:09:43.253 E/ActivityThread(15848): Activity md5719e249934bca077b67d8f77dc6da002.WebAuthenticatorNativeBrowserActivity has leaked ServiceConnection md59f70a99687498e7ba187118950981d26.CustomTabsServiceConnectionImpl@319ba3f8 that was originally bound here 03-24 11:09:43.253 E/ActivityThread(15848): android.app.ServiceConnectionLeaked: Activity md5719e249934bca077b67d8f77dc6da002.WebAuthenticatorNativeBrowserActivity has leaked ServiceConnection md59f70a99687498e7ba187118950981d26.CustomTabsServiceConnectionImpl@319ba3f8 that was originally bound here 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1134) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1028) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:2155) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ContextImpl.bindService(ContextImpl.java:2138) 03-24 11:09:43.253 E/ActivityThread(15848): at android.content.ContextWrapper.bindService(ContextWrapper.java:559) 03-24 11:09:43.253 E/ActivityThread(15848): at android.support.customtabs.CustomTabsClient.bindCustomTabsService(CustomTabsClient.java:71) 03-24 11:09:43.253 E/ActivityThread(15848): at md5719e249934bca077b67d8f77dc6da002.WebAuthenticatorNativeBrowserActivity.n_onCreate(Native Method) 03-24 11:09:43.253 E/ActivityThread(15848): at md5719e249934bca077b67d8f77dc6da002.WebAuthenticatorNativeBrowserActivity.onCreate(WebAuthenticatorNativeBrowserActivity.java:34) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.Activity.performCreate(Activity.java:6289) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ActivityThread.access$900(ActivityThread.java:177) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449) 03-24 11:09:43.253 E/ActivityThread(15848): at android.os.Handler.dispatchMessage(Handler.java:102) 03-24 11:09:43.253 E/ActivityThread(15848): at android.os.Looper.loop(Looper.java:145) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ActivityThread.main(ActivityThread.java:5951) 03-24 11:09:43.253 E/ActivityThread(15848): at java.lang.reflect.Method.invoke(Native Method) 03-24 11:09:43.253 E/ActivityThread(15848): at java.lang.reflect.Method.invoke(Method.java:372) 03-24 11:09:43.253 E/ActivityThread(15848): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 03-24 11:09:43.253 E/ActivityThread(15848): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 03-24 11:09:43.253 E/ActivityThread(15848): Activity md5719e249934bca077b67d8f77dc6da002.WebAuthenticatorNativeBrowserActivity has leaked ServiceConnection md59f70a99687498e7ba187118950981d26.CustomTabsServiceConnectionImpl@385ae0c2 that was originally bound here 03-24 11:09:43.253 E/ActivityThread(15848): android.app.ServiceConnectionLeaked: Activity md5719e249934bca077b67d8f77dc6da002.WebAuthenticatorNativeBrowserActivity has leaked ServiceConnection md59f70a99687498e7ba187118950981d26.CustomTabsServiceConnectionImpl@385ae0c2 that was originally bound here 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1134) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1028) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:2155) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ContextImpl.bindService(ContextImpl.java:2138) 03-24 11:09:43.253 E/ActivityThread(15848): at android.content.ContextWrapper.bindService(ContextWrapper.java:559) 03-24 11:09:43.253 E/ActivityThread(15848): at android.support.customtabs.CustomTabsClient.bindCustomTabsService(CustomTabsClient.java:71) 03-24 11:09:43.253 E/ActivityThread(15848): at md5719e249934bca077b67d8f77dc6da002.WebAuthenticatorNativeBrowserActivity.n_onCreate(Native Method) 03-24 11:09:43.253 E/ActivityThread(15848): at md5719e249934bca077b67d8f77dc6da002.WebAuthenticatorNativeBrowserActivity.onCreate(WebAuthenticatorNativeBrowserActivity.java:34) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.Activity.performCreate(Activity.java:6289) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ActivityThread.access$900(ActivityThread.java:177) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449) 03-24 11:09:43.253 E/ActivityThread(15848): at android.os.Handler.dispatchMessage(Handler.java:102) 03-24 11:09:43.253 E/ActivityThread(15848): at android.os.Looper.loop(Looper.java:145) 03-24 11:09:43.253 E/ActivityThread(15848): at android.app.ActivityThread.main(ActivityThread.java:5951) 03-24 11:09:43.253 E/ActivityThread(15848): at java.lang.reflect.Method.invoke(Native Method) 03-24 11:09:43.253 E/ActivityThread(15848): at java.lang.reflect.Method.invoke(Method.java:372) 03-24 11:09:43.253 E/ActivityThread(15848): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 03-24 11:09:43.253 E/ActivityThread(15848): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 9
  • Comments: 39 (3 by maintainers)

Most upvoted comments

Still happening with Xamarin.Android.Support.CustomTabs v28.0.0 and Xamarin.Auth 1.6.0.4 on Android. Does not happen with Xamarin.Auth 1.5.0.3 on Android. Also works with Xamarin.Auth 1.6.0.4 on iOS.

Please re-open this issue.

Seems to be resolved, closing

I guess no solution yet. My solution is to revert back to 1.5.0.3. I’m just a bit surprise that this is not seem as a critical bug, and coming to 4 mths without a fix.

@newky2k Not fixed. @samih7 was talking about a MissingMethodException but the OP posted about a leaked ServiceConnection which is still occurring.

I think this repository is as good as dead. Like @stealthcold , we too starting to implement native login

How is this issue closed when it’s still occurring with newest dependencies?

I am also facing issue of non closure of Custom Tabs with Xamarin.Auth 1.7.0 and Xamarin.Androd.support.customtabs 28.0.0.1. But here is link which helped me to close custom tab

(Suggested by EdHubbel ) Change Interceptor Activity code from below

protected override void OnCreate(Bundle savedInstanceState)
{
  base.OnCreate(savedInstanceState);

  global::Android.Net.Uri uri_android = Intent.Data;

  Uri uri_netfx = new Uri(uri_android.ToString());

  // load redirect_url Page
  AuthenticationState.Authenticator.OnPageLoading(uri_netfx);

  this.Finish();

  return;
}

to

protected override void OnCreate(Bundle savedInstanceState)
{
  base.OnCreate(savedInstanceState);

  global::Android.Net.Uri uri_android = Intent.Data;

  Uri uri_netfx = new Uri(uri_android.ToString());

  // load redirect_url Page
  AuthenticationState.Authenticator.OnPageLoading(uri_netfx);

  var intent = new Intent(this, typeof(MainActivity));
  intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
  StartActivity(intent);

  this.Finish();

  return;
}

how is this thing going on? i still encounter this even with latest 1.7

ActivityCustomUrlSchemeInterceptor not hitting at all

Hi,

I’m using this workaround for this issue. Basically, do not use the standard presenter: OAuthLoginPresenter presenter = new OAuthLoginPresenter(); presenter.Login(authenticator); But use your own code to create the Chrome Custom Tabs, construct the OAuth2 url authentication yourself, and call them using the Chrome Custom Tabs. In Core project: string url = "https://accounts.google.com/o/oauth2/v2/auth?scope=email+profile&response_type=code&redirect_uri=[your_reversed_client_id]:/oauth2redirect&client_id=[your_client_id]&access_type=offline"; ICustomTabsManager customTabsManager = DependencyService.Get<ICustomTabsManager>(); customTabsManager.LaunchUrl(url); The ICustomTabsManager is the interface defined in Core project, which will have its own implementation in Android and iOS projects, and then it get called using the DependencyService from Core project.

The Android implementation looks like this: void ICustomTabsManager.LaunchUrl(string url) { Android.Support.CustomTabs.CustomTabsIntent customTabsIntent = new Android.Support.CustomTabs.CustomTabsIntent.Builder().Build(); customTabsIntent.Intent.SetFlags(ActivityFlags.NoHistory | ActivityFlags.ClearTop); Activity activity = CrossCurrentActivity.Current.Activity; customTabsIntent.LaunchUrl(activity, Android.Net.Uri.Parse(url)); }

And then, in the interceptor activity, use the same code from Xamarin.Auth as usual. The returned Intent.Data is still compatible with the OnPageLoading method of the WebAuthenticator (it still raised the Completed event like the usual Xamarin.Auth. And, the Custom Tabs will be closed / replaced with the MainActivity.

Btw, the interceptor activity Launch Mode is SingleTop, and the main activity Launch Mode is SingleTask.

I was able to get my Droid project to close the tabs, but I believe I still get the memory leak error. Using 1.6.0.1. I found this link via Ed Hubble - https://forums.xamarin.com/discussion/100286/xamarin-auth-android-chrome-custom-tabs-doesnt-close-on-redirect

In your InterceptorActivity you can add these lines after calling OnPageLoading and before Finish()

var intent = new Intent(this, typeof(MainActivity)); intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop); StartActivity(intent);`

Please re-open, I’m also facing this issue.

@hvaughan3 27.0.2.1

I got it to work correctly with 1.5.0.3 and .NET Standard. If you get a MissingMethodException in the CustomTabsConfiguration class with the 1.5.0.3 on .NET Standard, it probably means that the Android Support Libraries referenced by this version are too old to comply with the Xamarin.Android version you’re using.

  • To fix this, just add the lastest version of the Xamarin.Android.Support.CustomTabs NuGet package explicitly to your Xamarin.Android project.

NB: I still have the Xamarin.Auth.CustomTabsConfiguration.CustomTabsClosingMessage = null; instruction when initializing to avoid the weird Android Toast when getting back to the application.