Bug 24704 - System.Net.Http.HttpClient PostAsync ignores timeout if webproxy isn't accessible
Summary: System.Net.Http.HttpClient PostAsync ignores timeout if webproxy isn't access...
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
Depends on:
Reported: 2014-11-20 13:25 UTC by leankitryan
Modified: 2015-02-20 16:42 UTC (History)
4 users (show)

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:

Description leankitryan 2014-11-20 13:25:13 UTC
A continuation of the issue described here:

The timeout is being ignored when connecting to an HTTPS url via a proxy that cannot be reached. 

Sample Code:

			var handler = new HttpClientHandler
				Proxy = new WebProxy(""), // proxy that doesn't exist
				UseProxy = true,
				AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate

			var httpClient = new System.Net.Http.HttpClient(handler)
				BaseAddress = new Uri("https://google.com"),
				Timeout = TimeSpan.FromSeconds(20)

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

			var start = DateTime.Now;

				var restRequest = new HttpRequestMessage
					Method = HttpMethod.Post,
					RequestUri = new Uri("foo", UriKind.Relative),
					Content = new StringContent("", null, "application/json")
				await httpClient.PostAsync(restRequest.RequestUri, restRequest.Content);
			catch (Exception e)
				Console.WriteLine("Error: " + e.Message);

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

Output on iOS

2014-11-20 11:03:28.573 HttpTimeoutiOS[9315:70b] Timeout is set to: 00:00:20
2014-11-20 11:03:28.582 HttpTimeoutiOS[9315:70b] Start: 11/20/2014 11:03:28 AM
Thread started: <Thread Pool> #5
Thread started: <Thread Pool> #6
Thread started: <Thread Pool> #7
2014-11-20 11:04:43.849 HttpTimeoutiOS[9315:70b] Error: Error: ConnectFailure (Connection timed out)
2014-11-20 11:04:43.850 HttpTimeoutiOS[9315:70b] Finished: 11/20/2014 11:04:43 AM
2014-11-20 11:04:43.852 HttpTimeoutiOS[9315:70b] Duration: 75.267784

Output on Android

Timeout is set to: 00:00:20
Start: 11/20/2014 11:08:33 AM
Error: Error: ConnectFailure (Connection timed out)
Finished: 11/20/2014 11:09:36 AM
Duration: 63.293487

Xamarin Studio 5.5.3 (build 6)

Version: 4.18.1 (Business Edition)
Comment 1 Marek Safar 2014-11-21 07:01:06 UTC
Fixed in mono master
Comment 2 Jonathan Moosekian 2015-02-20 16:42:22 UTC
The timeout is now respected, however, if the timeout is exceeded, instead of throwing a WebException, it throws a TaskCancellationException which is not in line with expected behavior of the HttpClient.