Mono: HttpClient does not respect Timeout

Created on 23 Jan 2019  路  33Comments  路  Source: mono/mono

_From @csuzw on December 17, 2018 9:15_

I believe this is the same/similar to this issue for Xamarin.iOS: https://github.com/xamarin/xamarin-macios/issues/5190 . There is a hint in the original issue linked in that IOS issue that using the AndroidClientHandler and setting the ReadTimeout/ConnectTimeout is a workaround but I get the an exception about encoding 'identity' not being supported if I do this.

Steps to Reproduce

  1. Create a HttpClient and set Timeout to a value greater than 100s ( the default value).
  2. Send a request that takes longer than 100s but less than the Timeout set.

Expected Behavior

The request to return successfully

Actual Behavior

The request times out after 100s.

Version Information

Microsoft Visual Studio Enterprise 2017 (2)
Version 15.9.4
VisualStudio.15.Release/15.9.4+28307.222
Microsoft .NET Framework
Version 4.7.03190

Installed Version: Enterprise

Visual C++ 2017 00369-60000-00001-AA999
Microsoft Visual C++ 2017

Application Insights Tools for Visual Studio Package 8.14.11009.1
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2017 15.9.04012.0
ASP.NET and Web Tools 2017

ASP.NET Core Razor Language Services 15.8.31590
Provides languages services for ASP.NET Core Razor.

ASP.NET Web Frameworks and Tools 2017 5.2.60913.0
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0 15.9.03024.0
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools 15.9.02046.0
Azure Functions and Web Jobs Tools

C# Tools 2.10.0-beta2-63501-03+b9fb1610c87cccc8ceb74a770dba261a58e39c4a
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus 1.1.49 (remotes/origin/d15-8@ee674f3)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

HgSccPackage 2.0.5
Mercurial source code control package

JavaScript Language Service 2.0
JavaScript Language Service

JavaScript Project System 2.0
JavaScript Project System

JavaScript UWP Project System 2.0
JavaScript UWP Project System

JetBrains ReSharper Ultimate 2018.2.1 Build 182.0.20180829.63240
JetBrains ReSharper Ultimate package for Microsoft Visual Studio. For more information about ReSharper Ultimate, visit http://www.jetbrains.com/resharper. Copyright 漏 2018 JetBrains, Inc.

Microsoft Azure Tools 2.9
Microsoft Azure Tools for Microsoft Visual Studio 2017 - v2.9.10730.2

Microsoft Continuous Delivery Tools for Visual Studio 0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger 1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager 1.0
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger 1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards 1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers 1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package 1.0
Microsoft Visual Studio VC Package

MLGen Package Extension 1.0
MLGen Package Visual Studio Extension Detailed Info

Mono Debugging for Visual Studio 4.13.12-pre (9bc9548)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager 4.6.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

ProjectServicesPackage Extension 1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

ResourcePackage Extension 1.0
ResourcePackage Visual Studio Extension Detailed Info

ResourcePackage Extension 1.0
ResourcePackage Visual Studio Extension Detailed Info

Snapshot Debugging Extension 1.0
Snapshot Debugging Visual Studio Extension Detailed Info

SQL Server Data Tools 15.1.61810.11040
Microsoft SQL Server Data Tools

Telerik WPF VSExtensions 2018.4.1121.1
Progress庐 Telerik庐 UI for WPF Visual Studio extensions

Telerik Xamarin VSExtensions 2018.4.1121.1
Progress庐 Telerik庐 UI for Xamarin Visual Studio extensions

TypeScript Tools 15.9.20918.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 2.10.0-beta2-63501-03+b9fb1610c87cccc8ceb74a770dba261a58e39c4a
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 10.2 for F# 4.5 15.8.0.0. Commit Hash: 6e26c5bacc8c4201e962f5bdde0a177f82f88691.
Microsoft Visual F# Tools 10.2 for F# 4.5

Visual Studio Code Debug Adapter Host Package 1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Tools for Containers 1.0
Visual Studio Tools for Containers

Visual Studio Tools for Universal Windows Apps 15.0.28307.208
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

VisualStudio.Mac 1.0
Mac Extension for Visual Studio

