Bug 10812 - HttpWebrequest.Create() throws exception when having nested HTTP request or multiple threads performing HTTP request at the same time.
Summary: HttpWebrequest.Create() throws exception when having nested HTTP request or ...
Status: RESOLVED FEATURE
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: unspecified
Hardware: PC All
: --- normal
Target Milestone: Untriaged
Assignee: Martin Baulig
URL:
Depends on:
Blocks:
 
Reported: 2013-03-01 09:03 UTC by Sze Siong
Modified: 2014-10-29 16:14 UTC (History)
8 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 GitHub or Developer Community 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 FEATURE

Description Sze Siong 2013-03-01 09:03:27 UTC
The mono implementation of HttpWebRequest throws the following exception when having nested HTTP request or multiple threads performing HTTP request at the same time.

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


MS implementation of HttpWebRequest class doesn't seem to have this problem
Comment 1 dango 2014-07-18 06:51:31 UTC
This code is affected by this error:

var request = (HttpWebRequest) WebRequest.Create(string.Format("{0}/Mobile/CommunityLookup/{1}", App.BaseUrl, code));
			request.Method = "GET";
			request.ContentType = "application/json";

			string responseJson;
			try
			{
				using (var response = await request.GetResponseAsync())
				{
					using (var reader = new StreamReader(response.GetResponseStream()))
					{
						responseJson = reader.ReadToEnd();
					}
				}
			}
			catch (Exception ex)
			{
				
				throw;
			}
Comment 2 Martin Baulig 2014-07-29 08:55:05 UTC
What's the error message that you're getting and do you have some sample Json?
Comment 3 softlion 2014-08-07 05:23:40 UTC
Got the same exception, when not using the NativeHttpHandler nuget
Comment 4 softlion 2014-08-07 05:29:41 UTC
Happens only when using an HttpClientHandler.

Multiple async calls at same time: NOK (error):

var client = new HttpClient();



Multiple async calls at same time: OK:

var client = new HttpClient(new NativeMessageHandler())
Comment 5 softlion 2014-08-07 05:30:14 UTC
OUPS the first line should have been deleted, the bug is not related to HttpClientHandler.
Comment 6 softlion 2014-08-07 05:33:06 UTC
Well this may not be a bug !

"Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are NOT guaranteed to be thread safe."

So httpclient is not thread safe and can not be used for concurrent calls.
Comment 7 Martin Baulig 2014-08-11 09:47:20 UTC
Same thing also applies to HttpWebRequest and HttpClient; instance members of these classes are not guaranteed to be thread-safe.

If you want to make multiple concurrent requests, then simply create a separate HttpClient instance for each of your threads.
Comment 8 T.J. Purtell 2014-09-06 00:47:47 UTC
There definitely seems to be a regression regression recently introduced in Xamarin/Mono recently that is causing this error to recur.  I have code that runs perfectly on .NET and previously perfectly (sans other mono bugs) with the mono Http handler.  Additionally, it runs correctly with ModernHttp substituted as the HttpClientHandler.  It does not run correctly with Xamarin Android 4.16.

If you check this out, you'll see that you are supposed to be able to run multiple concurrent requests with HttpClient.
http://blogs.msdn.com/b/henrikn/archive/2012/08/07/httpclient-httpclienthandler-and-httpwebrequesthandler.aspx

Even if I wrap the HttpClientHandler in a lock for when it issues requests, this error still occcurs.

>        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
>        {
>            lock (_Lock)
>            {
>                return base.SendAsync(request, cancellationToken);
>            }
>        }
Comment 9 softlion 2014-09-06 02:28:22 UTC
Be warn that NativeHttpHandler does not work anymore on latest Android AND ios 8 beta. On ios 8 it crashes. On Android it does not trigger http requests.

I removed it and created a separate HttpClient for each call. That works perfectly.
Comment 10 T.J. Purtell 2014-09-06 02:44:02 UTC
Is NativeHttpHandler == ModernHttpCleint from paulcbetts?

We shouldn't have to create a new HttpClient per request.  That is not the .NET design.

What version of Xamarin.Android does it fail on for you? 

I am using a modified version of NativeHttpClient branched off at January 2014: https://github.com/tpurtell/modernhttpclient

It seems to work still for 4.16 of Xamarin.Android.  Last time I checked 4.17 (about 2-3 weeks ago) it still worked.  I have not tested on iOS 8 beta yet though. :(
Comment 11 T.J. Purtell 2014-09-07 15:11:22 UTC
See this bug... https://bugzilla.xamarin.com/show_bug.cgi?id=22724