UnityCancelWebRequest method in the UnityWebRequest.mm class causes an EXC_BAD_ACCESS crash

Hey, everybody

After we migrated our project from Unity 2020 LTS to Unity 2022 LTS we found that crashlytics is receiving a noticeable number of crashes with the next stacktrace:

EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000e537792a0

Crashed: com.apple.main-thread
0 libobjc.A.dylib 0x3120 objc_retain + 16
1 UnityFramework 0x8e40 UnityCancelWebRequest + 450 (UnityWebRequest.mm:450)
2 UnityFramework 0x1834c48 TransportiPhone::AbortWithError(WebError) + 168 (TransportiPhone.cpp:168)
3 UnityFramework 0x1838f6c UnityWebRequestProto<UnityWebRequestTransport, AtomicRefCounter, RedirectHelper, ResponseHelper, DownloadHandler, UploadHandler, CertificateHandler, HeaderHelper, AsyncOperation>::Abort() + 782 (UnityWebRequestProto.h:782)
4 UnityFramework 0x1469b60 UnityWebRequest_CUSTOM_Abort(ScriptingBackendNativeObjectPtrOpaque*) + 740 (UnityWebRequestBindings.gen.cpp:740)
<...>

The full stacktrace can be found at the following link

Аfter doing some research we found that this crash was present before we upgraded to Unity 2022. This crash is mostly reproduced on ipads, on iOS versions starting from 15.

Unfortunately we were not able to reproduce this behavior ourselves, so we can’t say what steps or what request causes this behavior. Moreover, there are several third-party modules in our application (analytics, advertising, etc.) that also use unity web requests and may be the source of this crash.

In the build directory there is a class UnityURLRequest.mm, in which we can find the method UnityCancelWebRequest.

<...>
extern "C" void UnityCancelWebRequest(void* connection)
{
@autoreleasepool
{
UnityURLRequest* request = (__bridge UnityURLRequest*)connection;
[unityWebRequestLock lock];
[unityWebRequestSession getAllTasksWithCompletionHandler:^(NSArray<NSURLSessionTask*>* _Nonnull tasks) {
for (unsigned i = 0; i < tasks.count; ++i)
if (tasks*.taskIdentifier == request.taskIdentifier)*
*{*
_[tasks *cancel];*_
_*break;*_
_*}*_
_*}];*_
_*[unityWebRequestLock unlock];*_
_*}*_
_*}*_
_*<...>*_
_*```*_
_*According to the stacktrace, the crash is caused by this line:*_
<em><em>```UnityURLRequest* request = (__bridge UnityURLRequest*)connection;```</em></em>
_*Perhaps someone has encountered a similar problem and can suggest how it can be solved?*_

The code does not look like newest. There was a race condition with abort, should be fixed in latest version.

Thanks for the quick response.

Please clarify, in order for the corrected code to get into my project I need to update the Unity version?

I am currently using Unity 2022.3.13.

Yes, please update Unity. Right now .20 is latest release in the series.
You can search Unity release notes for “UnityWebRequest”, if you want to find out the exact version this was fixed in.

Thanks, we’ll try to update and release asap