IOS crashing on UnityWebRequest calls

Okay, so this one is causing us to pull our hair out, and causing a poor user experience for about 1/20 of our users. These are only happening on IOS

Essentially we are seeing random intermittent crashes throughout our game. Most prominently right at start. We are getting a few different stack traces, but they all seem related.

This is Unity 5.6.2.p2 and Unity 5.6.2.p4

No pattern of device/OS versions.

Between our previous release and this release we converted our internal CDN class to utilize UnityWebRequests to grab sprites from our CDN, added retry logic, and moved to add more aggressive Resources.UnloadUnusedAssets. (We aren’t using asset bundles in this instance, we are downloading PNGS converting them to textures and applying them).

Our running theory is that Resources.UnloadUnusedAssets is causing heap corruption, which is causing crashes eventually, but this is a tough one to confirm. I can generate crashes by setting up downloads and repeatedly calling UnloadUnusedAssets, but the stack trace is different, so I’m not sure if it’s the exact issue. We can cause images in our app to glitch out with this technique too, which we think is related.

Is there a known issue with unloadUnusedAssets and UnityWebRequest downloads on IOS? I can make a build demonstrating the problems if that helps.

Here are the stack traces…

Crashed: BackgroundWorker
0  libobjc.A.dylib                0x180250150 objc_msgSend + 16
1  Lucktastic                     0x1000c6290 WaitOnCondition(UnityWWWConnectionDelegate*) (WWWConnection.mm:127)
2  Lucktastic                     0x1000c602c UnitySendWWWConnection (WWWConnection.mm:359)
3  Lucktastic                     0x1010c6558 TransportiPhone::DoRequest(core::basic_string<char, core::StringStorageDefault<char> > const&, unsigned long, UnityWebRequestMethod, core::basic_string<char, core::StringStorageDefault<char> > const&, bool, bool, HeaderHelper*, UploadHandler*, DownloadHandler*, ResponseHelper*, bool) (TransportiPhone.cpp:126)
4  Lucktastic                     0x1010c66c4 TransportiPhone::DoRequest(core::basic_string<char, core::StringStorageDefault<char> > const&, unsigned long, UnityWebRequestMethod, core::basic_string<char, core::StringStorageDefault<char> > const&, bool, bool, HeaderHelper*, UploadHandler*, DownloadHandler*, ResponseHelper*) (TransportiPhone.cpp:134)
5  Lucktastic                     0x100d5bd58 UnityWebRequestProto<TransportiPhone, AtomicRefCounter, RedirectHelper, ResponseHelper, DownloadHandler, UploadHandler, HeaderHelper, AsyncOperation>::DoRequest() (WebRequestProto.h:421)
6  Lucktastic                     0x100d5b884 UnityWebRequestProto<TransportiPhone, AtomicRefCounter, RedirectHelper, ResponseHelper, DownloadHandler, UploadHandler, HeaderHelper, AsyncOperation>::Task_DoRequest(void*) (WebRequestProto.h:125)
7  Lucktastic                     0x100d5b910 UnityWebRequestProto<TransportiPhone, AtomicRefCounter, RedirectHelper, ResponseHelper, DownloadHandler, UploadHandler, HeaderHelper, AsyncOperation>::Job_ExecuteUnityWebRequest(UnityWebRequestProto<TransportiPhone, AtomicRefCounter, RedirectHelper, ResponseHelper, DownloadHandler, UploadHandler, HeaderHelper, AsyncOperation>*) (dynamic_array.h:369)
8  Lucktastic                     0x100e87900 JobQueue::Exec(JobInfo*, long long, int) (JobQueue.cpp:355)
9  Lucktastic                     0x100e8786c JobQueue::Steal(JobGroup*, JobInfo*, long long, int, bool) (JobQueue.cpp:561)
10 Lucktastic                     0x100e87c44 JobQueue::ExecuteJobFromQueue() (JobQueue.cpp:718)
11 Lucktastic                     0x100e87cec JobQueue::ProcessJobs(void*) (JobQueue.cpp:767)
12 Lucktastic                     0x100e872b4 JobQueue::WorkLoop(void*) (JobQueue.cpp:852)
13 Lucktastic                     0x100fe11c0 Thread::RunThreadWrapper(void*) (Thread.cpp:35)
14 libsystem_pthread.dylib        0x18089d68c _pthread_body + 240
15 libsystem_pthread.dylib        0x18089d59c _pthread_body + 282
16 libsystem_pthread.dylib        0x18089acb4 thread_start + 4


    
    And this one
    

    Crashed: com.apple.main-thread
    0  libsystem_kernel.dylib         0x189be5014 __pthread_kill + 8
    1  libsystem_pthread.dylib        0x189caf264 pthread_kill + 112
    2  libsystem_c.dylib              0x189b599c4 abort + 140
    3  libsystem_malloc.dylib         0x189c2a9e8 nanozone_default_reader + 330
    4  libsystem_malloc.dylib         0x189c2bc54 _nano_malloc_check_clear + 412
    5  libsystem_malloc.dylib         0x189c2ac38 nano_malloc + 44
    6  libsystem_malloc.dylib         0x189c19664 malloc_zone_malloc + 172
    7  libsystem_malloc.dylib         0x189c1c56c malloc + 32
    8  Lucktastic                     0x100bd2fd8 MemoryManager::LowLevelCAllocate(unsigned long, unsigned long) (MemoryManager.cpp:809)
    9  Lucktastic                     0x100fd2598 bool UnityDefaultAllocator<LowLevelAllocator>::AllocationPage<(RequestType)0>(void const*) const (UnityDefaultAllocator.cpp:205)
    10 Lucktastic                     0x100fd2054 UnityDefaultAllocator<LowLevelAllocator>::RegisterAllocation(void const*) (Mutex.h:30)
    11 Lucktastic                     0x100fd1fc4 UnityDefaultAllocator<LowLevelAllocator>::Allocate(unsigned long, int) (UnityDefaultAllocator.cpp:30)
    12 Lucktastic                     0x100bd203c MemoryManager::Allocate(unsigned long, unsigned long, MemLabelIdentifier, AllocateOptions, char const*, int) (MemoryManager.cpp:1153)
    13 Lucktastic                     0x100f92a14 UnityWebRequestManager::CreateUnityWebRequest() (WebRequestManager.cpp:93)
    14 Lucktastic                     0x100fb6900 UnityWebRequest_CUSTOM_InternalCreate(Il2CppObject*) (WebRequestBindings.gen.cpp:47)
    15 Lucktastic                     0x1007c6a6c UnityWebRequest__ctor_m2378599348 (UnityEngine_UnityEngine_Networking_UnityWebRequest254341728.h:53)
    16 Lucktastic                     0x1007c6cd8 UnityWebRequest_GetTexture_m333882849 (Bulk_UnityEngine_2.cpp:8223)
    17 Lucktastic                     0x1002ff594 U3CExecuteGetTextureU3Ec__Iterator2_MoveNext_m3869864382 (AssemblyU2DCSharp_JumpRampGames_JRGWebRequest_U3CE4115363001.h:65)
    18 Lucktastic                     0x1007d3dd4 SetupCoroutine_InvokeMoveNext_m2975616245 (Bulk_UnityEngine_3.cpp:2915)
    19 Lucktastic                     0x100acebcc RuntimeInvoker_Void_t1841601450_Il2CppObject_IntPtr_t(MethodInfo const*, void*, void**) (Il2CppInvokerTable.cpp:4083)
    20 Lucktastic                     0x1014c27a4 il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**) (Runtime.cpp:505)
    21 Lucktastic                     0x100f00fec scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool) (ScriptingApi_Il2Cpp.cpp:216)
    22 Lucktastic                     0x100efa03c ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool) (ScriptingInvocation.cpp:303)
    23 Lucktastic                     0x100e23cfc Coroutine::InvokeMoveNext(ScriptingExceptionPtr*) (ScriptingInvocation.h:72)
    24 Lucktastic                     0x100e23940 Coroutine::Run(bool*) (Coroutine.cpp:259)
    25 Lucktastic                     0x100fda898 MonoBehaviour::TryCreateAndRunCoroutine(ScriptingObjectPtr, ScriptingMethodPtr, Coroutine**) (MonoBehaviour.cpp:727)
    26 Lucktastic                     0x100fdabbc MonoBehaviour::StartCoroutineManaged2(ScriptingObjectPtr) (MonoBehaviour.cpp:745)
    27 Lucktastic                     0x100fbfd5c MonoBehaviour_CUSTOM_StartCoroutine_Auto_Internal(Il2CppObject*, Il2CppObject*) (MonoBehaviourBindings.gen.cpp:129)
    28 Lucktastic                     0x1002763ec CDN_Download_m1212489378 (Bulk_Assembly-CSharp_1.cpp:12162)
    29 Lucktastic                     0x1002b1b9c DashboardView_ScrollChanged_m115476082 (Bulk_Assembly-CSharp_2.cpp:7979)
    30 Lucktastic                     0x100390418 CreatedMessageSender_SendMessages_m618318556 (Bulk_Atesh.WeNeedCreatedMessage_0.cpp:782)
    31 Lucktastic                     0x100ac9db4 RuntimeInvoker_Void_t1841601450(MethodInfo const*, void*, void**) (Il2CppInvokerTable.cpp:2459)
    32 Lucktastic                     0x1014c27a4 il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**) (Runtime.cpp:505)
    33 Lucktastic                     0x100f00fec scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool) (ScriptingApi_Il2Cpp.cpp:216)
    34 Lucktastic                     0x100efa03c ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool) (ScriptingInvocation.cpp:303)
    35 Lucktastic                     0x100fda578 MonoBehaviour::CallUpdateMethod(int) (MonoBehaviour.cpp:563)
    36 Lucktastic                     0x100d08ba8 void BaseBehaviourManager::CommonUpdate<LateBehaviourManager>() (Behaviour.cpp:209)
    37 Lucktastic                     0x100e0cf70 PlayerLoop() (PlayerLoop.cpp:190)
    38 Lucktastic                     0x101045e98 UnityPlayerLoopImpl(bool) (LibEntryPoint.mm:223)
    39 Lucktastic                     0x10003a5f4 UnityRepaint (UnityAppController+Rendering.mm:271)
    40 Lucktastic                     0x10003a4e0 -[UnityAppController(Rendering) repaintDisplayLink] (UnityAppController+Rendering.mm:78)
    41 QuartzCore                     0x18ddfc5dc CA::Display::DisplayLinkItem::dispatch(unsigned long long) + 44
    42 QuartzCore                     0x18ddfc48c CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 436
    43 IOKit                          0x18ae57b9c IODispatchCalloutFromCFMessage + 372
    44 CoreFoundation                 0x18ab81960 __CFMachPortPerform + 180
    45 CoreFoundation                 0x18ab99ae4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
    46 CoreFoundation                 0x18ab99284 __CFRunLoopDoSource1 + 436
    47 CoreFoundation                 0x18ab96d98 __CFRunLoopRun + 1752
    48 CoreFoundation                 0x18aac6da4 CFRunLoopRunSpecific + 424
    49 GraphicsServices               0x18c530074 GSEventRunModal + 100
    50 UIKit                          0x190d81058 UIApplicationMain + 208
    51 Lucktastic                     0x1000340ac main (main.mm:35)
    52 libdyld.dylib                  0x189ad559c start + 4

    Crashed: com.apple.main-thread
    0  Lucktastic                     0x127d35500 (Missing)
    1  Lucktastic                     0x10032c5e0 U3CDownloadSpriteU3Ec__AnonStorey0_U3CU3Em__0_m4061181975 (GeneratedVirtualInvokers.h:36)
    2  Lucktastic                     0x1003b40d8 U3CExecuteUnityWebRequestU3Ec__Iterator3_MoveNext_m1797967720 (Bulk_Assembly-CSharp_5.cpp:3525)
    3  Lucktastic                     0x10088808c SetupCoroutine_InvokeMoveNext_m2975616245 (Bulk_UnityEngine_3.cpp:2915)
    4  Lucktastic                     0x100b82e8c RuntimeInvoker_Void_t1841601450_Il2CppObject_IntPtr_t(MethodInfo const*, void*, void**) (Il2CppInvokerTable.cpp:4083)
    5  Lucktastic                     0x1015775f4 il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**) (Runtime.cpp:505)
    6  Lucktastic                     0x100fb5e5c scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool) (ScriptingApi_Il2Cpp.cpp:216)
    7  Lucktastic                     0x100faeeac ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool) (ScriptingInvocation.cpp:303)
    8  Lucktastic                     0x100ed8a80 Coroutine::InvokeMoveNext(ScriptingExceptionPtr*) (ScriptingInvocation.h:72)
    9  Lucktastic                     0x100ed86c4 Coroutine::Run(bool*) (Coroutine.cpp:259)
    10 Lucktastic                     0x100eb1e9c AsyncOperation::InvokeCoroutine() (AsyncOperation.cpp:19)
    11 Lucktastic                     0x100d7fc14 UnityWebRequestProto<TransportiPhone, AtomicRefCounter, RedirectHelper, ResponseHelper, DownloadHandler, UploadHandler, HeaderHelper, AsyncOperation>::Job_InvokeCoroutine(AsyncOperation*) (WebRequestProto.h:109)
    12 Lucktastic                     0x100eaa138 BackgroundJobQueue::ExecuteMainThreadJobs() (BackgroundJobQueue.cpp:85)
    13 Lucktastic                     0x100ec1a3c PlayerLoop() (PlayerLoop.cpp:90)
    14 Lucktastic                     0x1010fad68 UnityPlayerLoopImpl(bool) (LibEntryPoint.mm:223)
    15 Lucktastic                     0x1000ee448 UnityRepaint (UnityAppController+Rendering.mm:271)
    16 Lucktastic                     0x1000ee334 -[UnityAppController(Rendering) repaintDisplayLink] (UnityAppController+Rendering.mm:78)
    17 QuartzCore                     0x19081d5dc CA::Display::DisplayLinkItem::dispatch(unsigned long long) + 44
    18 QuartzCore                     0x19081d48c CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 436
    19 IOKit                          0x18d877b9c IODispatchCalloutFromCFMessage + 372
    20 CoreFoundation                 0x18d5a1960 __CFMachPortPerform + 180
    21 CoreFoundation                 0x18d5b9ae4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
    22 CoreFoundation                 0x18d5b9284 __CFRunLoopDoSource1 + 436
    23 CoreFoundation                 0x18d5b6d98 __CFRunLoopRun + 1752
    24 CoreFoundation                 0x18d4e6da4 CFRunLoopRunSpecific + 424
    25 GraphicsServices               0x18ef51074 GSEventRunModal + 100
    26 UIKit                          0x1937a1c9c UIApplicationMain + 208
    27 Lucktastic                     0x1000e80a0 main (main.mm:33)
    28 libdyld.dylib                  0x18c4f559c start + 4

