Search Unity

HttpWebRequest frequently fails when built with IL2CPP

Discussion in 'Editor & General Support' started by MapMan, Jul 17, 2018.

  1. MapMan

    MapMan

    Joined:
    Oct 27, 2013
    Posts:
    38
    Hello

    I'm implementing search in my game. The search backend is search as a service, thus I do web requests with a query to get results. When the user types a letter into the search input box, a search request is sent. When the user types in another letter before the previous request was finished, the former request is cancelled and a new one is sent.

    This set up worked well until you started typing in letters in quick succession. Technically, for each query (or in other words, a new letter user types in) I need to send 4 requests. To keep this example simple, think of it as a query for movies in 4 different genres. The problem was that the underlying HttpWebRequest would throw a WebException for the requests, with reason: SendFailure - cannot access a disposed object. That was pretty cryptic in itself. After a lot of search I found out that the HTTP spec limits the number of concurrent connections to a single endpoint and that limit, by default, is 2. Nowadays, this limit is ignored by web browsers and often overwritten to something much bigger, e.g. ASP.NET overrides it to 10 by default. There's a .NET property that allows us to override that too: System.Net.ServicePointManager.DefaultConnectionLimit

    After increasing the limit to 10 simultaneous connections, the exceptions went away and the search works like a charm in editor and on Android.

    The issue comes back when I build the game with IL2CPP (Android or iOS). When built with IL2CPP, the underlying HttpWebRequest frequently throws misc WebExceptions: connection interrupted, send failure (the aforementioned exception about accessing a disposed object), connection timed out. All of those exceptions are perfectly valid themselves - they can happen when there's a poor connection or even no connection at all. The problem is that even with great Internet connection these errors are very frequent and hinder UX - only with IL2CPP.

    At this point, I'm out of ideas as to what is causing this problem. My 2 best guesses are:
    1. Something is causing the game not to obey the DefaultConnectionLimit value when built with IL2CPP.
    2. There's something wrong with HttpWebRequest, completely unrelated to the DefaultConnectionLimit, that only affects IL2CPP builds.

    Any ideas?
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    This sounds like a bug with IL2CPP. Can you submit a bug report with a project that reproduces this issue?
     
  3. MapMan

    MapMan

    Joined:
    Oct 27, 2013
    Posts:
    38
    Hello @JoshPeterson,

    I worked around the issue completely by using HttpClient instead. This approach works great for me with 1 small problem: HTTP content decompression does not work on Android with mono backend. There's even a bug report for this: https://issuetracker.unity3d.com/issues/android-dllnotfoundexception-monoposixhelper
    Is this something you are aware of and are actively working on resolving?

    Even though I worked around the issue I will try and do a repro for a bug request.

    Regards
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    We are aware of this Android/Mono bug, but we've not started to actively work on it yet. We should be able to correct it though.

    Excellent, I would appreciate that!