Analytics crashes the game at shutdown

See the attached player log. In there you see that the analytics events at startup get send successfully. When I close the game (without doing anything else) the game actually crashes.
It is in the creation of a webrequest by the analytics system.

Unity 2023.2.0f1 - Analytics 5.0.1

What can I do to fix this?

9480196–1333099–Player.log.txt (38.4 KB)

I have the same issue, but with Unity 2023.2.0,b14 the problem does not occur.

1 Like

Hi @grrava

Could you provide a little more information on what you are testing on?
It looks like you are testing on a touch enabled windows machine?

It seems to be crashing maybe on attempt to flush the events when you close the game.
I will try to reproduce using the versions you mentioned and post back here.

It indeed crashes when flushing the events I think. I avoided the issue by disabling analytics at shutdown, but because of that I’ll never get a gameEnded event. But at least it does not crash.

The device I’m testing on is a very classic desktop with windows 10, no touch enabled. I am using a bluetooth mouse.

Hi @grrava thanks, I was indeed able to reproduce the problem. I will report the problem to the team. Not sure if trying another version of the editor is an option at this point?

Thx @SebT_Unity , unfortunately it is indeed not an option to use another version for me, since that version fixed another issue I really need fixed.

Hi. I confirm we are observing this crash too, with Unity 2022.3.14f1 LTS, for Windows builds. Haven’t tried yet to see if it happens on other platforms. It did not seem to occur on 2021.3.22f1 LTS.
Cheers,
Charles

Here’s the stacktrace:

0x00007FFCDC51C433 (UnityPlayer) DebugStringToFilePostprocessedStacktrace
0x00007FFCDC51BC30 (UnityPlayer) DebugStringToFile
0x00007FFCDBD7DABD (UnityPlayer) UnityWebRequest::UnityWebRequest
0x00007FFCDBD7EB15 (UnityPlayer) UnityWebRequest::Create
0x00007FFCDAB85E9A (UnityPlayer) UnityWebRequest_CUSTOM_Create
0x000001DCAC357326 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.Networking.UnityWebRequest:Create ()
0x000001DCAC35709B (Mono JIT Code) [C:\build\output\unity\unity\Modules\UnityWebRequest\Public\UnityWebRequest.bindings.cs:196] UnityEngine.Networking.UnityWebRequest:.ctor (string,string)
0x000001DCAC7717D3 (Mono JIT Code) [.\Library\PackageCache\com.unity.services.analytics@5.0.1\Runtime\Runtime\WebRequestHelper.cs:22] Unity.Services.Analytics.Internal.AnalyticsWebRequest:.ctor (string,string)
0x000001DCAC7714BB (Mono JIT Code) [.\Library\PackageCache\com.unity.services.analytics@5.0.1\Runtime\Runtime\WebRequestHelper.cs:43] Unity.Services.Analytics.Internal.WebRequestHelper:CreateWebRequest (string,string,byte[])
0x000001DCAC770284 (Mono JIT Code) [.\Library\PackageCache\com.unity.services.analytics@5.0.1\Runtime\Runtime\Dispatcher.cs:72] Unity.Services.Analytics.Internal.Dispatcher:FlushBufferToService ()
0x000001DCAC76FB8B (Mono JIT Code) [.\Library\PackageCache\com.unity.services.analytics@5.0.1\Runtime\Runtime\Dispatcher.cs:54] Unity.Services.Analytics.Internal.Dispatcher:Flush ()
0x000001DCAC73FA73 (Mono JIT Code) [.\Library\PackageCache\com.unity.services.analytics@5.0.1\Runtime\AnalyticsServiceInstance.cs:323] Unity.Services.Analytics.AnalyticsServiceInstance:Flush ()
0x000001DCC38EE2AB (Mono JIT Code) [.\Library\PackageCache\com.unity.services.analytics@5.0.1\Runtime\AnalyticsServiceInstance.cs:372] Unity.Services.Analytics.AnalyticsServiceInstance:ApplicationQuit ()
0x000001DCC38EE06B (Mono JIT Code) [.\Library\PackageCache\com.unity.services.analytics@5.0.1\Runtime\Runtime\AnalyticsContainer.cs:113] Unity.Services.Analytics.AnalyticsContainer:OnDestroy ()
0x000001DCAB9C72A8 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007FFCF9E44B6E (mono-2.0-bdwgc) mono_jit_set_domain
0x00007FFCF9D7D1F4 (mono-2.0-bdwgc) mono_object_get_virtual_method
0x00007FFCF9D7D36C (mono-2.0-bdwgc) mono_runtime_invoke
0x00007FFCDB88C06D (UnityPlayer) scripting_method_invoke
0x00007FFCDB87146C (UnityPlayer) ScriptingInvocation::Invoke
0x00007FFCDB871706 (UnityPlayer) ScriptingInvocation::InvokeChecked
0x00007FFCDB8CA6D5 (UnityPlayer) SerializableManagedRef::CallMethod
0x00007FFCDB86C67C (UnityPlayer) MonoBehaviour::WillDestroyComponent
0x00007FFCDADD990D (UnityPlayer) GameObject::WillDestroyGameObject
0x00007FFCDB3443DA (UnityPlayer) PreDestroyRecursive
0x00007FFCDB342AE0 (UnityPlayer) DestroyObjectHighLevel_Internal
0x00007FFCDB3428D6 (UnityPlayer) DestroyObjectHighLevel
0x00007FFCDB34DC5A (UnityPlayer) CleanupAllObjects
0x00007FFCDB34E58D (UnityPlayer) CleanupEngine
0x00007FFCDB372172 (UnityPlayer) PlayerCleanup
0x00007FFCDB90E061 (UnityPlayer) PerformMainLoop
0x00007FFCDB90CFBB (UnityPlayer) MainMessageLoop
0x00007FFCDB9136CD (UnityPlayer) UnityMainImpl
0x00007FFCDB9156CB (UnityPlayer) UnityMain
0x00007FF6BFCD11F2 (GAME_NAME) __scrt_common_main_seh
0x00007FFE5B03257D (KERNEL32) BaseThreadInitThunk
0x00007FFE5C7AAA58 (ntdll) RtlUserThreadStart