Xamarin 4.12.3.77 (d15-9@e3f40b477)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 4.16.13 (45a16efd4)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates 1.1.128 (6f5ebb2)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK 9.1.4.2 (HEAD/8255f42fc)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK 12.2.1.12 (65ec520)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

_Copied from original issue: xamarin/xamarin-android#2544_

System.Net.Http

Most helpful comment

@baulig is this available now? If so how do you take advantage of this with Xamarin.Android?

At present, when setting the timeout of a HttpClient, Android still isn't respecting this when using the native AndroidClientHandler.

All 33 comments

_From @jonathanpeppers on December 17, 2018 14:51_

Hi @csuzw,

You mentioned you are _not_ using the AndroidClientHandler, which options do you have for your HTTP settings here under Project Options | Android Options | Advanced?

image

Default HttpClient implementation is the same as Managed (as of this post).

_From @csuzw on December 17, 2018 15:0_

I have Default, Managed and Android. It is currently set to Default (if I try to set it to Android I get the exception about encoding 'identity').

When the HttpClient is being constructed it is using the default constructor so I believe it uses whatever the default is (which seems to be HttpClientHandler).

_From @jonathanpeppers on December 17, 2018 17:14_

@grendello after the holiday, is it worth testing timeouts with this service? https://httpstat.us/ They have an option to declare a timeout.

It might be that all we can do is forward this issue to the Mono team?

@csuzw it might also help us if you can send a quick code example of what you are doing, thanks!

_From @csuzw on December 17, 2018 17:52_

Here's a really quick example calling httpstat.us: https://www.dropbox.com/s/rukqqp2yyji5ug9/HttpClientTimeoutApp.zip?dl=0 . HttpClient has timeout set to 200s. The call to httpstat.us has timeout set to 110s. When you click the test button it times out after 100s (at least it does for me!).

Strangely when I tried setting the timeout to a value less than 100s the timeout is respected so this seems to only be a problem when it is greater than the default.

_From @oOGenosOo on January 23, 2019 11:24_

The problem is not only under Android, i have a IOS/Droid project and the timeout is 100s even under IOS.
Some workaround?

This issue is a problem in the Mono BCL, which HttpClient is part of and should be moved to the Mono org.

@baulig this sounds familiar, did we fix it recently?

The new HttpClient has the fix.

Maybe I'm asking the obvious, but what means "new HttpClient"? I've got the same problem in Xamarin.Forms, I've updated all nugget packages, but it seems that the problem is still there. I don't see any milestones in this issue, so can anybody tell me when I can expect the fix? Or is there any workaround for this? It's quite blocking bug.

We need a new version of xamarin forms that use the new mono packages.... considering that an update of the mono FW of the entire xamarin forms need a bunch of tests we need to wait at least till the end of the month.

EDIT:
Not sure of that because i was using the 3.5.X, but there is a prerelease version of XForms (4.0.0) that probably has the new FW (The only reason i can think of the major version change).

HERE: https://www.nuget.org/packages/Xamarin.Forms/

EDIT2:

@marek-safar : You mean the unreleased 5.20 or you mean the 5.18 of december?

@baulig is this available now? If so how do you take advantage of this with Xamarin.Android?

At present, when setting the timeout of a HttpClient, Android still isn't respecting this when using the native AndroidClientHandler.

@baulig Just bumping this as I don't see a milestone or release on this. We're running into the same issue. Is there a workaround that's possible until this new HttpClient is released?

@baulig

bumping again. Can you share when the new HttpClient will be in a released version?

I'm not sure about milestone/release info but it looks like some work for new HttpClient has been done in https://github.com/mono/mono/pull/11906 and https://github.com/mono/mono/pull/13338. According to this comment additional work is required on android side.

So what does it mean generally? When we can expect working HttpClient on the Android?

@baulig

