Bug 13200 - System.Net.Http.HttpClient Timeout seems to be ignored
Summary: System.Net.Http.HttpClient Timeout seems to be ignored
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: master
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
: 13331 (view as bug list)
Depends on:
Blocks:
 
Reported: 2013-07-12 05:14 UTC by asp_net
Modified: 2016-05-26 13:12 UTC (History)
20 users (show)

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


Attachments

Description asp_net 2013-07-12 05:14:44 UTC
I am using Xamarin.iOS 6.3.7.164 and facing a very strange behaviour with timing out requests sent with HttpClient():

The following code tries to get a result from an url that does not exist respectively isn't available (simulating a server outage):

private async void Connect()
{
    var httpClient = new System.Net.Http.HttpClient
    {
        BaseAddress = new Uri("http://192.168.179.33/fooapi/"),
        Timeout = TimeSpan.FromSeconds(3)
    };

    Console.WriteLine("Timeout is set to: " + httpClient.Timeout);
    Console.WriteLine("Start: " + DateTime.Now);

    var start = DateTime.Now;

    try
    {
        await httpClient.GetAsync("foo");
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: " + e.Message);
    }

    Console.WriteLine("Finished: " + DateTime.Now);
    Console.WriteLine("Duration: " + (DateTime.Now - start).TotalSeconds);
}

I expected to see a result after 3 seconds, or at least after 15 seconds (http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.timeout.aspx). But what I get is a task running for 75 seconds:

2013-07-12 00:06:21.134 XamariniOSHttpClientConnectionTimeout[50422:c07] Timeout is set to: 00:00:03
2013-07-12 00:06:21.146 XamariniOSHttpClientConnectionTimeout[50422:c07] Start: 12.07.2013 00:06:21
2013-07-12 00:07:36.613 XamariniOSHttpClientConnectionTimeout[50422:c07] Error: Error: ConnectFailure (Connection timed out)
2013-07-12 00:07:36.614 XamariniOSHttpClientConnectionTimeout[50422:c07] Finished: 12.07.2013 00:07:36
2013-07-12 00:07:36.617 XamariniOSHttpClientConnectionTimeout[50422:c07] Duration: 75,468696

It is reproduceable on multiple machines.

