Bug 52346 - Random Delays when using System.Net.HttpClient
Summary: Random Delays when using System.Net.HttpClient
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib (show other bugs)
Version: 4.8.0 (C9)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2017-02-07 01:30 UTC by me
Modified: 2017-03-15 14:11 UTC (History)
2 users (show)

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

Sample with delay issue on Android (3.12 MB, application/zip)
2017-02-07 01:30 UTC, me

Description me 2017-02-07 01:30:06 UTC
Created attachment 19743 [details]
Sample with delay issue on Android

I have a method that executes several HTTP requests called LoadServers() A button that executes LoadServers(). For my issue I only tap the button once the previous LoadServers() call has finished. A progress dialog appears while loading so I can only execute them serially.

Once in about every 10-15 calls to LoadServers results in the first http request to delay for almost exactly 10 seconds. The average time for LoadServers to complete is less than a half second and never more than 1 second. This only happens on Xamarin.Android. The delay does not happen on Xamarin.iOS and all of this code is shared.

Here is my code

private async Task LoadServers() {
    await Get();
    await Post();
    await Get();
    await Get();
    await Post();

private async Task Get() {
    var url = _httpClient.BaseAddress + model.GetToken();
    Log("Attempting to send GET to: " + url);
    using (var response = await _httpClient.GetAsync(url))
        var resultContent = await response.Content.ReadAsStringAsync();
        Log("Got response back from : " + url + ": " + resultContent);

private async Task Post() {
    var content = requestData.GetToken() + "=" + requestData.PostBody ();
    var request = new StringContent(content)
        Headers = { ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") }
    var url = _httpClient.BaseAddress + "/No_content";
    Log ("Attempting to send POST to: " + url + "  with content: " + content);
    using (var response = await _httpClient.PostAsync(url, request))
        string resultContent = await response.Content.ReadAsStringAsync();
        Log("Got response back from : " + url + ": " + resultContent);
Every 15 or so executions of LoadServers() results in the following log statements:

Thread started:  #37
Thread finished:  #37
[2017-01-21T13:42:30.8841620-06:00] [debug] Loading Servers
[2017-01-21T13:42:30.8946770-06:00] [debug] Attempting to send GET to: XXX
Thread finished: <Thread Pool> #23
Thread started: <Thread Pool> #38
[2017-01-21T13:42:40.9550360-06:00] [debug] Got response back from : XXX <-- Notice the time (~10 seconds)

It was suggested that I try and complete a sample with a console app and see if the issue still occurs without the mobile sdk's. After completing a sample app, it appears that the issues is not reproducible on the console app but I can reproduce it every time on Android. Eventually, it will have a large delay on Android. See the attached sample code. The gists below are the log output from the console app vs the Android app. I was running this on a 6.0 Android device (but it happens on many devices and OS levels). In the logs below, search for "!!!!!!!TOOK OVER 5 SECONDS!!!!!!!" and you'll see the delay examples in the Android output.

Console Output

Android Output

This is connected to a local device. The local device emits a wireless network the mobile device connects to. I then communicate via HTTP to the device on the network. The company I represent are happy to supply the device for you to debug on if that helps.

Here is a SO post about the issue:

=== Xamarin Studio Community ===

Version 6.1.3 (build 19)
Installation UUID: 985cc5e6-4004-4327-8be4-d79f5c66fa97
	Mono 4.6.2 (mono-4.6.0-branch/ac9e222) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 406020016

=== NuGet ===


=== Xamarin.Profiler ===

Not Installed

=== Apple Developer Tools ===

Xcode 8.2.1 (11766.1)
Build 8C1002

=== Xamarin.iOS ===

Version: (Xamarin Studio Community)
Hash: 8b53676
Branch: cycle8-xi
Build date: 2016-12-18 12:23:27-0500

=== Xamarin.Android ===

Version: (Xamarin Studio Community)
Android SDK: /Users/kevinskrei/Library/Android/sdk
	Supported Android versions:
		4.1 (API level 16)
		4.4 (API level 19)
		5.0 (API level 21)
		5.1 (API level 22)
		6.0 (API level 23)
		7.0 (API level 24)

SDK Tools Version: 25.2.3
SDK Platform Tools Version: 25.0.1
SDK Build Tools Version: 25.0.1

Java SDK: /usr
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

Android Designer EPL code available here:

=== Xamarin Android Player ===

Not Installed

=== Xamarin.Mac ===

Version: (Xamarin Studio Community)

=== Build Information ===

Release ID: 601030019
Git revision: 7fabf9293c2d7b638e28a07f798d21799da1f886
Build date: 2016-12-21 07:08:35-05
Xamarin addins: efbae0e683a0060d2230e91e70c1598abc054845
Build lane: monodevelop-lion-cycle8

=== Operating System ===

Mac OS X 10.11.6
Darwin KevinSkrei-MBP.local 15.6.0 Darwin Kernel Version 15.6.0
    Thu Sep  1 15:01:16 PDT 2016
    root:xnu-3248.60.11~2/RELEASE_X86_64 x86_64

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