Can you please help on finding at least a workaround? We’re on latest Analytics version and every single exit of the game from all players and QA team provokes this crash, flooding JIRA with thousands of undesired, unfixable (it seems) entries.

Ideally, I’d want to disable completely Analytics right before the game exits, to avoid sending web requests upon exit, as it seems like it’s the cause of the issue?

Note that this stacktrace is from a debug Mono build (for faster build times), but it happens in the same way with similar stacktrace in IL2CPP (used for release builds).

Thank you.

Alright, some progress here. I think I have found a solution. We had not yet upgraded the code to use the new consent flow, we were still using:

UGSconsentIdentifiers = await Unity.Services.Analytics.AnalyticsService.Instance.CheckForRequiredConsents();

We migrated to the new consent flow for latest Analytics version, which allows us now to call this function when clicking the Exit button of the game:

AnalyticsService.Instance.StopDataCollection();

This at least solves many crashes upon “proper game exit”.

Note: The migration mentioned in the link above is mandatory, otherwise it’ll use the old flow and StopDataCollection() will do nothing!

However, it still crashed if ALT+F4 was pressed, because not using the Exit button. By adding StopDataCollection() to OnApplicationQuit(), it also solves the crash upon hard exit.

So, it’s definitely related to Analytics still trying to perform stuff OnApplicationQuit(). This might be something Unity will want to solve to avoid everyone having to perform these changes. I didn’t see the documentation mentioning calling StopDataCollection() was mandatory before exiting the game, so I don’t think this is intended?

Thank you!

@grrava and @manuelgoellnitz You may want to try this out in case it fixes it for you for now :wink:

1 Like

I can confirm the crashes as well on iOS. Need to test a new build tomorrow with the fix you posted.

1 Like

Indeed, the crashes stopped after putting StopDataCollection() to OnApplicationQuit(). I put a FlushEvents() before stopping the data collection to still get events that have not been sent previously.

Hey folks, 5.0.2 has been released targeting this bug. Can you try updating and letting us know if this resolves the issue? Initial testing looked good in our tests but would like to have your feedback.

1 Like

Hey Seb, do I interpret the release notes right that this is only targeting 2023.x?
Analytics no longer throws a fatal exception on shutdown for builds made with 2023 editor versions

I ask because I got the crashes with builds made on 2022.3.14f1.

From my side I can confirm that Analytics version 5.0.2 fixes the crashes on shutdown. Nice work!

2 Likes

For me, 5.0.2. also seems to work. :slight_smile:

Indeed, the issue seems to be fixed in 5.0.2. Thanks!

1 Like

I was able to reproduce this with Unity 2021.3.34 LTS and Analytics 4.4.2 on iOS.

@SebT_Unity Could we get this fix back-ported to version 4 for those of us that don’t want to update to the new consent flow yet?

Pretty sure we are having this bug on 2022.3.17

