Bug 23957 - HttpClient throws System.Net.ProtocolViolationException and System.InvalidOperationException exceptions
Summary: HttpClient throws System.Net.ProtocolViolationException and System.InvalidOpe...
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: master
Hardware: Macintosh Mac OS
: High major
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-10-21 10:23 UTC by Michael Stonis
Modified: 2016-12-08 07:35 UTC (History)
11 users (show)

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Michael Stonis 2014-10-21 10:23:47 UTC
When making some calls using the HttpClient, we will receive ProtocolViolationExceptions and InvalidOperationException in the application in quick succession. These issues will usually occur when multiple HttpClient objects have been created and are being used simultaneously. 

The same code worked without issue until upgrading to Mono 3.10, Xamarin Studio 5.5 and iOS 8+ SDK.

To work around the issue, we can delay and recall the same method again and it will be successful.

Exception 1
---------------
System.Net.ProtocolViolationException - The number of bytes to be written is greater than the specified ContentLength.

Stacktrace 1
---------------

at System.Net.WebConnectionStream.CheckWriteOverflow (Int64 contentLength, Int64 totalWritten, Int64 size) [0x00000] in <filename unknown>:0 
  at System.Net.WebConnectionStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.TaskFactory`1[System.Object].FromAsyncBeginEnd[Byte[],Int32,Int32] (System.Func`6 beginMethod, System.Func`2 endMethod, System.Byte[] arg1, Int32 arg2, Int32 arg3, System.Object state, TaskCreationOptions creationOptions) [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.TaskFactory.FromAsync[Byte[],Int32,Int32] (System.Func`6 beginMethod, System.Action`1 endMethod, System.Byte[] arg1, Int32 arg2, Int32 arg3, System.Object state, TaskCreationOptions creationOptions) [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.TaskFactory.FromAsync[Byte[],Int32,Int32] (System.Func`6 beginMethod, System.Action`1 endMethod, System.Byte[] arg1, Int32 arg2, Int32 arg3, System.Object state) [0x00000] in <filename unknown>:0 
  at System.IO.Stream.WriteAsync (System.Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) [0x00000] in <filename unknown>:0 
  at System.IO.Stream.WriteAsync (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at System.Net.Http.ByteArrayContent.SerializeToStreamAsync (System.IO.Stream stream, System.Net.TransportContext context) [0x00000] in <filename unknown>:0 
  at System.Net.Http.HttpContent.CopyToAsync (System.IO.Stream stream, System.Net.TransportContext context) [0x00000] in <filename unknown>:0 
  at System.Net.Http.HttpContent.CopyToAsync (System.IO.Stream stream) [0x00000] in <filename unknown>:0 
  at System.Net.Http.HttpClientHandler+<SendAsync>c__async0.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at System.Net.Http.HttpClient+<SendAsyncWorker>c__async0.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at AMC.Contacts.Core.Services.ContactManagement.ContactManagementRepository+<GetContactsAsync>c__async0.MoveNext () [0x00000] in <filename unknown>:0 


Exception 2
---------------
System.InvalidOperationException - Cannot re-call start of asynchronous method while a previous call is still in progress.

Stacktrace 2
---------------
  at System.Net.HttpWebRequest.BeginGetResponse (System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.TaskFactory`1[System.Net.WebResponse].FromAsyncBeginEnd (System.Func`3 beginMethod, System.Func`2 endMethod, System.Object state, TaskCreationOptions creationOptions) [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.TaskFactory`1[System.Net.WebResponse].FromAsync (System.Func`3 beginMethod, System.Func`2 endMethod, System.Object state, TaskCreationOptions creationOptions) [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.TaskFactory`1[System.Net.WebResponse].FromAsync (System.Func`3 beginMethod, System.Func`2 endMethod, System.Object state) [0x00000] in <filename unknown>:0 
  at System.Net.WebRequest.GetResponseAsync () [0x00000] in <filename unknown>:0 
  at System.Net.Http.HttpClientHandler+<SendAsync>c__async0.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at System.Net.Http.HttpClient+<SendAsyncWorker>c__async0.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at AMC.Contacts.Core.Services.ContactManagement.ContactManagementRepository+<GetContactsAsync>c__async0.MoveNext () [0x00000] in <filename unknown>:0 



Configuration Information
=== Xamarin Studio ===

Version 5.5.2 (build 3)
Installation UUID: aea7a79d-01c5-4c5b-ba27-04b485961c0c
Runtime:
	Mono 3.10.0 ((detached/e204655)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000023

=== Apple Developer Tools ===

Xcode 6.0.1 (6528)
Build 6A317

=== Xamarin.Mac ===

Version:

=== Xamarin.Android ===

Not Installed

=== Xamarin.iOS ===

Version: 8.2.0.207 (Business Edition)
Hash: 3bf072d
Branch: 
Build date: 2014-10-15 21:44:26-0400

=== Build Information ===

Release ID: 505020003
Git revision: 6d4c806e383ba366b15fbccca9ddc1ba94f8d5ad
Build date: 2014-10-15 15:00:36-04
Xamarin addins: 069ddd29bb70a42238142eee9bac21a5e4b2f9f9

=== Operating System ===

Mac OS X 10.9.4
Darwin administrators-Mac-mini.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 1 Parmendra Kumar 2014-10-21 13:42:40 UTC
I have checked this issue but I am not able to reproduce this issue, please check the screencast and let me know If I missed anything. 

Screencast: http://www.screencast.com/t/oOgIR9gAXRg

Could you Please provide us sample project or steps that can be helpful to reproduce this issue at my end. 

Environment Info:
=== Xamarin Studio ===

Version 5.5.2 (build 3)
Installation UUID: 1a096c6f-0678-402e-89b2-a2c10f7e80e4
Runtime:
	Mono 3.10.0 ((detached/e204655)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000023

=== Apple Developer Tools ===

Xcode 6.0.1 (6528)
Build 6A317

=== Xamarin.iOS ===

Version: 8.2.0.207 (Business Edition)
Hash: 3bf072d
Branch: 
Build date: 2014-10-15 21:44:26-0400

=== Xamarin.Mac ===

Version: 1.10.0.13 (Business Edition)

=== Build Information ===

Release ID: 505020003
Git revision: 6d4c806e383ba366b15fbccca9ddc1ba94f8d5ad
Build date: 2014-10-15 15:00:36-04
Xamarin addins: 069ddd29bb70a42238142eee9bac21a5e4b2f9f9

=== Operating System ===

Mac OS X 10.9.4
Darwin ShrutiMac.local 13.3.0 Darwin Kernel Version 13.3.0
Comment 2 TeunVR 2014-11-28 07:19:56 UTC
I created a testpoject in which you should be able to reproduce it really easy:
https://github.com/TeunVR/HttpClientCrash

It crashes in iOS device and simulator and on Android simulator (did not test device yet).

When you start with >1 postAsync "at once" it will most of the time crash with the invalid contentlength exception.

When try to do >maximum simultaneous requests at once it will crash with InvalidOperationException or TaskCancelledException

Besides this it looks like Android is not performing the requests in parallel, only 1 at a time.

=== Xamarin Studio ===

Version 5.5.4 (build 15)
Installation UUID: d9774cfc-d739-4fa7-a266-ced558b2ecc2
Runtime:
	Mono 3.10.0 ((detached/92c4884)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000031

=== Xamarin.Android ===

Version: 4.20.0.28 (Trial Edition)
Android SDK: /Users/tvanrun/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		4.0.3 (API level 15)
		4.4   (API level 19)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)

=== Apple Developer Tools ===

Xcode 6.1 (6604)
Build 6A1052d

=== Xamarin.Mac ===

Version:

=== Xamarin.iOS ===

Version: 8.4.0.43 (Trial Edition)
Hash: 840a925
Branch: 
Build date: 2014-11-16 21:03:22-0500

=== Build Information ===

Release ID: 505040015
Git revision: f93940a35458a18052f1a25e106e62ca970d9c40
Build date: 2014-11-19 15:32:41-05
Xamarin addins: dc23cbd91a3a0e1d326328e1229e86c942a49ec8

=== Operating System ===

Mac OS X 10.10.1
Darwin Teuns-MacBook-Pro.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 3 Parmendra Kumar 2014-12-04 05:49:17 UTC
Thanks TeunVR,

I have checked this issue with attached sample in comment 2 and I am getting some error message. 
Could you Please check the screencast and application output and let we now if you also getting same issue or I have missed anything.

Screencast:http://www.screencast.com/t/uNsqent3

Application Output:https://gist.github.com/Parmendrak/37f8c1664c01b9017838

Environment info:

Xamarin Studio 5.5.4 (build 15)
Mono 3.10.0 ((detached/92c4884)
Xcode 6.1 (6602)
Xamarin.iOS : 8.4.0.43 (Business Edition)
Xamarin.Mac : 1.10.0.18 (Business Edition)
Operating System : Mac OS X 10.9.4
Comment 4 TeunVR 2014-12-04 06:06:55 UTC
No, the error you are seeing is because it cannot connect to the test-server.
Please start the NodeJS server as described in the github readme.
And make sure you have the correct address in line 18 of MyClass.cs:
httpClient.BaseAddress=new Uri ("http://127.0.0.1:3000/");
Comment 5 TeunVR 2014-12-04 06:11:52 UTC
Also make sure that when testing on device (ios/android) or android-emulator you need to set the real ipaddress of your nodejs-server there.
Comment 6 Bob 2014-12-10 13:28:58 UTC
Any update on this. I am encountering the same issues and its blocking go-live in the app store.
Comment 8 Allan Ritchie 2015-01-13 11:25:55 UTC
We were using a fresh httpclient with each call without issue on android, however, this was deadlocking on iOS on occasion.  We tried switching to a single httpclient from all calls and the iOS deadlock is far less frequent but now on android we're getting Exception #2
Comment 9 Bob 2015-01-13 12:24:20 UTC
Allan...try using the Modernhttp component in you project.  It fixed 99% of my issues.  Xamarin is working on a fix to the underlying issue but its complicated and could take a bit

-bob
Comment 10 Allan Ritchie 2015-01-13 14:00:06 UTC
Hi Bob.  It didn't help unfortunately.  I'm still getting Exception #1 & #2 randomly.  One of our apps makes about 6 async http calls as it builds its cache.  It is 50/50 chance on android that one of these exceptions comes up.  I am using the latest release as of yesterday.
Comment 11 Bob 2015-01-13 14:01:56 UTC
Allan..are you using Azure mobile services as well?
Comment 12 Allan Ritchie 2015-01-13 14:09:01 UTC
Nope.  Straight to httpclient with modernhttpclient.

It seems I'll have to run two different setups
1) iOS - single httpclient with many threads hitting it.  This fixes some sort of deadlock occurring on iOS.  I am using ConfigureAwait(false), but the network request itself is coming back, but the task was never completing.

2) Android - HttpClient instance per call.  I was not getting the mentioned exceptions with this setup.  However, you are supposed to use one instance as per the recommendation.

Incidentally, the latest release also breaks reading content-disposition headers in both iOS & Android (I have to write another defect shortly) :P
Comment 13 Grigory (Playtika) 2015-01-21 06:07:04 UTC
Fix this ASAP plz. This is a blocker for almost all apps/games release on Xamarin.
Comment 14 Bradley Locke 2015-04-01 06:21:44 UTC
I was regularly getting this error within my app.

Exception 1
---------------
System.Net.ProtocolViolationException - The number of bytes to be written is
greater than the specified ContentLength.

Adding ModernHTTPClient appears to have eliminated the issues.
Comment 15 Marek Safar 2015-05-15 13:12:45 UTC
How can I reproduce the issues. Attached repro is missing files and fails for me with

> node ./bin/www


module.js:340
    throw err;

during start
Comment 16 TeunVR 2015-05-15 15:24:49 UTC
Sorry, my gitignore file was ignoring the bin-folder :)
I just added it to the repository.
Note that also the readme is changed: 'npm install' instead of 'npm init'
Comment 17 Marek Safar 2015-05-18 03:46:43 UTC
Everything seems to work for me using Xamarin.iOS 8.10.1
Comment 18 cheansiong 2016-12-08 07:35:09 UTC
Exactly I'm having this issue right now with the following version in Xamarin.Android.

Xamarin: 4.2.0.719
Xamarin.Android: 7.0.1.6

Is this issues really fixed?