This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 51774 - AndroidClientHandler throws NetworkOnMainThreadException on Cancelation via cancelation delegate call
Summary: AndroidClientHandler throws NetworkOnMainThreadException on Cancelation via c...
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries (show other bugs)
Version: 7.1 (C9)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Habersack
URL:
Depends on:
Blocks:
 
Reported: 2017-01-26 04:02 UTC by chorsfield
Modified: 2017-02-14 20:38 UTC (History)
2 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description chorsfield 2017-01-26 04:02:17 UTC
Cycle 9 and Master Branches have this problem.

This change https://github.com/xamarin/xamarin-android/commit/5055a209de3f782468e0b0f1e7526e32ec2bd3bd
Resulted in cancelation tokens calling httpConnection.Disconnect();

httpConnection.Disconnect(); is called directly from DoProcessRequest and from the cancelation delegate cancellationToken.Register(httpConnection.Disconnect);

The problem is with the cancelation delegate.

All the calls to httpConnection are run in Task.Run to avoid the Android NetworkOnMainThreadException Exception. 

In the case of cancellationToken.Register(httpConnection.Disconnect) it is not and can run on the UI Thread, this results in a crash on Android 6 (M) Marshmellow. 

This does not seem to happen on other versions of Android.  If you invoke the cancelation token from the UI thread, then this will result in this httpConnection.Disconnect getting called on the UI thread and result in crash in this code base. 

The rest of the code protects against this with the Task.Run ConfigureAwait(false);

https://github.com/xamarin/xamarin-android/blob/master/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs
Comment 1 chorsfield 2017-01-26 04:06:04 UTC
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.disconnect(DelegatingHttpsURLConnection.java: 93)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.disconnect(HttpsURLConnectionImpl.java)(: )
at 

Stack
android.os.NetworkOnMainThreadException:
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java: 1273)
at com.android.org.conscrypt.OpenSSLSocketImpl.shutdownAndFreeSslNative(OpenSSLSocketImpl.java: 1131)
at com.android.org.conscrypt.OpenSSLSocketImpl.close(OpenSSLSocketImpl.java: 1126)
at com.android.okhttp.Connection.closeIfOwnedBy(Connection.java: 132)
at com.android.okhttp.OkHttpClient$1.closeIfOwnedBy(OkHttpClient.java: 75)
at com.android.okhttp.internal.http.HttpConnection.closeIfOwnedBy(HttpConnection.java: 137)
at com.android.okhttp.internal.http.HttpTransport.disconnect(HttpTransport.java: 135)
at com.android.okhttp.internal.http.HttpEngine.disconnect(HttpEngine.java: 578)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.disconnect(HttpURLConnectionImpl.java: 122)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.disconnect(DelegatingHttpsURLConnection.java: 93)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.disconnect(HttpsURLConnectionImpl.java)(: )
at mono.android.support.v7.widget.SearchView_OnQueryTextListenerImplementor.n_onQueryTextChange(Native Method: )
at mono.android.support.v7.widget.SearchView_OnQueryTextListenerImplementor.onQueryTextChange(SearchView_OnQueryTextListenerImplementor.java: 31)
at android.support.v7.widget.SearchView.onTextChanged(SearchView.java: 1150)
at android.support.v7.widget.SearchView.access$2000(SearchView.java: 103)
at android.support.v7.widget.SearchView$12.onTextChanged(SearchView.java: 1680)
at android.widget.TextView.sendOnTextChanged(TextView.java: 7988)
at android.widget.TextView.setText(TextView.java: 4350)
at android.widget.TextView.setText(TextView.java: 4204)
at android.widget.TextView.setText(TextView.java: 4179)
at android.support.v7.widget.SearchView.onActionViewExpanded(SearchView.java: 1265)
at android.support.v7.widget.Toolbar$ExpandedActionViewMenuPresenter.expandItemActionView(Toolbar.java: 2026)
at android.support.v7.view.menu.MenuBuilder.expandItemActionView(MenuBuilder.java: 1323)
at android.support.v7.view.menu.MenuItemImpl.expandActionView(MenuItemImpl.java: 687)
Comment 2 Marek Habersack 2017-01-27 12:36:43 UTC
PR opened: https://github.com/xamarin/xamarin-android/pull/409
Comment 3 Marek Habersack 2017-02-14 20:38:13 UTC
Fixed in

  xamarin-android/master, commit c873c7ba6041d10def9206df6ca732a29accb11f
  monodroid/master, commit 30b2e6ec57d40131ab402c27a8ffeddbf08290a5

Note You need to log in before you can comment on or make changes to this bug.