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...
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
Depends on:
Reported: 2017-01-26 04:02 UTC by chorsfield
Modified: 2017-02-14 20:38 UTC (History)
2 users (show)

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


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

This change
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);
Comment 1 chorsfield 2017-01-26 04:06:04 UTC
at 93)
at )

at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork( 1273)
at 1131)
at 1126)
at 132)
at$1.closeIfOwnedBy( 75)
at 137)
at 135)
at 578)
at 122)
at 93)
at )
at Method: )
at 31)
at 1150)
at$2000( 103)
at$12.onTextChanged( 1680)
at android.widget.TextView.sendOnTextChanged( 7988)
at android.widget.TextView.setText( 4350)
at android.widget.TextView.setText( 4204)
at android.widget.TextView.setText( 4179)
at 1265)
at$ExpandedActionViewMenuPresenter.expandItemActionView( 2026)
at 1323)
at 687)
Comment 2 Marek Habersack 2017-01-27 12:36:43 UTC
PR opened:
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.