Timeout exception HttpWebRequest.GetResponse() in Mono 5.X

XavierFortinXavierFortin Xavier FortinUSMember ✭✭

I realize this might not be the best place to post an issue with Mono, but I'll do it in case someone encountered a similar issue.

I have this code:

string apiUrl = "https://url.to.server/name/of/request";

System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(apiUrl);
webRequest.ContentType = "text/json";
webRequest.Accept = "Accept-application/json";
webRequest.Method = "GET";
webRequest.Headers.Add("tokenId", "00000000-0000-0000-0000-000000000000");

webRequest.AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip;
webRequest.CookieContainer = new System.Net.CookieContainer();
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;

try
{
    System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)webRequest.GetResponse();

    System.Diagnostics.Debug.WriteLine(response);
}
catch (Exception ex)
{
    System.Diagnostics.Debug.WriteLine(ex);
}

Prior to Mono 5.X, this has worked fine and returned with the response without much issue. Since Mono 5.0 and later, this has stopped working and instead throw a Timeout on GetResponse consistently. I've been building my release with Mono 4.8.1 for a few months because of this issue, but building the UnifiedAPI version with the static registrar with Xamarin Studio and Mono 4.8.1 fails.

I don't think this is an issue with HttpWebRequest per se. But with the Mono implementation and maybe the server I'm connecting to? That being said, there is definitely something different regarding the HttpWebRequest between Mono 4.8.1 and 5.0.

Any ideas?

I'll keep investigating and if I find anything more concrete, I'll file a bug.

Answers

  • XavierFortinXavierFortin Xavier Fortin USMember ✭✭

    I'm adding a bit more information on this after investigating.

    I'm pretty sure the timeout happens only on some HTTPS server. In fact, my issue seems very similar to this one from two years ago.

    I've tested with NSURLSession and I have no issue with it, so this is definitely Mono related.

    I know of some of our users who experienced the issue before downgrading to Mono 4.8.1 and I have one internal server on which I can reproduce this. But since I don't know what's special about the faulty service, I can't reproduce this into a test Web Service. I tried. Maybe something weird with the certificate?

    I'm pretty sure this has to do with the SSL negociation. If I configure a proxy between my application and the remote server, and I enable the HTTPS traffic decryption (in this case I use Fiddler on a VM), it suddenly starts to work without issue.

    I know I'm not being particularly helpful here, but I'm kind of desperate.

    I'm working on using NSURLSession instead, but that could prove to be quite a task.

  • ChrisHamonsChrisHamons Chris Hamons USXamarin Team Xamurai

    So try building / running with MONO_TLS_PROVIDER=legacy in newer (but not latest) Xamarin.Mac.

    Maybe:

    https://dl.xamarin.com/XamarinforMac/Mac/xamarin.mac-3.2.0.175.pkg
    or
    https://dl.xamarin.com/XamarinforMac/Mac/xamarin.mac-3.0.0.393.pkg

    and compare to the default of AppleTLS.

    AppleTLS has some behavior changes from the old legacy TLS, maybe you are hitting one of those?

  • XavierFortinXavierFortin Xavier Fortin USMember ✭✭

    I get the same behavior in Xamarin.Mac 3.0 and 3.2 with or without MONO_TLS_PROVIDER set to legacy.

    I'm following the instruction found in the Mono 5.0.0 release notes: http://www.mono-project.com/docs/about-mono/releases/5.0.0/

    Is setting the MONO_TLS_PROVIDER environment variable all that is needed?

  • ChrisHamonsChrisHamons Chris Hamons USXamarin Team Xamurai

    So MONO_TLS_PROVIDER will in theory let you swap the tls provider (and determine if this is introduced by that). The rub however, is this:

    https://github.com/mono/mono/commit/582b140f7dfad1a51d5fcc75cf8c54838c3c80f6#diff-a2c7eee27fca23828250fa839a85d87fR68

    we define that for both supported profiles:

    mcs/build/profiles/xammac.make:ONLY_APPLETLS=1
    mcs/build/profiles/xammac_net_4_5.make:ONLY_APPLETLS=1

    since legacy TLS is not something we want to support in XM supported profiles.

    Doing some more digging, this was introduced 2017-02, so any Xamarin.Mac after 3.2 won't honor MONO_TLS_PROVIDER.

    However, if your test is a simple console application, not using Xamarin.Mac, then you can install various mono's to determine when the behavior change occurred. It is not clear for your post if your sample requires XM or can be done in a console app.

  • XavierFortinXavierFortin Xavier Fortin USMember ✭✭

    I did produce the bug in a Mono console app. Just to be sure, I've went back an tried with the Mono 4.8.1 and 5.0.0. I can confirm that this started in 5.0.0.

  • ChrisHamonsChrisHamons Chris Hamons USXamarin Team Xamurai

    Please file a bug https://bugzilla.xamarin.com with your sample app attached and I'll get it directed to the right team.

  • XavierFortinXavierFortin Xavier Fortin USMember ✭✭

    Sure can do.

    Here it is: https://bugzilla.xamarin.com/show_bug.cgi?id=60128

    I can't provide a functioning test project though. I'm testing on a local server and, as mentioned, I don't know what's special about it as other similar servers do not have the issue.

    Another thing I'd like to mentioned. I don't know how I haven't noticed it sooner (I think it might be because of the recent updates). Most HttpWebRequest do not work anymore. At least, all those I've tested so far have timed out. This only seems to be in the Classic API though. While it's not much of an issue since I'm migrating to the Unified API, maybe it's somehow related to my main issue?

  • ChrisHamonsChrisHamons Chris Hamons USXamarin Team Xamurai

    Classic XM uses the system mono very directly, so any issue you have with console applications would directly hit it. Unified builds it own curated version of the BCL / runtime for storage internally, so doesn't touch system mono (whatever version you have) outside of using tooling.

Sign In or Register to comment.