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

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!

public class ExceptionTest : MonoBehaviour
{
    private HttpClient client;

    private void Start()
    {
        client = new HttpClient();
    }

    // Update is called once per frame
    void Update()
    {
        if (Time.frameCount % 60 == 0)
        {
            Task.Run(() =>
            {
                PostDataAsync("https://www.test.com/post", new byte[8]);
            });
        }

    }


    public async void PostDataAsync(string url, byte[] data)
    {
        try
        {
            ByteArrayContent byteContent = new ByteArrayContent(data, 0, data.Length);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = byteContent
            };

            Task<HttpResponseMessage> httpTask = client.SendAsync(request);

            using (HttpResponseMessage resp = await httpTask.ConfigureAwait(false))
            {
                Debug.Log(resp.StatusCode);
            }
        }
        catch (Exception ex) // failed to post async to url
        {
            Debug.LogException(ex);
        }
    }
}

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…”

there are some tricky things to do with http headers, proxies and other stuff that make the HttpClient necessary for our use case unfortunately

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.

It’s a test case. It’s designed to reproduce a bug it’s not real code

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.

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

I’ll raise this issue with our QA team, thanks!

Thanks!

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?

This does sounds very much like the problem @Carpet_Head linked to. The fix is in process toward releases now.

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?

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.