Search Unity

Bug (Case 1377750)100% reproduceable major crash issue in 2021.2.0f1, using il2cpp, HttpClient and await

Discussion in 'Scripting' started by Carpet_Head, Nov 2, 2021.

  1. Carpet_Head

    Carpet_Head

    Joined:
    Nov 27, 2014
    Posts:
    258
    Trying to draw attention to my bug report with repro ID: (Case 1377750)

    In an il2cpp build, the below code will 100% crash the Player. If there is an exception from the web side of things, unity will crash, guaranteed.

    Even a valid post request will cause this crash if the user has no internet. Please help!

    Code (CSharp):
    1.  
    2. public class ExceptionTest : MonoBehaviour
    3. {
    4.     private HttpClient client;
    5.  
    6.     private void Start()
    7.     {
    8.         client = new HttpClient();
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         if (Time.frameCount % 60 == 0)
    15.         {
    16.             Task.Run(() =>
    17.             {
    18.                 PostDataAsync("https://www.test.com/post", new byte[8]);
    19.             });
    20.         }
    21.  
    22.     }
    23.  
    24.  
    25.     public async void PostDataAsync(string url, byte[] data)
    26.     {
    27.         try
    28.         {
    29.             ByteArrayContent byteContent = new ByteArrayContent(data, 0, data.Length);
    30.             HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url)
    31.             {
    32.                 Content = byteContent
    33.             };
    34.  
    35.             Task<HttpResponseMessage> httpTask = client.SendAsync(request);
    36.  
    37.             using (HttpResponseMessage resp = await httpTask.ConfigureAwait(false))
    38.             {
    39.                 Debug.Log(resp.StatusCode);
    40.             }
    41.         }
    42.         catch (Exception ex) // failed to post async to url
    43.         {
    44.             Debug.LogException(ex);
    45.         }
    46.     }
    47. }
    48.  
     
    Wattosan likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Neat!

    I've always stuck with the reference
    UnityWebRequest
    API, formerly known as
    WWW
    .

    Not sure how much of a refactor that would be, but it's one option.

    "When in Rome..."
     
  3. Carpet_Head

    Carpet_Head

    Joined:
    Nov 27, 2014
    Posts:
    258
    there are some tricky things to do with http headers, proxies and other stuff that make the HttpClient necessary for our use case unfortunately
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,007
    Your test case looks strange. Why do you wrap everything in a Task when you then also do an async http request and again using ConfigureAwait(false)? This could cause all sorts of unpredicable behaviour. However I just copied your script as it is into my test project, created a windows standalone build with IL2CPP backend and the game doesn't crash. Of course I get a lot of exceptions in the player log but the game is still responsive and just runs. Note that I have vsync enabled and a refresh rate of 60Hz. So I would roughly create a new task every second. Though the threadpool has an internal limit so requests could pile up as each request may take up to 30 seconds until they timeout. If you're running on a platform without vsync you may get framerates in the range of several thousands. So you would spam web requests like crazy. Why do you send a request every 60 frames?

    When testing in the editor the exceptions and timeouts have a delay of about 30 seconds, even after the playmode has been stopped as all those threads are still running. That's why I generally avoid using the internal threadpools. Lowering the timeout to 3 seconds will cause the exceptions to show up faster.
     
  5. Carpet_Head

    Carpet_Head

    Joined:
    Nov 27, 2014
    Posts:
    258
    It's a test case. It's designed to reproduce a bug it's not real code
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,007
    Fine, but it does not reproduce the bug, at least not for me ^^. I was testing on Unity 2021.1.24b on a windows10 machine. It runs fine inside the editor and also as a standalone build with IL2CPP backend. You haven't given any information about your testing setup. What platform, does it crash immediately after start? What does the player log contain?

    Note that the actual implementation of the HttpClient would differ heavily depending on the target platform.
     
  7. Carpet_Head

    Carpet_Head

    Joined:
    Nov 27, 2014
    Posts:
    258
    the issue is with 2021.2.0f1. I don't know if it affects other versions. If I turn off my internet, the build crashes immediately. It works fine in the editor, it's only in builds. Both android and windows have the issue. It's caused by some issue generating a stacktrace for an exception
     
  8. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    I'll raise this issue with our QA team, thanks!
     
    Bunny83 likes this.
  9. Carpet_Head

    Carpet_Head

    Joined:
    Nov 27, 2014
    Posts:
    258
    Thanks!
     
  10. Skibitsky

    Skibitsky

    Joined:
    Mar 22, 2016
    Posts:
    24
    Hello @JoshPeterson,

    We also experience crashes with Android IL2CPP builds made in 2021.2.0f1. They only happen when device is not connected to the Internet. The app might crash on launch or at random point when running, and there is nothing helpful in the logs.

    We have a library, that depends on HttpClient and Tasks a lot, which among other things runs connectivity checks in background with fixed interval. After we removed that library, the crashes continued to occur, but less often. There are a few more dependencies in our project that rely on HttpClient internally (e.g. Sentry), we may try removing all of them to confirm that the issue is caused by HttpClient.

    The crashes don't happen in Mono build. Before upgrading to 2021.2.0f1we used 2019.4 LTS and it worked fine offline.

    Have you been able to reproduce the issue? Is there a workaround for it?
     
  11. Carpet_Head

    Carpet_Head

    Joined:
    Nov 27, 2014
    Posts:
    258
  12. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    This does sounds very much like the problem @Carpet_Head linked to. The fix is in process toward releases now.
     
    Wattosan likes this.
  13. Wattosan

    Wattosan

    Joined:
    Mar 22, 2013
    Posts:
    460
    I'd just like to confirm that once we downgraded our project to 2021.1.28f1, the issue was gone and everything is working as expected.

    Can you give a rough estimate on when can we expect access to the release?
     
  14. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    I would expect it within the next Unity 2021.2 release or two. It is in the process of landing in a release branch now. It might happen that a release is cut before this lands, but if that is the case, the fix should be in the release after that.
     
    Skibitsky and Wattosan like this.