Bug 7200 - HtttpWebRequest returns result of a request in another thread
Summary: HtttpWebRequest returns result of a request in another thread
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 5.3.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Martin Baulig
: 7025 7308 ()
Depends on:
Reported: 2012-09-16 15:48 UTC by Philippe Leybaert
Modified: 2013-06-08 19:32 UTC (History)
15 users (show)

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

test.cs (1.06 KB, application/octet-stream)
2012-09-17 05:05 UTC, Rolf Bjarne Kvinge [MSFT]
Minimal test project demonstrating the described bug (6.08 KB, application/zip)
2012-09-17 15:46 UTC, Philippe Leybaert
Console test case (1.32 KB, application/octet-stream)
2012-09-26 17:53 UTC, Martin Baulig

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:

Description Philippe Leybaert 2012-09-16 15:48:09 UTC
I launch 2 threads, where each thread creates a HttpWebRequest. These threads are started at the same time (well, one millisecond apart). What happens is that the call to GetResponse() from the first HTTP request (url1) receives the contents of the second HTTP request (url2). When this happens, the second HTTP request times out after 30 seconds.

This doesn't happen every time, but it does happen 4 out of 5 times.

This is the code: (simplified to produce a working test case)

    public void Launch()
       Thread thread1 = new Thread(() => { GetData("http://url1"); });
       Thread thread2 = new Thread(() => { GetData("http://url2"); });

    public static void GetData(string url)
          XDocument xDoc;
          var webRequest = HttpWebRequest.Create(url);
          using (var response = webRequest.GetResponse())
             using (var stream = response.GetResponseStream())
                 xDoc = XDocument.Load(stream);
                 Logger.Default.Log("Result for {0}: {1}", url, xDoc);
       catch(Exception ex)
           Logger.Default.Log("Result for {0}: [Exception: {1}]", url, ex.Message);

This is the output (4 out of 5 times):

    Result for http://url1: <contents of http://url2>
    Result for http://url2: [Exception: timeout...]

When everything is ok (1 out of 5 times):

    Result for http://url1: <contents of http://url1>
    Result for http://url2: <contents of http://url2>
Comment 1 Rolf Bjarne Kvinge [MSFT] 2012-09-17 05:05:25 UTC
Created attachment 2550 [details]

I can't reproduce this, I tried with the following test case (compiling it as a desktop app in Mac, and in an iOS app too).

Can you provide a complete test case (with urls) I can try out?
Comment 2 Philippe Leybaert 2012-09-17 06:00:38 UTC
After some extra testing, it seems that this only occurs if there is a third simultaneous HTTP connection.

There's also a warning logged to the console: 
HttpWebRequest was added to the ConnectionGroup queue because the connection limit was reached

I'll try to create a standalone test app which reproduces this behavior.
Comment 3 Philippe Leybaert 2012-09-17 15:46:26 UTC
Created attachment 2559 [details]
Minimal test project demonstrating the described bug

This MonoTouch iPhone project demonstrates the bug. The project is an empty View-based app, and it launches 4 threads, each accessing a different URL.

So far I have been able to reproduce this every single time the application is launched.
Comment 4 Rolf Bjarne Kvinge [MSFT] 2012-09-17 18:46:04 UTC
I can reproduce this now.

The interesting part is that if I use the ThreadPool instead of creating separate threads, it seems to work just fine.
Comment 5 Matthijs Koopman 2012-09-18 05:40:47 UTC
We are facing the same issues in monodroid 4.2.5

Comment 6 Jared Reed 2012-09-19 15:26:47 UTC
I'm seeing the same issues on Mono for Android.
Comment 8 Martin Baulig 2012-09-26 17:53:59 UTC
Created attachment 2630 [details]
Console test case

This is Philippe Leybaert's test case as a console application.
Comment 9 Martin Baulig 2012-09-26 19:35:40 UTC
I have bisected and identified this as mono/mono-2-10 commit bfa23f8:
Comment 10 Martin Baulig 2012-09-26 19:56:07 UTC
This seems to be commit https://github.com/mono/mono/commit/9069d078374b48c6eb982ebd293ff085132de835 in master.

I did a search on bugzilla and couldn't find any bug that seemed related to this issue.

Marek, do you still remember what you tried to fix there, maybe still have a test case for it ?

The change on line 716 is causing the problem, so changing

+          Data.request = (HttpWebRequest) queue.Dequeue ();
+          SendRequest (Data.request);
717	 722	

back to

-          SendRequest ((HttpWebRequest) queue.Dequeue ());

fixes this.

I'll do this now, leaving your other changes from this commit in place.  Could you please have a quick look at that when you have time, I don't want to break your disposable changes.
Comment 11 Martin Baulig 2012-09-26 20:44:14 UTC
*** Bug 7308 has been marked as a duplicate of this bug. ***
Comment 12 Martin Baulig 2012-09-26 20:44:53 UTC
Fixed; mono/master commit 9a8ed89, mono-2-10 commit 0fa7cd1.
Comment 15 Marek Safar 2012-09-27 03:30:12 UTC
Martin, it was a fix for race in dispose. I don't remember exact details but it showed up on build boot from time to time.
Comment 16 Sebastien Pouliot 2012-09-28 15:16:57 UTC
MonoTouch 6.0.2 is available with this fix (beta channel at the moment). Please try it and re-open the bug if the issue is not completely fixed. Thanks
Comment 17 Weeble 2012-10-17 08:35:27 UTC
Just for reference, I believe this was causing trouble with the NuGet command-line tool when fetching many (5+?) packages at once. Symptoms were errors like:

WARNING: Could not connect to the feed specified at 'https://nuget.org/api/v2/'. Please verify that the package source (located in the Package Manager Settings) is valid and ensure your network connectivity.
The specified archive is invalid.


WARNING: Could not connect to the feed specified at 'https://nuget.org/api/v2/'. Please verify that the package source (located in the Package Manager Settings) is valid and ensure your network connectivity.
Unable to find version '0.3.1' of package 'Gate'.

It was broken for 2.11.2, 2.11.3 and 2.11.4, and it works again in master. I haven't done the bisect, but from the description and looking at the stack traces I'm pretty confident it was caused by this bug.
Comment 18 Jonathan Pryor 2012-11-17 22:20:09 UTC
*** Bug 7025 has been marked as a duplicate of this bug. ***
Comment 21 Bassam 2013-06-08 19:32:21 UTC
I'm seeing the same issue as Weeble with NuGet with the latest from master.