See a failing test attached.
Comment 1 Marek Safar 2013-07-15 08:35:37 UTC
Fixed in master & 3-2 branch
Comment 2 Marek Safar 2013-07-22 12:29:37 UTC
*** Bug 13331 has been marked as a duplicate of this bug. ***
Comment 3 Bo Pang 2013-07-22 15:25:40 UTC
(In reply to comment #2)
> *** Bug 13331 has been marked as a duplicate of this bug. ***

I tried this test case. It works for that url in the test case. However, when I replace the url with the image url in Bug 13331, the timeout does not work.

Here the the code and output:

 async Task Connect()
		{
			HttpResponseMessage response = null;
			var httpClient = new System.Net.Http.HttpClient
			{
				BaseAddress = new Uri("http://photojournal.jpl.nasa.gov/jpeg/PIA15416.jpg"),
				Timeout = TimeSpan.FromSeconds(3)
			};

			Console.WriteLine("Timeout is set to: " + httpClient.Timeout);
			Console.WriteLine("Start: " + DateTime.Now);

			var start = DateTime.Now;

			try
			{
				response = await httpClient.GetAsync("");
			}
			catch (Exception e)
			{
				Console.WriteLine("Error: " + e.Message);
			}
			Console.WriteLine("Finished: " + DateTime.Now);
			Console.WriteLine("Duration: " + (DateTime.Now - start).TotalSeconds);
		}

Output:
2013-07-22 15:20:12.672 GetAsyncTestcase[50981:c07] Timeout is set to: 00:00:03
2013-07-22 15:20:12.686 GetAsyncTestcase[50981:c07] Start: 7/22/2013 3:20:12 PM
2013-07-22 15:20:19.948 GetAsyncTestcase[50981:c07] Finished: 7/22/2013 3:20:19 PM
2013-07-22 15:20:19.950 GetAsyncTestcase[50981:c07] Duration: 7.261929
Comment 4 Marek Safar 2013-07-23 03:59:53 UTC
Works as expected for me
Comment 5 asp_net 2013-09-15 16:00:46 UTC
I am sorry to say that, but that bug occurs again in the same way as described here. Interestingly not by calling DeleteAsync() but by calling the other methods (PostAsync() for example).

Could you please check that again.

I am currently on 6.4.5.0.
Comment 6 Tim Brand 2013-09-22 07:40:50 UTC
I'm experiencing the exact same.
I've tested all *Async() methods: GetAsync/PostAsync/PutAsync/DeleteAsync/SendAsync, and all of them are ignoring the timeout setting.
I've set the timeout to 3 seconds and included a sleep of 10secs in my php script. All requests times are at least 10 secs, so the 3 secs is ignored.
I've tried connecting to a DNS name and an IPaddress, both act the same.
Comment 7 Tim Brand 2013-09-22 07:43:23 UTC
forgot to add my version info.
I'm using an Android project.
Xamarin Studio: Version 4.0.12 (build 3)
Xamarin.Android: Version: 4.8.1 (Indie Edition)
Comment 8 Marek Safar 2013-09-22 13:24:41 UTC
OK, it was not really working but it should be fixed in master now. It should make into next round of alphas.
Comment 9 Stephen Shaw 2013-10-22 19:19:25 UTC
What version should this fix be in?
Comment 10 Rolf Bjarne Kvinge [MSFT] 2013-10-28 07:19:18 UTC
This will for sure be in 7.0.4, possibly in the 7.0.3 which is currently in alpha.
Comment 11 Tim Brand 2014-03-17 07:58:07 UTC
The bug is back again (currently using the Beta channel).

The timeout seemed to be ignored again. The only timeout setting that worked for me was 600ms (and less), 700ms and above was totally ignored.

Xamarin Studio: version 4.2.4 (build32)
Xamarin.iOS: Version: 7.2.0.2
Comment 12 asp_net 2014-03-17 08:08:08 UTC
Confirmed, I am seeing this, too (7.2.0.2). And I think it has never (!) been fixed.
Comment 13 Marek Safar 2014-03-21 17:01:29 UTC
Works as expected with 7.2.1

Timeout is set to: 00:00:03
Start: 3/21/2014 9:59:57 PM
Error: The Task was canceled
Finished: 3/21/2014 10:00:00 PM
Duration: 3.122437
Comment 14 Seb Richards 2014-05-28 14:55:22 UTC
I'm afraid I'm seeing this exact problem also - timeout is ignored and 75 seconds is used instead.

Xamarin Studio 5.0 b878
Xamarin.iOS 7.2.3.39
Comment 15 Jonathan Moosekian 2014-11-05 09:51:23 UTC
I see 75 seconds, too. Particularly, when I am testing on a device that is configured to connect through a proxy on WIFI, but I am attempting connection through LTE network. We discovered this during testing of offline features, and since we test through a proxy on a wifi, if we turn off wifi on the device, it can no longer connection to our server, but the phone has a valid connection through the cell network. In other words, our test server is only reachable through the wifi.

Here's a valid test scenario:

Configure app to connect to proxy that is not reachable outside of your internal network (but can be reached on your wifi)
Then turn off your wifi and attempt to connect through the proxy.
HttpClient will take a little over 75 seconds to throw a WebException.
Comment 16 Jonathan Moosekian 2014-11-05 09:58:15 UTC
I'm using:

Xamarin Studio
Version 5.5.3 (build 6)

Xamarin.iOS
Version: 8.4.0.16
Comment 17 David Johnson 2014-11-17 11:50:21 UTC
I see this behavior as well with Xamarin Forms 1.2.3.6257 on Android
Comment 18 Seb Richards 2014-11-17 14:09:57 UTC
It would be great if Xamarin staff could at least respond to this ticket!
Comment 19 Marek Safar 2014-11-18 04:53:03 UTC
Original test case retested with Xamarin.iOS 8.2 and Xamarin.Android 4.17 and both work as expected.

Provide actual test case which is failing or detailed steps how to reproduce it. If different setup is involved than this ticket it's better to fill different bug report.
Comment 20 leankitryan 2014-11-20 13:29:06 UTC
I was able to replicate Jonathan's issue which I have submitted as a separate bug. https://bugzilla.xamarin.com/show_bug.cgi?id=24704
Comment 21 Paulo Ortins 2014-12-09 15:13:00 UTC
It's not solved yet. I'm having this issue with Xamarin.Forms 1.2.3.6257 on Android.
Comment 22 Paulo Ortins 2014-12-09 15:13:21 UTC
It's not solved yet. I'm having this issue with Xamarin.Forms 1.2.3.6257 on Android.
Comment 23 Paul Read 2015-02-04 08:58:11 UTC
Ditto this issue still exists today, taking 75 secs to timeout rather than the 5 I have set it to.
Comment 24 Paul Read 2015-02-04 09:02:56 UTC
Still exists, magic 75 secs to timeout.
Note XS 5.7
Android 4.4 (19)
ModernHTTPClient -> OkHTTP
XForms 1.3.2.6316
Comment 25 Brian 2015-02-20 15:38:15 UTC
Still exists... And causing lots of trouble.

Why is this being ignored by Xamarin?
Comment 26 Jonathan Moosekian 2015-02-20 16:22:19 UTC
It's actually fixed in mono. That's where the bug actually exists. See this bug: https://bugzilla.xamarin.com/show_bug.cgi?id=24704
Comment 27 David Johnson 2015-03-27 11:28:46 UTC
When is this mono fix going to make it into Xamarin? I still experience the 75 sec timeout on Xamarin Forms 1.4.0.6341, although I set the timeout to 8 sec. In most cases the 8 sec is respected, but when the timeout is at the socket level, it's 75 sec:

2015-01-29 10:19:59.742 skillstore[958:185552]  7 ERROR Skillstore.AppLogic.Rest.Request: ExecuteAsync - WebException from POST http://192.168.1.192:9000/api/v1/auth/token, retry count = 1: System.Net.WebException: Error: ConnectFailure (Connection timed out) ---> System.Net.Sockets.SocketException: Connection timed out
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000f1] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.6.0.51/src/mono/mcs/class/System/System.Net.Sockets/Socket_2_1.cs:1257 
  at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x001c2] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.6.0.51/src/mono/mcs/class/System/System.Net/WebConnection.cs:213 

I am NOT using a proxy.
Comment 29 Malisa Ncube 2015-12-04 09:11:01 UTC
This bug still exists and its a blocker to our progress. I'm really getting frustrated.
Comment 30 mitchell balsam 2015-12-22 20:31:21 UTC
We had the same problem with a library we were using.   The library was trying to provide Async support. We're not using Async so we made this change.

We changed this 

return request.EndGetRequestStream(request.BeginGetRequestStream(null, null));

to

return request.GetRequestStream();

And it started working.  

But we also found this comment in 

HttpWebRequest.Timeout Property

https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

The Timeout property has no effect on asynchronous requests made with the BeginGetResponse or BeginGetRequestStream method
Comment 31 rpetrov 2016-05-26 13:12:18 UTC
This bug still exists!
Xamarin   4.0.4.4 (a9c7826)
Xamarin.Android   6.0.4.0 (ee215fc)

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