edited answer

I posted this same issue over in Forums, and Unity replied with a fix!

This bug has been fixed and we’re shipping it in patch releases. However, the fix is very trivial and you can do it yourself without risking an upgrade: in the same file find a definition for UnityWWWConnectionDelegate and remove the ‘nonatomic’ flag from the condition property.



old ‘answer’:

unity answers is being weird for me, so posting what’s really a comment as an answer.

we’re getting this too, reported via TestFlight Beta.
same exact stack:

Thread 10 Crashed:
0   libobjc.A.dylib               	0x0000000187a58150 objc_msgSend + 16
1   cue                           	0x00000001000ece4c WaitOnCondition(UnityWWWConnectionDelegate*) + 48 (WWWConnection.mm:126)
2   cue                           	0x00000001000eca2c UnitySendWWWConnection + 544 (WWWConnection.mm:363)
3   cue                           	0x0000000100fffb38 TransportiPhone::DoRequest(core::basic_string<char, core::StringStorageDefault<char> > const&, unsigned long, UnityWebRequestMethod, core::basic_string<char, core::StringStorageDefault<char> > const&, bool, bool, HeaderHelper*, UploadHandler*, DownloadHandler*, ResponseHelper*, bool) + 564 (TransportiPhone.cpp:124)
4   cue                           	0x0000000100fffca4 TransportiPhone::DoRequest(core::basic_string<char, core::StringStorageDefault<char> > const&, unsigned long, UnityWebRequestMethod, core::basic_string<char, core::StringStorageDefault<char> > const&, bool, bool, HeaderHelper*, UploadHandler*, DownloadHandler*, ResponseHelper*) + 48 (TransportiPhone.cpp:134)
5   cue                           	0x0000000100f8d794 UnityWebRequestProto<TransportiPhone, AtomicRefCounter, RedirectHelper, ResponseHelper, DownloadHandler, UploadHandler, HeaderHelper, AsyncOperation>::DoRequest() + 224 (WebRequestProto.h:421)
6   cue                           	0x0000000100f8d2c0 UnityWebRequestProto<TransportiPhone, AtomicRefCounter, RedirectHelper, ResponseHelper, DownloadHandler, UploadHandler, HeaderHelper, AsyncOperation>::Task_DoRequest(void*) + 32 (WebRequestProto.h:125)
7   cue                           	0x0000000100f8d34c UnityWebRequestProto<TransportiPhone, AtomicRefCounter, RedirectHelper, ResponseHelper, DownloadHandler, UploadHandler, HeaderHelper, AsyncOperation>::Job_ExecuteUnityWebRequest(UnityWebRequestProto<TransportiPhone, AtomicRefCounter, RedirectHelper, ResponseHelper, DownloadHandler, UploadHandler, HeaderHelper, AsyncOperation>*) + 64 (WebRequestProto.h:194)
8   cue                           	0x0000000100de10b4 JobQueue::Exec(JobInfo*, long long, int) + 120 (JobQueue.cpp:355)
9   cue                           	0x0000000100de1020 JobQueue::Steal(JobGroup*, JobInfo*, long long, int, bool) + 272 (JobQueue.cpp:521)
10  cue                           	0x0000000100de13c4 JobQueue::ExecuteJobFromQueue() + 100 (JobQueue.cpp:718)
11  cue                           	0x0000000100de146c JobQueue::ProcessJobs(void*) + 72 (JobQueue.cpp:767)
12  cue                           	0x0000000100de0b10 JobQueue::WorkLoop(void*) + 12 (JobQueue.cpp:833)
13  cue                           	0x0000000100f222c8 Thread::RunThreadWrapper(void*) + 96 (Thread.cpp:35)
14  libsystem_pthread.dylib       	0x00000001880a568c _pthread_body + 240 (pthread.c:697)
15  libsystem_pthread.dylib       	0x00000001880a559c _pthread_start + 284 (pthread.c:744)
16  libsystem_pthread.dylib       	0x00000001880a2cb4 thread_start + 4

this corresponds to the lock() call here:

static void WaitOnCondition(UnityWWWConnectionDelegate* delegate)
{
    NSCondition *condition = delegate.condition;
    [condition lock];         <------------------------- here
    [condition wait];
    [condition unlock];
}

my guess is that the condition object has been deallocated before this call is made.

@unity ?