Thread 0 name:
Thread 0 Crashed:
0   libsystem_kernel.dylib            0x00000001e7d49fbc __pthread_kill + 8 (:-1)
1   libsystem_pthread.dylib           0x000000020a6cf680 pthread_kill + 268 (pthread.c:1681)
2   libsystem_c.dylib                 0x00000001a8644dd0 raise + 32 (raise.c:30)
3   UnityFramework                    0x0000000107209144 DebugStringToFilePostprocessedStacktrace(DebugStringToFileData const&) + 1228 (LogAssert.cpp:1134)
4   UnityFramework                    0x000000010720960c DebugStringToFile(DebugStringToFileData const&) + 600 (LogAssert.cpp:1198)
5   UnityFramework                    0x0000000106e6b5bc DebugStringToFile<core::basic_string<char> > + 84 (LogAssert.h:148)
6   UnityFramework                    0x0000000106e6b5bc UnityWebRequestManager::Register(UnityWebRequest*) + 236 (UnityWebRequestManager.cpp:83)
7   UnityFramework                    0x0000000106e6b4a0 UnityWebRequest::UnityWebRequest(MemLabelId) + 52 (UnityWebRequest.cpp:250)
8   UnityFramework                    0x000000010ac72b64 UnityWebRequest_Create_m0180FA724ACCE19C0C761A8B7AF280A489639CE9 + 36 (UnityEngine.UnityWebRequestModule.cpp:5581)
9   UnityFramework                    0x000000010ac72b64 UnityWebRequest__ctor_mD4739B0BF1C4937479A83B24B531C6B819712A3E + 80 (UnityEngine.UnityWebRequestModule.cpp:5647)
10  UnityFramework                    0x000000010a8a0980 AnalyticsWebRequest__ctor_m8CA36BAF33C32134D350FC88E7BF795AFFDFD77D + 16 (Unity.Services.Analytics.cpp:13791)
11  UnityFramework                    0x000000010a8a0980 WebRequestHelper_CreateWebRequest_m06C697E4C704BADC84F815F3F84A4144708DDFFA + 152 (Unity.Services.Analytics.cpp:13851)
12  UnityFramework                    0x000000010a8a033c InterfaceFuncInvoker3<Il2CppObject*, String_t*, String_t*, ByteU5BU5D_tA6237BF417AE52AD70CFB4EF24A7A82613DF9031*>::Invoke(unsigned short, Il2CppClass*, Il2CppObject*, String_t*, String_t*, ByteU5BU... + 40 (Unity.Services.Analytics.cpp:218)
13  UnityFramework                    0x000000010a8a033c Dispatcher_FlushBufferToService_m55AC50B02B30BD4ED1E554D794AA5DEE9FB77C39 + 628 (Unity.Services.Analytics.cpp:13528)
14  UnityFramework                    0x000000010a894fac InterfaceActionInvoker0::Invoke(unsigned short, Il2CppClass*, Il2CppObject*) + 32 (Unity.Services.Analytics.cpp:97)
15  UnityFramework                    0x000000010a894fac AnalyticsServiceInstance_Flush_mB8F0ED919FB47026C5CB61DD3562FAA09009C21E + 932 (Unity.Services.Analytics.cpp:4602)
16  UnityFramework                    0x000000010a898e54 AnalyticsContainer_OnDestroy_m27C97541D5D0E67B2F4FA9D110EFDD12C88866DD + 68 (Unity.Services.Analytics.cpp:7478)
17  UnityFramework                    0x0000000107ea8764 il2cpp::vm::Runtime::InvokeWithThrow(MethodInfo const*, void*, void**) + 100 (Runtime.cpp:606)
18  UnityFramework                    0x0000000107ea86ac il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**) + 84 (Runtime.cpp:592)
19  UnityFramework                    0x0000000106cca6ac scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool) + 124 (ScriptingApi_Il2Cpp.cpp:290)
20  UnityFramework                    0x0000000106cd7a7c ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool) + 120 (ScriptingInvocation.cpp:298)
21  UnityFramework                    0x0000000106cda258 Invoke + 12 (ScriptingInvocation.h:72)
22  UnityFramework                    0x0000000106cda258 ScriptingInvocation::InvokeChecked(ScriptingExceptionPtr*) + 60 (ScriptingInvocation.cpp:123)
23  UnityFramework                    0x0000000106d28774 SerializableManagedRef::CallMethod(Object&, ScriptingMethodPtr) + 96 (SerializableManagedRef.cpp:623)
24  UnityFramework                    0x0000000106ce6310 CallMethodInactive + 12 (MonoBehaviour.h:81)
25  UnityFramework                    0x0000000106ce6310 MonoBehaviour::WillDestroyComponent() + 340 (MonoBehaviour.cpp:333)
26  UnityFramework                    0x0000000106b082d0 GameObject::WillDestroyGameObject() + 56 (GameObject.cpp:114)
27  UnityFramework                    0x0000000106bd4800 PreDestroyRecursive(GameObject&, unsigned long*) + 160 (GameObjectUtility.cpp:1774)
28  UnityFramework                    0x0000000106bd5090 DestroyGameObjectHierarchy + 28 (GameObjectUtility.cpp:1841)
29  UnityFramework                    0x0000000106bd5090 DestroyObjectHighLevel_Internal(Object*, bool) + 392 (GameObjectUtility.cpp:2032)
30  UnityFramework                    0x0000000106bcdf58 CleanupAllObjects() + 284 (SaveAndLoadHelper.cpp:444)
31  UnityFramework                    0x0000000106bce62c CleanupEngine() + 44 (SaveAndLoadHelper.cpp:584)
32  UnityFramework                    0x0000000106bddd70 PlayerCleanup(bool) + 72 (Player.cpp:698)
33  UnityFramework                    0x0000000107299bc4 UnityCleanup + 16 (LibEntryPoint.mm:221)
34  UnityFramework                    0x0000000105023e28 -[UnityAppController applicationWillTerminate:] + 36 (UnityAppController.mm:531)

Seems to be the same you are talking about, anybody else?