Bug 40104 - HttpClient Timeout - Incorrect and Inconsistent behaviour
Summary: HttpClient Timeout - Incorrect and Inconsistent behaviour
Status: RESOLVED DUPLICATE of bug 46792
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Net.Http ()
Version: 4.8.0 (C9)
Hardware: PC Windows
: --- normal
Target Milestone: 4.8.0 (C9)
Assignee: Marek Safar
Depends on:
Reported: 2016-04-05 11:42 UTC by Michael
Modified: 2017-02-14 17:14 UTC (History)
7 users (show)

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

A demo app which illustrates the inconsistent and incorrect HttpClient timeout behaviour (57.05 KB, application/x-zip-compressed)
2016-04-05 11:42 UTC, Michael

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:

Description Michael 2016-04-05 11:42:17 UTC
Created attachment 15605 [details]
A demo app which illustrates the inconsistent and incorrect HttpClient timeout behaviour

There seems to be inconsistent behaviour with the timeout when using the HttpClient class.

The scenario arose for us when sending  a Web API request is sent to a IP address which does not exist/reachable via the mobile network. Wifi is turned off

I have included a zip file containing an app to demonstrate this. (You will probably have to change the IP when to attempt to repeat the issue in your test environment)

1. If  you alter the code to send the last request only, then the 15 second time-out is honoured
2. If you alter the code to send both the first and last request then , then on the first attempt, the timeout is not honoured (it takes over a minute, but if you repeat the action it is honoured the second time.
3. If you alter the code to send all three requests , then the timeout is sometimes not honoured for a couple of repeated requests. I have also seen the last request not timing out, the first and second attempts timeout.

The task which initiates the request, seems to be irrelevant as the same behaviour is exhibited regardless of whether Task.Run or Task.Factory.StartNew is used

This behaviour seems inconsistent and wrong. Can you investigate, and provide a fix.

I am running this on an iPhone 6 runinng iOS version 9.2.1 (13D15)

This has been built on visual studio 2012 update 5, with the following installed
Xamarin (odd817c)
Xamarin.Android (a94a03b)
Xamarin.iOS (3a25bf1)

My mac is running the following software

=== Xamarin Studio ===

Version 5.10.3 (build 27)
Installation UUID: c0a4c8dc-0061-4b22-a3ad-b4e6bc68b1db
	Mono 4.2.3 (explicit/832de4b)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402030004

=== Xamarin.Profiler ===

Version: 0.31.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 7.2 (9548)
Build 7C68

=== Xamarin.Mac ===

Version: (Starter Edition)

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/McCoolM/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.4   (API level 19)
		5.0   (API level 21)

SDK Tools Version: 24.2.0
SDK Platform Tools Version: 22.0.0
SDK Build Tools Version: 22.0.1

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

Android Designer EPL code available here:

=== Xamarin Android Player ===

Not Installed

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: d7cac50
Branch: master
Build date: 2016-03-21 20:13:04-0400

=== Build Information ===

Release ID: 510030027
Git revision: 8dc6bca63f5cd93719a093973e74de6999864193
Build date: 2016-03-17 17:13:33-04
Xamarin addins: 45239909442742bdee83a0c0f77eecb8a08bedfa
Build lane: monodevelop-lion-cycle6-c6sr2

=== Operating System ===

Mac OS X 10.10.5
Darwin W013208-MAC 14.5.0 Darwin Kernel Version 14.5.0
    Wed Jul 29 02:26:53 PDT 2015
    root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64
Comment 1 Marek Safar 2016-05-10 19:51:00 UTC
I need some help in reproducing this. Using your sample with IP address which does not exist in my local network I get

2016-05-10 21:44:36.618 TimeoutDemoTouch[59257:2416276] mvx:Diagnostic: 25.69 Starting test...
2016-05-10 21:44:42.992 TimeoutDemoTouch[59257:2416614] mvx:Diagnostic: 32.06 Completed first request- Exception occurred [Error: ConnectFailure (Network is unreachable)]
2016-05-10 21:44:42.992 TimeoutDemoTouch[59257:2416613] mvx:Diagnostic: 32.06 Completed last request- Exception occurred [Error: ConnectFailure (Network is unreachable)]
2016-05-10 21:44:42.992 TimeoutDemoTouch[59257:2416612] mvx:Diagnostic: 32.06 Completed second request- Exception occurred [Error: ConnectFailure (Network is unreachable)]
2016-05-10 21:44:42.992 TimeoutDemoTouch[59257:2416613] mvx:Diagnostic: 32.06 Test completed

Which looks ok to me as it's less than 15 seconds timeout.
Comment 2 Michael 2016-05-11 08:05:29 UTC
The scenario we have is that our servers are on a private network, therefore we cannot access it across the mobile network. We can only access it via wifi. The IP address you are attempting to access must be reachable via the wifi network.

So the scenario is start the app, turn off wifi (mobile still turned on), then run the test.

I don't know if the lack of mobile connectivity aspect of it is significant.
Comment 3 Marek Safar 2016-05-11 08:58:58 UTC
I am still trying to repro this as I don't have same network setup.

Does this happen on device only or also on simulator?
Does this happen when you disable wifi before running the app?

If your server is on private network perhaps you could share the IP with me privately so I get same response from your firewall as you do when using public network.
Comment 4 Michael 2016-05-13 13:56:31 UTC
I cannot provide access to our network.

I have been testing this on an iPhone 6 and a 5S.

Have you changed the code to send more than one request. A single request always works. You have to change the code to simulate the error.

What is the issue you are having replicating it?
Comment 5 Marek Safar 2016-05-24 13:55:51 UTC
I cannot replicate the case where timeout is ignored.

I am testing on simulator (don't have a device with GSM/3G/LTE/etc network) but whatever I tried once the timeout is hit the connection terminates.

One more thing you could try is to use native http handlers, that's new feature available in latest release. See https://developer.xamarin.com/releases/ios/xamarin.ios_9/xamarin.ios_9.8/#Selecting_the_Default_Handler
Comment 6 Michael 2016-05-24 14:02:00 UTC
I think you need to get an iPhone with mobile network coverage to get to the bottom of this problem. There is another user on the forum experiencing the same issue. I need this to work on Android as well so I do not want an iOS specific solution.
Comment 7 Marek Safar 2016-05-24 14:03:55 UTC
This is not iOS specific solution, we have support for native handlers on Android as well
Comment 8 Michael 2016-05-24 14:11:36 UTC
How do you know this will work, when you cannot replicate the fault I have reported? I think you have to replicate the scenario. Speak to the other person on the forum as he is having the exact same issue as me.
Comment 9 Marek Safar 2016-05-24 15:26:42 UTC
I am suggestion workaround which you can use before I somehow manage to reproduce the issue
Comment 10 Manuel de la Peña [MSFT] 2016-09-15 09:53:46 UTC
Michel, do you continue to experience the issue?
Comment 11 Alex Soto [MSFT] 2016-10-23 22:45:30 UTC
Michel, do you continue to experience the issue?
Comment 12 Michael 2016-10-24 09:19:45 UTC
Yes I am still experiencing the issue, event when using the native http client handler within Xamarin
Comment 13 Manuel de la Peña [MSFT] 2016-10-24 15:48:37 UTC
@Alex I'll take over this bug.
Comment 14 Manuel de la Peña [MSFT] 2016-10-26 16:58:31 UTC
Just to clarify, playing with the sample code I have the following results:

NSUrlSession HttpClient:

mvx:Diagnostic: 17.43 Completed last request
mvx:Diagnostic: 17.43 Completed first request
mvx:Diagnostic: 17.43 Completed second request
mvx:Diagnostic: 17.43 Test completed

CFNetwork HttpClient:

mvx:Diagnostic: 17.18 Completed first request
mvx:Diagnostic: 17.18 Completed second request
mvx:Diagnostic: 17.18 Completed last request
mvx:Diagnostic: 17.18 Test completed

Managed HttpClient:

mvx:Diagnostic:199.43 Completed last request
mvx:Diagnostic:199.43 Completed second request
mvx:Diagnostic:199.43 Test completed
mvx:Diagnostic:199.43 Completed first request

So, AFAIK I can reproduce the issue with the managed version of the HttpClient. You mentioned that you do see the issue with the NSUrlSession and CFNetwork HttpClients, is this correct? Do my results look ok?
Comment 15 Michael 2016-11-01 17:15:34 UTC
I'm not quite sure what these timings represent. The issue is how long it takes, so I would have to know when you started the request to know whether you are experiencing the same issue. Is it honouring the timeout set? or is it using the default timeout?

Can you clarify your last update?
Comment 16 Manuel de la Peña [MSFT] 2016-11-02 17:53:18 UTC
@Michel my comment was that I was able to replicate the bug with at least the managed version of the HttpClient. 

@Marek the way I replicated the issue was as simple as disabling my WiFi to force the timeout which is not honoured. I'll move this to BCL and reopen it.
Comment 17 Michael 2016-11-03 09:10:57 UTC
Yes, I managed to replicate this with the Android and IOS managed version
Comment 18 Manuel de la Peña [MSFT] 2016-11-07 10:59:32 UTC
@Michel I'm sure Marek will take a look, we will keep you posted.
Comment 19 Marek Safar 2016-11-11 12:50:59 UTC
I can reproduce the issue on device if I disable WiFi before launching the app
Comment 20 Sebastien Pouliot 2016-12-06 14:03:12 UTC
Sounds related to bugs #46792 and #45761
Comment 21 Marek Safar 2017-02-06 15:25:56 UTC

*** This bug has been marked as a duplicate of bug 46792 ***
Comment 22 Michael 2017-02-14 12:51:40 UTC
Hi Marek

Has this bug been fixed. If so what version of Xamarin is it in?


Comment 23 Marek Safar 2017-02-14 17:14:43 UTC
The latest alphas should have it for sure