Bug 30442 - HttpClient GetStreamAsync blocks
Summary: HttpClient GetStreamAsync blocks
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 5.1
Hardware: PC Mac OS
: Highest normal
Target Milestone: ---
Assignee: Marek Habersack
Depends on:
Reported: 2015-05-26 09:39 UTC by John Miller [MSFT]
Modified: 2015-09-01 09:47 UTC (History)
4 users (show)

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

Test Case (36.72 KB, application/zip)
2015-05-26 09:39 UTC, John Miller [MSFT]

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 Developer Community or GitHub 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 John Miller [MSFT] 2015-05-26 09:39:12 UTC
Created attachment 11326 [details]
Test Case


   Calling HttpClient.GetStreamAsync blocks the UI thread on Android. 

**Steps to Reproduce:**

   1. Run the attached sample on an Android simulator (tested on API 19)
   2. Press the button

**Actual Results:**

   Notice the progress bar spinner does not animate (UI thread is blocked)

**Expected Results:**

   The progress bar should be able to animate while the work is being done. 

**Build Date & Platform:**

   XA 5.1.1
   API 19
   Mono 4.0

**Additional Information:**

   Similar code on iOS works as expected. 
   awaiting something else instead of GetStreamAsync, like Task.Delay(2000) will show the expected results.
Comment 1 Atin 2015-05-26 13:55:16 UTC
I have checked this issue and able to reproduce the above behavior with the help of sample and steps given in the bug description.Progress bar spinner does not animate but but project is still in progress shows in application output.

Screencast:  http://www.screencast.com/t/1xvj2miPRNB
Ide Logs: https://gist.github.com/anonymous/35d7095847430566becc
Build Output: https://gist.github.com/anonymous/3749131b41099719b666
Application Output: https://gist.github.com/anonymous/f5f6a0f90d26f4b5ad1d
adb Logcat: https://gist.github.com/anonymous/f976407fcedf6cf6647b

Environment Info:

=== Xamarin Studio ===

Version 5.9.2 (build 2)
Installation UUID: 0b7eaebc-a0ed-4b58-81df-91e378cad28c
	Mono 4.0.1 ((detached/11b5830)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400010043

=== Apple Developer Tools ===

Xcode 6.3.2 (7718)
Build 6D2105

=== Xamarin.iOS ===

Version: (Enterprise Edition)
Hash: fce006e
Branch: master
Build date: 2015-05-21 12:10:06-0400

=== Xamarin.Android ===

Version: (Enterprise Edition)
Android SDK: /Users/Admin_Mac/Desktop/Anddk/android-sdk-macosx
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: (Enterprise Edition)

=== Build Information ===

Release ID: 509020002
Git revision: 43735a4b905b571f69cf9428a3ea7b52aef55476
Build date: 2015-05-18 09:38:12-04
Xamarin addins: ab18ddff45f507ed74f36c2b65df9aee22e28a56

=== Operating System ===

Mac OS X 10.10.2
Darwin Admin-Macs-Mac-mini.local 14.1.0 Darwin Kernel Version 14.1.0
    Thu Nov 13 18:36:56 PST 2014
    root:xnu-2782.10.65~2/RELEASE_X86_64 x86_64
Comment 2 Marek Habersack 2015-09-01 09:47:07 UTC
The code in the provided sample behaves the way it should. The call that blocks is actually

reader.ReadToEnd ();

which is correct since that call is synchronous. HttpClient.GetStreamAsync () is supposed to make the connection to the server and return a network stream as soon as it receives response from the server. This tends to happen pretty quickly, so await takes little time. Reading of the data actually happens below await, in reader.ReadToEnd () which is a blocking operation - this is why the UI thread blocks.

*Note: what's written below is just a guess*

The reason it works on Xamarin.iOS might be the underlying implementation of networking code used by HttpClient. On iOS it uses native network framework which *might* read the data before GetStreamAsync () returns and so when reader.ReadToEnd () is called it's just a quick local read instead of the actual network access.

So the issue might be a quirk/bug in Xamarin.iOS, or just a platform-specific behavior. In either case, the code as presented in the attached test case should be expected to block the UI thread.