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...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-11-20 13:25 UTC by leankitryan
Modified: 2015-02-20 16:42 UTC (History)
4 users (show)

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


Attachments

Description leankitryan 2014-11-20 13:25:13 UTC
A continuation of the issue described here:
https://bugzilla.xamarin.com/show_bug.cgi?id=13200

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("192.168.10.25:8888/"), // 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;

			try
			{
				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)

Xamarin.Android
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.

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