Search Unity

Bug Three new exceptions in Unity IAP and Services Core

Discussion in 'Unity IAP' started by smithmj5, Nov 3, 2022.

  1. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    143
    Hi,

    I'm using Unity In App Purchasing v4.5.1 and Services Core 1.5.2 in Unity 2020.3.40f1.

    I'm getting three different exceptions in Unity Crash Reporting dashboard. These are all only happening on Android (they do not occur on iOS).

    All of these are new since I updated the IAP and Services Core plugins.

    Happy to share a link to my project/dashboard - all of these are happening on the same game/project, just let me know.

    Thanks!

    Code (CSharp):
    1.  
    2. [SIZE=4]ArgumentException: An item with the same key has already been added. Key: 42[/SIZE]
    3. Unity.Services.Core.Scheduler.Internal.ActionScheduler.ScheduleAction (System.Action action, System.Double delaySeconds) (at <00000000000000000000000000000000>:0)
    4.  
    5. Managed Stack Trace:
    6.  
    7. System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <00000000000000000000000000000000>:0)
    8. Unity.Services.Core.Scheduler.Internal.ActionScheduler.ScheduleAction (System.Action action, System.Double delaySeconds) (at <00000000000000000000000000000000>:0)
    9. Unity.Services.Core.Telemetry.Internal.TelemetryHandler`2[TPayload,TEvent].ScheduleSendingLoop () (at <00000000000000000000000000000000>:0)
    10. Unity.Services.Core.Telemetry.Internal.TelemetryHandler`2[TPayload,TEvent].Register (TEvent telemetryEvent) (at <00000000000000000000000000000000>:0)
    11. Unity.Services.Core.Telemetry.Internal.Metrics.Unity.Services.Core.Telemetry.Internal.IMetrics.SendHistogramMetric (System.String name, System.Double time, System.Collections.Generic.IDictionary`2[TKey,TValue] tags) (at <00000000000000000000000000000000>:0)
    12. UnityEngine.Purchasing.Telemetry.TelemetryMetricsInstanceWrapper.SendMetric (UnityEngine.Purchasing.Telemetry.TelemetryMetricTypes metricType, System.String metricName, System.Double metricTimeSeconds) (at <00000000000000000000000000000000>:0)
    13. UnityEngine.Purchasing.Telemetry.TelemetryMetricEvent.StopAndSendMetric () (at <00000000000000000000000000000000>:0)
    14. UnityEngine.Purchasing.GooglePlayStoreService.OnConnected () (at <00000000000000000000000000000000>:0)
    15. System.Action.Invoke () (at <00000000000000000000000000000000>:0)
    16. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <00000000000000000000000000000000>:0)
    17. UnityEngine.AndroidJavaProxy.Invoke (System.String methodName, System.Object[] args) (at <00000000000000000000000000000000>:0)
    18. UnityEngine._AndroidJNIHelper.InvokeJavaProxyMethod (UnityEngine.AndroidJavaProxy proxy, System.IntPtr jmethodName, System.IntPtr jargs) (at <00000000000000000000000000000000>:0)
    19. Rethrow as TargetInvocationException: UnityEngine.Purchasing.BillingClientStateListener.onBillingSetupFinished(UnityEngine.AndroidJavaObject)
    20. UnityEngine.AndroidJavaProxy.Invoke (System.String methodName, System.Object[] args) (at <00000000000000000000000000000000>:0)
    21. UnityEngine._AndroidJNIHelper.InvokeJavaProxyMethod (UnityEngine.AndroidJavaProxy proxy, System.IntPtr jmethodName, System.IntPtr jargs) (at <00000000000000000000000000000000>:0)
    Code (CSharp):
    1. IndexOutOfRangeException: Index was outside the bounds of the array.
    2. Unity.Services.Core.Telemetry.Internal.MetricsPayload.Unity.Services.Core.Telemetry.Internal.ITelemetryPayload.Add (Unity.Services.Core.Telemetry.Internal.ITelemetryEvent telemetryEvent) (at <00000000000000000000000000000000>:0)
    3.  
    4. Managed Stack Trace:
    5.  
    6. System.Collections.Generic.List`1[T].Add (T item) (at <00000000000000000000000000000000>:0)
    7. Unity.Services.Core.Telemetry.Internal.MetricsPayload.Unity.Services.Core.Telemetry.Internal.ITelemetryPayload.Add (Unity.Services.Core.Telemetry.Internal.ITelemetryEvent telemetryEvent) (at <00000000000000000000000000000000>:0)
    8. Unity.Services.Core.Telemetry.Internal.CacheExtensions.Add[TPayload] (Unity.Services.Core.Telemetry.Internal.CachedPayload`1[TPayload] self, Unity.Services.Core.Telemetry.Internal.ITelemetryEvent telemetryEvent) (at <00000000000000000000000000000000>:0)
    9. Unity.Services.Core.Telemetry.Internal.TelemetryHandler`2[TPayload,TEvent].Register (TEvent telemetryEvent) (at <00000000000000000000000000000000>:0)
    10. Unity.Services.Core.Telemetry.Internal.Metrics.Unity.Services.Core.Telemetry.Internal.IMetrics.SendHistogramMetric (System.String name, System.Double time, System.Collections.Generic.IDictionary`2[TKey,TValue] tags) (at <00000000000000000000000000000000>:0)
    11. UnityEngine.Purchasing.Telemetry.TelemetryMetricsInstanceWrapper.SendMetricByType (UnityEngine.Purchasing.Telemetry.TelemetryMetricParams metricParams) (at <00000000000000000000000000000000>:0)
    12. UnityEngine.Purchasing.Telemetry.TelemetryMetricsInstanceWrapper.SendMetric (UnityEngine.Purchasing.Telemetry.TelemetryMetricTypes metricType, System.String metricName, System.Double metricTimeSeconds) (at <00000000000000000000000000000000>:0)
    13. UnityEngine.Purchasing.Telemetry.TelemetryMetricEvent.StopAndSendMetric () (at <00000000000000000000000000000000>:0)
    14. UnityEngine.Purchasing.MetricizedGooglePlayStoreService.DequeueFetchPurchases () (at <00000000000000000000000000000000>:0)
    15. System.Action.Invoke () (at <00000000000000000000000000000000>:0)
    16. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <00000000000000000000000000000000>:0)
    17. System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <00000000000000000000000000000000>:0)
    18. UnityEngine.AndroidJavaProxy.Invoke (System.String methodName, System.Object[] args) (at <00000000000000000000000000000000>:0)
    19. UnityEngine._AndroidJNIHelper.InvokeJavaProxyMethod (UnityEngine.AndroidJavaProxy proxy, System.IntPtr jmethodName, System.IntPtr jargs) (at <00000000000000000000000000000000>:0)
    20. Rethrow as TargetInvocationException: UnityEngine.Purchasing.BillingClientStateListener.onBillingSetupFinished(UnityEngine.AndroidJavaObject)
    21. UnityEngine.AndroidJavaProxy.Invoke (System.String methodName, System.Object[] args) (at <00000000000000000000000000000000>:0)
    22. UnityEngine._AndroidJNIHelper.InvokeJavaProxyMethod (UnityEngine.AndroidJavaProxy proxy, System.IntPtr jmethodName, System.IntPtr jargs) (at <00000000000000000000000000000000>:0)
    Code (CSharp):
    1. [ServicesCore]: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException (System.ExceptionResource resource) [0x00000] in <00000000000000000000000000000000>:0 at System.Collections.Generic.List`1+Enumerator[T].MoveNextRare () [0x00000] in <00000000000000000000000000000000>:0 at System.Collections.Generic.List`1+Enumerator[T].MoveNext () [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList (Newtonsoft.Json.JsonWriter writer, System.Collections.IEnumerable values, Newtonsoft.Json.Serialization.JsonArrayContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract collectionContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject (Newt
    2. Unity.Services.Core.Scheduler.Internal.ActionScheduler:ExecuteExpiredActions()
    3.  
    4. Managed Stack Trace:
    5.  
    6. Unity.Services.Core.Scheduler.Internal.ActionScheduler:ExecuteExpiredActions()
    7. UnityEngine.LowLevel.UpdateFunction:Invoke()
     
    damelin likes this.
  2. Yannick_D

    Yannick_D

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    231
    Hello,

    Thank you for the report, I have forwarded this issue to Services Core so they can investigate it on their end.

    On the IAP side, we have an update coming in 4.5.2 that will catch these exceptions (an error will be logged) to make sure that we recover from them.
    However, the InvalidOperationException doesn't seem to originate from a call we do, so that one won't be caught.
     
  3. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    143
    Excellent news, thanks for the update!

    Is there an estimate on when 4.5.2 will be available? Google does not look favorably on updates that cause a lot more crashes than previous updates (which this one is doing for me currently). Or can the fixes be shared somewhere so I can manually apply them locally in the meantime?

    For the InvalidOperationException, I made a new thread about it (linking so anyone else having same issue can follow along): https://forum.unity.com/threads/services-core-exception.1356902/
     
    Last edited: Nov 3, 2022
  4. Yannick_D

    Yannick_D

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    231
    4.5.2 will be released later this month.

    In the meantime, you could do these modifications:
    TelemetryDiagnosticsInstanceWrapper.cs
    • Try/catch inside SendDiagnostic
    • util.RunOnMainThread(() => SendDiagnostic(diagnosticParams));
    TelemetryMetricsInstanceWrapper.cs
    • Try/catch inside SendMetricByType
    • util.RunOnMainThread(() => SendMetricByType(metricParams));
    The try/catch should handle all the exceptions.
    The other issue is still being looked at, but all these issues seem to be related to Telemetry not being fully thread safe. We will be running both these functions with RunOnMainThread to solve this until it becomes thread safe. This also requires modifying StandardPurchasingModule.cs to pass util to both Telemetry[...]InstanceWrapper.
     
  5. damelin

    damelin

    Joined:
    Jul 3, 2012
    Posts:
    65
    Until things get more stable around ServicesCore/Purchase, is there a way to disable telemetry-related code from the purchase package? I'm pretty sure that I don't need this.

    If not, I guess my safest option is to revert InAppPurchase to pre-UnityGamingServices version (v4.1.5)?

    Edit: I have just read that UnityServices.InitializeAsync() is optional (but it will output a scary warning). If I skip it, do you thing that would prevent the issues with the telemetry?
     
    Last edited: Nov 22, 2022
  6. Brown2Fox

    Brown2Fox

    Joined:
    Mar 6, 2017
    Posts:
    7
    You can do following:

    Code (CSharp):
    1.  
    2. const string TelemetryDisabledKey = "com.unity.services.core.telemetry-disabled";
    3.              
    4. var options = new InitializationOptions()
    5.     .SetEnvironmentName("production")
    6.     .SetOption(TelemetryDisabledKey, true);
    7.              
    8. await UnityServices.InitializeAsync(options);
    9.  
     
    Darkgisba1, -chris and damelin like this.
  7. OleksiiSB

    OleksiiSB

    Joined:
    Jan 8, 2021
    Posts:
    4
  8. Yannick_D

    Yannick_D

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    231
    -chris likes this.
  9. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    143
    So should we wait and update Services Core and IAP at the same time? Or does Services Core fix some issues, and the IAP update fixes other ones?
     
  10. Yannick_D

    Yannick_D

    Unity Technologies

    Joined:
    Feb 21, 2022
    Posts:
    231
    Both packages solved the issue independently, so only one needs to be updated.

    For Services Core, they improved the thread safety.
    For IAP, we added protection to catch the exceptions and also moved those calls to the main thread to avoid the issue entirely.
     
    lucbloom, smithmj5 and -chris like this.
  11. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    143
    Thanks for the info, that's fantastic news!

    Going to update Services Core today in my games. :)
     
    lucbloom likes this.