Search Unity

Are there any caveats when using HttpClient with IL2CPP?

Discussion in 'iOS and tvOS' started by j-borden, Aug 7, 2015.

  1. j-borden

    j-borden

    Joined:
    Mar 3, 2015
    Posts:
    21
    About 50% of the time, the library I am working on freezes on a certain network call that is making use of HttpClient.SendAsync(). I am working on getting a smaller test case to submit, but this problem only happens on Unity iOS (Mono2x has different problems with EXC_BAD_ACCESS at the moment so cannot confirm if it works there or not, but on Standalone this works fine). When I pause to get the thread stack traces I notice a similar pattern each time:

    1) There are two threads labeled Async I/O Thread that are both blocking on socket receive functionality.
    2) I have one thread of my own blocking on receiving data from a hanging HTTP connection (this is on purpose)
    3) No other threads go through any of my code, which suggests that the problem is between SendAsync and ContinueWith

    Before I get an actual reproduction case ready, is there anything anyone can think of that could be causing this? Does IL2CPP hard limit to two HTTP connections the way that Mono does by default? Can this be altered?

    Thanks!
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    @j.borden

    I'm not aware of what might be causing this, and I don't think that it is something we've seen yet with IL2CPP. The behavior of IL2CPP should be the same as it is with Mono in this case, and IL2CPP does not expose any way to modify it.
     
  3. xmedeko

    xmedeko

    Joined:
    Jun 6, 2018
    Posts:
    23
    I have probably similar issue with IL2CPP and Unity 2019.1.0f2. Some await HttpClient.SendAsync() works and some get stuck. Probably the problem is when await HttpClient.SendAsync() is called from non-main thread, i.e. without Unity SynchronizationContext. The process (window) cannot be closed, probably waiting for SendAsync() to finish.
    I'll try to isolate the issue to some example project.
     
    JoshPeterson likes this.
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Please submit a bug report if you can isolate this, thanks!
     
  5. xmedeko

    xmedeko

    Joined:
    Jun 6, 2018
    Posts:
    23
    JoshPeterson likes this.
  6. xmedeko

    xmedeko

    Joined:
    Jun 6, 2018
    Posts:
    23
    Another problem is, that the
    HttpClient
    ignores Windows proxy settings. I have found a workaround for the Mono build:

    Code (CSharp):
    1. var handler = new HttpClientHandler();
    2. handler.UseDefaultCredentials = true;
    3. handler.Proxy = System.Net.WebRequest.DefaultWebProxy;
    4. var client = new HttpClient(handler);
    But it does not work for IL2CPP! I have filled a bug https://fogbugz.unity3d.com/default.asp?1156595_buha3p1v4iq7vpv9
     
  7. xmedeko

    xmedeko

    Joined:
    Jun 6, 2018
    Posts:
    23
  8. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Thanks for the bugs, we will investigate them.
     
  9. kei233

    kei233

    Joined:
    Dec 16, 2019
    Posts:
    59
    Hey, this still happen in 2021.3.10, have you found any solutions for IL2CPP build?
    acording to the fogbugz, they are not going to fixed this.
     
  10. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    393
    I’m using 2020.3.30 and encountered an issue about HttpClient. The process may become freezing for a while when it calls Task.Delay in main thread and receive response from HttpClient in a background thread.

    Actually, I found the root cause: the timers created in the background thread leaks and it has been fixed in 2021.2 and above. Hope the fix can be backported to 2020.

    I submitted a bug report for it: IN-16679

    UPDATE:
    They decided to not backport the fix to 2020: https://issuetracker.unity3d.com/is...hen-using-an-http-server-to-download-a-bundle
     
    Last edited: Jan 31, 2023
    JoshPeterson likes this.