bumping again, we just need a release date, to understand what can we start working on large api call... on the last version still not working :(

/cc @steveisok

We started pulling in the corefx HttpClient, but we only were able to enable it for desktop and web assembly. Unfortunately, I don't have an answer as to what release the rest will make it in as the work still needs to be prioritized.

The corefx HttpClient got rolled out on Desktop and Web Assembly. It was originally planned to also roll it out on all profiles - but this got abandoned, so there is no ETA for it.

I will have a look at this today to see whether we can implement it in the existing HttpClient - though getting it into 2019-02 is a different story and it is not up to me to decide whether or not that will happen.

Well, good news! It looks like I just need to hookup that property.

Thx man, It's really good to hear that sth has moved about this issue :)

Thx man, It's really good to hear that sth has moved about this issue :)

Sure. The problem was simply that I got moved off to another high-priority project.

I just tested in public 16.2 Preview 2 release and the HttpClient timout setting is getting respected on Android and iOS.

Sorry for maybe the newbie question, but by saying "16.2 Preview 2 release" you're thinking about Visual Studio version? If so, how it's related to the version of mono? It's like mono is part of the Visual Studio, so new release of mono goes out together with release of VS? Or new mono is released together with new version of Xamarin?

So basically - how do I know that fix from this issue and related closed pull request is released and availabile to "download"?

@Lazys

There is no direct relationship between VS version and Mono, but there is between VS version and Xamarin.iOS and Xamarin.Android.

I used VS 2019 version 16.2 preview 2 for testing the Xamarin.iOS and Xamarin.Android SDKs to see if the mono fix for this issue was indeed included in those SDKs.

Basically, Xamarin.iOS and Xamarin.Android use parts of Mono, which are pulled in when the Xam.iOS and Xam.Android SDKs are built.

Mono itself, i.e. the full desktop Mono, is a separate beast and is often, but not always, updated at the same time as Xamarin.iOS and/or Xamari.Android. At this time I am not sure what version of Mono this fix is in, but the release notes should indicate it:
https://www.mono-project.com/docs/about-mono/releases/

@jgold6

Good morning, If I have installed VS Professionnal 2017 version 15.8.7, it means that the only way I can fix this bug is to install the Visual studio 2019, or is there a means to update the Xamarin.Android version within my current VS 2017 ?

Thank you for the reply

Hi @jgold6,
Are you sure, timeout is now respected? I've just checked in VS2019 Preview 3 and still have 100s timeout although I change Timeout property on HttpClient object.

@toto287
Yes, you would need to move to VS 2019 Preview version to get this fix. There is no way to update the Xamarin versions in VS 2017 at this time.

@mireksk
I tested this with multiple timeout settings and the timeout set on the HttpClient was respected even when set to greater than 100 seconds. But I did not test on Preview 3 yet, only Preview 2.

Just tested in Preview 3 and yes, I am getting the timeout after 100 seconds, so somehow the fix did not get in Preview 3. I will get this looked at.

Not sure about that, but it should depend of Xamarin.Forms version, not of Visual studio one... what version of XF did you use?

Fix is not in Xam.Android 9.4.0.50 in 16.2 P4 either.

However Xamarin.iOS 12.14.0.110 has the fix in 16.2 P4.

jgolde said:
I just tested in public 16.2 Preview 2 release and the HttpClient timout setting is getting respected on Android and iOS.

mireksk said:
Hi @jgold6,
Are you sure, timeout is now respected? I've just checked in VS2019 Preview 3 and still have 100s timeout although I change Timeout property on HttpClient object.

jgolde said:
@mireksk
I tested this with multiple timeout settings and the timeout set on the HttpClient was respected even when set to greater than 100 seconds. But I did not test on Preview 3 yet, only Preview 2.

Just tested in Preview 3 and yes, I am getting the timeout after 100 seconds, so somehow the fix did not get in Preview 3. I will get this looked at.

Update to this: It appears that I can NOT reproduce this issue on my test device, which is an LG phone running Lollipop. But I CAN reproduce the issue on an emulator running Pie. Another engineer CAN reproduce the issue on their Pixel 3 device, so not sure why I CANNOT reproduce the issue on my Android test device.

I likely tested VS 2019 16.2 preview 2 using my device, but tested 16.2 Preview 3 on emulator, hence my differing results between preview 2 and preview 3.

There is a commit to fix this issue on Android by increasing the default AndroidClientHandler timeouts to 24 hours. That is the same method that was used to fix the issue on iOS. See:
https://github.com/xamarin/xamarin-android/commit/0b79ab6653a4364f837d513453567485c535c611

Using Xamarin.Android.Net.AndroidClientHandler as HttpClientHandler for HttpClient worked for me

Was this page helpful?
0 / 5 - 0 ratings