googleads-mobile-unity: [UMP + MobileAds] MobileAds.RaiseAdEventsOnUnityMainThread = true does not guarantee that the callbacks will be on the Unity main thread, leading to unknown crashes.

[REQUIRED] Step 1: Describe your environment

  • Unity version: 2020.3.45f1
  • Google Mobile Ads Unity plugin version: 8.6.0
  • Platform: Android
  • Platform OS version: Android 13
  • Any specific devices issue occurs on:
  • Mediation ad networks used, and their versions:

[REQUIRED] Step 2: Describe the problem

I have upgraded from 8.4.1 version to 8.6.0 today and implemented the UMP workflow. However I am getting crashes in my app which seems to be due to order of execution of MobileAds.RaiseAdEventsOnUnityMainThread and ConsentInformation.Update() method.

If the order in the code is as follows, everything works correctly:

MobileAds.RaiseAdEventsOnUnityMainThread = true;
ConsentInformation.Update(request, OnConsentInfoUpdated);

But if the order is as follows:

ConsentInformation.Update(request, OnConsentInfoUpdated);

void OnConsentInfoUpdated(FormError consentError)
{
        ConsentForm.LoadAndShowConsentFormIfRequired((FormError formError) =>
        {
            // Consent has been gathered.
            MobileAds.RaiseAdEventsOnUnityMainThread = true;
            MobileAds.Initialize(initStatus =>
            {
               ...............................
            });
        });
}

When above code is used, it seems it does not capture the Unity main thread correctly and the ad callbacks are not called on the main thread. Therefore, if there is some code in the callback which needs to be on the main thread (like a TextMeshProUGUI etc.) it causes a fatal crash.

I think the race condition of #2841 is not fixed completely. It seems the Unity main thread is set in a static constructor of MobileAds class. From the docs (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors) – The user has no control on when the static constructor is executed in the program. If the static constructor gets called at the wrong time, then it may be possible that wrong values are captured by it. This can cause unexpected crashes which can be very hard to trace. Either the constructor needs to be forced to be executed at the correct time (by accessing a static property or method) or a different solution needs to be found. If this is working as expected, then perhaps the docs should be updated to reflect that the RaiseAdEventsOnUnityMainThread flag (or any other static property/method) should be set before any calls to UMP api.

This is just my basic analysis based on the issues that I faced. Happy to be corrected if I went wrong somewhere.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 23 (5 by maintainers)

Most upvoted comments

@shobhit-samaria

I will look into a fix for this. For the time being please use the suggested work around:

MobileAds.RaiseAdEventsOnUnityMainThread = true;
ConsentInformation.Update(request, OnConsentInfoUpdated);

Thank you. Looking forward to a robust solution. I have updated the title to better reflect the issue.